Skip to content

Instantly share code, notes, and snippets.

@Tyrrrz
Last active August 29, 2022 21:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tyrrrz/f0f2e26e1cc4c22812cae12dde399d13 to your computer and use it in GitHub Desktop.
Save Tyrrrz/f0f2e26e1cc4c22812cae12dde399d13 to your computer and use it in GitHub Desktop.
Useful browser scripts
// Likes all tweets on the screen
// Copy the lines below and add them as a bookmark
javascript: (() => {
const processedTweetIds = new Set();
const parseTweet = (el) => {
const id = [...el.querySelectorAll("a[href]")]
.map((a) => a.href)
.find((href) => href.includes("/status/"))
.split("/")
.at(-1);
const uid = [...el.querySelectorAll("a[href]")]
.map((a) => a.href)
.find((href) => href.includes("/status/"))
.split("/")
.at(-3);
const text = el.querySelector('[data-testid="tweetText"]')?.textContent;
const isLiked = !!el.querySelector('[data-testid="unlike"]');
const open = () => {
el.querySelector("time[datetime]")?.click();
};
const like = () => {
el.querySelector('[data-testid="like"]')?.click();
};
const unlike = () => {
el.querySelector('[data-testid="unlike"]')?.click();
};
return {
el,
id,
uid,
text,
isLiked,
open,
like,
unlike,
};
};
const getNextTweet = () => {
for (const el of document.querySelectorAll('[data-testid="tweet"]')) {
const tweet = parseTweet(el);
if (!processedTweetIds.has(tweet.id)) {
processedTweetIds.add(tweet.id);
return tweet;
}
}
return null;
};
const isRateLimited = () => {
return (
document
.querySelector('[data-testid="toast"]')
?.textContent?.includes("we can’t complete this action right now") ??
false
);
};
const interval = setInterval(() => {
const tweet = getNextTweet();
if (tweet && !isRateLimited()) {
console.log("Tweet", tweet);
scrollTo(0, document.body.scrollHeight);
tweet.el.scrollIntoView();
tweet.like();
} else {
clearInterval(interval);
processedTweetIds.clear();
console.log(`Liked ${processedTweetIds.size} tweets`);
}
}, 250);
})();
// Suggests titles based on top posts in the same subreddit
// Copy the lines below and add them as a bookmark
javascript: (async () => {
const createElement = (tag, attrs = {}, ...children) => {
const el = document.createElement(tag);
for (const [key, value] of Object.entries(attrs)) {
el.setAttribute(key, value);
}
for (const child of children) {
if (typeof child === "string") {
el.appendChild(document.createTextNode(child));
} else {
el.appendChild(child);
}
}
return el;
};
const createSuggestionSection = () => {
const existing = document.querySelector("#title-suggestions");
if (existing) {
return existing;
}
const el = createElement("div", {
id: "title-suggestions",
style: "margin: 0.5em 0; font-size: 0.85em",
});
document.querySelector("#title-field").appendChild(el);
return el;
};
const setSuggestions = (suggestions) => {
const section = createSuggestionSection();
section.innerHTML = "";
for (const suggestion of suggestions) {
const a = createElement(
"a",
{
href: "#",
},
suggestion
);
a.onclick = (e) => {
e.preventDefault();
setPostTitle(suggestion);
};
const div = createElement(
"div",
{
style: "margin: 0.15em 0",
},
a
);
section.appendChild(div);
}
};
const setPostTitle = (title) => {
document.querySelector("textarea[name='title']").value = title;
};
const getTopTitles = async () => {
const subreddit = document
.querySelector("link[rel='canonical']")
.href.split("/")
.at(-3);
const top = await fetch(
`${location.origin}/r/${subreddit}/top.json?sort=top&t=all&limit=25`
).then((res) => res.json());
const posts = top.data.children.map((post) => post.data);
return posts.map((post) => post.title);
};
const titles = await getTopTitles();
setSuggestions(titles);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment