Skip to content

Instantly share code, notes, and snippets.

@MuntashirAkon
Created May 3, 2020 14:38
Show Gist options
  • Save MuntashirAkon/ec53338b9a5e760fc9344d8464f509d1 to your computer and use it in GitHub Desktop.
Save MuntashirAkon/ec53338b9a5e760fc9344d8464f509d1 to your computer and use it in GitHub Desktop.
Delete all tweets from twitter
// NOTE: This script is far from complete, especially “Likes” section
// https://stackoverflow.com/a/2706236/4147849
function eventFire(el, etype) {
if (el.fireEvent) {
el.fireEvent('on' + etype);
} else {
var evObj = document.createEvent('Events');
evObj.initEvent(etype, true, false);
el.dispatchEvent(evObj);
}
}
// https://stackoverflow.com/a/39914235/4147849
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function delete_tweets(sleep_time, counter) {
console.log("Called")
// Get posts
let carets = document.getElementsByClassName('css-1dbjc4n r-1niwhzg r-sdzlij r-1p0dtai r-xoduu5 r-1d2f490 r-podbf7 r-u8s1d r-zchlnj r-ipm5af r-o7ynqc r-6416eg');
let retweets = document.getElementsByClassName('css-901oao r-1awozwy r-nw8l94 r-6koalj r-1qd0xha r-a023e6 r-16dba41 r-1h0z5md r-ad9z0x r-bcqeeo r-o7ynqc r-clp7b1 r-3s2u2q r-qvutc0');
let retweet_count = 0;
// End function if no more posts found
if (carets.length == 0) return;
// Iterate over posts
for (let i = 0; i < carets.length - counter; ++i) {
// Dropdown Menu
eventFire(carets[i], 'click')
let menuBtn = document.getElementsByClassName('css-1dbjc4n r-1loqt21 r-18u37iz r-1j3t67a r-9qu9m4 r-o7ynqc r-6416eg r-13qz1uu')[0];
switch (menuBtn.innerText) {
case "Delete":
// Select “Delete” button
eventFire(menuBtn, 'click')
// Click delete
eventFire(document.getElementsByClassName('css-18t94o4 css-1dbjc4n r-1dgebii r-42olwf r-sdzlij r-1phboty r-rs99b7 r-16y2uox r-1w2pmg r-1vuscfd r-1dhvaqw r-1fneopy r-o7ynqc r-6416eg r-lrvibr')[0], 'click')
break;
case "Embed Tweet":
// Remove previous menu
menuBtn.parentNode.remove();
if (/\/likes/.test(document.URL)) {
eventFire(document.getElementsByClassName('css-901oao r-1awozwy r-daml9f r-6koalj r-1qd0xha r-a023e6 r-16dba41 r-1h0z5md r-ad9z0x r-bcqeeo r-o7ynqc r-clp7b1 r-3s2u2q r-qvutc0')[0], 'click');
++counter;
} else if (/with_replies/.test(document.URL)) {
continue;
} else {
// Select “Retweet” button
eventFire(retweets[retweet_count], 'click');
// Click on “Undo Retweet”
eventFire(document.getElementsByClassName('css-1dbjc4n r-1loqt21 r-18u37iz r-1j3t67a r-9qu9m4 r-o7ynqc r-6416eg r-13qz1uu')[0], 'click');
++retweet_count;
}
break;
}
console.log("Deleted " + (i+1) + "th Tweet");
await sleep(sleep_time);
}
// Scroll to bottom, https://stackoverflow.com/a/33193668/4147849
let scrollingElement = (document.scrollingElement || document.body);
scrollingElement.scrollTop = scrollingElement.scrollHeight;
await sleep(sleep_time*5); // It might take some time to load the page
delete_tweets(sleep_time, counter);
}
// Example: Sleep 1.5 seconds to perform each delete
delete_tweets(1500, 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment