Skip to content

Instantly share code, notes, and snippets.

@maanimis
Last active October 29, 2024 16:13
Show Gist options
  • Save maanimis/547ed06ffd757030b88eb5cf140d1684 to your computer and use it in GitHub Desktop.
Save maanimis/547ed06ffd757030b88eb5cf140d1684 to your computer and use it in GitHub Desktop.
A script designed to extract information from twitter / x
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