Last active
October 29, 2024 16:13
-
-
Save maanimis/547ed06ffd757030b88eb5cf140d1684 to your computer and use it in GitHub Desktop.
A script designed to extract information from twitter / x
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const xPath = { | |
content: | |
"/html/body/div[1]/div/div/div[2]/main/div/div/div/div/div/section/div/div/div[1]/div/div/article/div/div/div[3]/div[1]", | |
postDate: | |
"/html/body/div[1]/div/div/div[2]/main/div/div/div/div/div/section/div/div/div[1]/div/div/article/div/div/div[3]/div[last()-1]", | |
createDate: | |
"/html/body/div[1]/div/div/div[2]/main/div/div/div/div/div/div[3]/div/div/div/div/div[4]/div/span[3]/span", | |
}; | |
const key = "priv8"; | |
const link = location.href; | |
const url = new URL(link); | |
function selectByXpath(x) { | |
const element = document.evaluate( | |
x, | |
document, | |
null, | |
XPathResult.FIRST_ORDERED_NODE_TYPE, | |
null | |
).singleNodeValue; | |
return element.innerText; | |
} | |
function getPostInfo() { | |
const username = url.pathname.split("/")[1]; | |
const tweetID = url.pathname.split("/").reverse()[0]; | |
const content = selectByXpath(xPath.content); | |
const postDate = selectByXpath(xPath.postDate); | |
const data = JSON.stringify({ | |
link, | |
username, | |
tweetID, | |
content, | |
postDate, | |
}); | |
sessionStorage.setItem(key, data); | |
location.href = "/" + username; | |
} | |
function getProfileInfo() { | |
const postData = JSON.parse(sessionStorage.getItem(key)); | |
const isVerified = document.querySelector( | |
'button > [data-testid="icon-verified"]' | |
) | |
? true | |
: false; | |
// const createDate = selectByXpath(xPath.createDate); | |
const createDate = document.querySelector('[data-testid="UserJoinDate"]')?.innerText | |
const data = JSON.stringify({ ...postData, isVerified, createDate }); | |
sessionStorage.setItem(key, data); | |
alert(data); | |
} | |
if (url.pathname.split("/").length > 3) { | |
getPostInfo(); | |
} else { | |
getProfileInfo(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment