Skip to content

Instantly share code, notes, and snippets.

@the-cc-dev
Forked from cqsd/unlike-tweets.js
Created December 17, 2019 01:52
Show Gist options
  • Save the-cc-dev/df9881da38180fb095b93fbf4e8534ee to your computer and use it in GitHub Desktop.
Save the-cc-dev/df9881da38180fb095b93fbf4e8534ee to your computer and use it in GitHub Desktop.
// unlike tweets using twitter web (use this on https://twitter.com/USERNAME/likes)
//
// @param {n} the number of likes to delete
// @param {maxWaitRetries} maximum number of times to try loading more likes when
// everything on the page has been unliked
const unlike = async (n, maxWaitRetries = 10) => {
let total = 0;
while(total < n) {
// get every like button on the page and click it
const likeButtons = await document.querySelectorAll('[data-testid="unlike"]');
likeButtons.forEach(e => e.click())
const unliked = likeButtons.length;
total += unliked;
console.log(`${unliked} unliked, ${total} total so far`);
// delete every tweet on the page from the DOM to avoid slowdown
document.querySelectorAll('[role="article"]').forEach(e => e.remove());
// scroll to the bottom of the page and wait for new tweets to load
window.scrollTo(0,document.body.scrollHeight);
const retries = 0;
while (!document.querySelector('[role="article"]')) {
await new Promise(r => setTimeout(r, 300));
console.log('waiting for tweets to load');
retries += 1;
if (retries >= maxWaitRetries) {
throw Error('Exceeded maximum number of retries for getting more tweets');
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment