Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@antlionguard
Last active March 29, 2024 00:17
Show Gist options
  • Star 50 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • Save antlionguard/000e24bb039e8824fa5c58d6e324c78e to your computer and use it in GitHub Desktop.
Save antlionguard/000e24bb039e8824fa5c58d6e324c78e to your computer and use it in GitHub Desktop.
With this script, you can remove all retweets you are retweeted on Twitter.
const timer = ms => new Promise(res => setTimeout(res, ms));
// Unretweet normally
const unretweetTweet = async (tweet) => {
await tweet.querySelector('div[data-testid="unretweet"]').click();
await timer(250);
await document.querySelector('div[data-testid="unretweetConfirm"]').click();
console.log('****// Unretweeted Successfully //****')
}
// Sometimes twitter shows your retweet but green retweet button is invisible and therefore you need to retweet again for make unreweet. This function is for that.
const unretweetUnretweetedTweet = async (tweet) => {
await tweet.querySelector('div[data-testid="retweet"]').click();
await timer(250);
await document.querySelector('div[data-testid="retweetConfirm"]').click();
console.log('****// Retweeted Successfully //****')
await timer(250);
unretweetTweet(tweet);
}
setInterval(async () =>
{
// Get all tweets
const retweetedTweetList = document.querySelectorAll('span[data-testid="socialContext"]');
console.log('****// Retweeted Tweet List Collected //****')
for (const retweet of retweetedTweetList) {
const tweetWrapper = retweet.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement;
tweetWrapper.scrollIntoView();
const isRetweeted = tweetWrapper.querySelector('div[data-testid="unretweet"]');
if (isRetweeted) {
console.log('****// Green Retweet Button Found - Starting "unretweetTweet" process //****')
await unretweetTweet(tweetWrapper);
} else {
console.log('****// Green Retweet Button Not Found - Starting "unretweetUnretweetedTweet" process //****')
await unretweetUnretweetedTweet(tweetWrapper);
}
await timer(2000);
}
console.log('****// List Completed //****')
console.log('****// Scrolling //****')
console.log(' ')
console.log(' ')
console.log(' ')
console.log(' ')
console.log(' ')
console.log(' ')
console.log(' ')
console.log(' ')
await window.scrollTo(0, document.body.scrollHeight);
}, 60000);
@OutOfThisPlanet
Copy link

This still works :)

@talhatugsat
Copy link

thanks

@enosV
Copy link

enosV commented Jul 23, 2022

Sick! Works like a charm!

@WaffleThief123
Copy link

Confirmed working as of November 2022.

@viltemcduck
Copy link

Is there a way to make this delete retweets only within a specific time period? For example from June 2022 to December 2021?

@imuzafarqadir
Copy link

worked fine for me

@kcanugz
Copy link

kcanugz commented Apr 29, 2023

DAMN! APRIL 29TH AND IT'S STILL WORKING! THANKS BRO! IT JUST DELETED ALL OF MY 7K TWEETS! But there's only problem the number of my tweets which seems above the page still same. Its shown like 7K tweets.

@urstrulyrocky
Copy link

not working i got undefined

can you please tell me how to use this and how can i tell if its working?

Here's the steps:

  1. Open your twitter profile
  2. Switch retweets tab
  3. Open your browser devtools (Pressing F12)
  4. Click the console tab
  5. Paste the code into console and wait

The script begins after 60 second.

not working i got undefined for first and uncaught error for second one please help

@urstrulyrocky
Copy link

bro i got undefined in browser please help me out

@CapricaTix
Copy link

This is a terrific idea, but I'm not sure I've got it working. It's throwing a few errors that might be, in light of the new ownership, to do with Twitter itself! Has anyone else got it working recently? It'd be great if someone could update! Cheers

@bananaTiko
Copy link

Confirmed working as of June 26, 2022

@cerkes78
Copy link

29.06.2023 still works. Thanks.

@loai271
Copy link

loai271 commented Jul 12, 2023

I got the next message

Uncaught (in promise) TypeError: Cannot read properties of null (reading 'click')
at unretweetTweet (:7:74)
at async :33:19

what does it mean? and how to fix? @antlionguard

@brunarafaela
Copy link

not working for me too

@Bossman556
Copy link

Bossman556 commented Aug 30, 2023

This code does the same but you dont have to wait.

const timer = ms => new Promise(res => setTimeout(res, ms));

const unretweetTweet = async (tweet) => {
  try {
    await tweet.querySelector('div[data-testid="unretweet"]').click();
    await timer(250);
    await document.querySelector('div[data-testid="unretweetConfirm"]').click();
    console.log('****// Unretweeted Successfully //****');
  } catch (error) {
    console.error('Error during unretweet:', error);
  }
};

const unretweetUnretweetedTweet = async (tweet) => {
  try {
    await tweet.querySelector('div[data-testid="retweet"]').click();
    await timer(250);
    await document.querySelector('div[data-testid="retweetConfirm"]').click();
    console.log('****// Retweeted Successfully //****');
    await timer(250);
    unretweetTweet(tweet);
  } catch (error) {
    console.error('Error during unretweetUnretweetedTweet:', error);
  }
};

const processTweets = async () => {
  const retweetedTweetList = document.querySelectorAll('span[data-testid="socialContext"]');
  console.log('****// Retweeted Tweet List Collected //****');

  for (const retweet of retweetedTweetList) {
    const tweetWrapper = retweet.closest('[data-testid="tweet"]');
    tweetWrapper.scrollIntoView();

    const isRetweeted = tweetWrapper.querySelector('div[data-testid="unretweet"]');
    if (isRetweeted) {
      console.log('****// Green Retweet Button Found - Starting "unretweetTweet" process //****');
      await unretweetTweet(tweetWrapper);
    } else {
      console.log('****// Green Retweet Button Not Found - Starting "unretweetUnretweetedTweet" process //****');
      await unretweetUnretweetedTweet(tweetWrapper);
    }
    await timer(2000);
  }
  console.log('****// List Completed //****');
  console.log('****// Scrolling //****');
  console.log('                  ');
  console.log('                  ');
  console.log('                  ');
  console.log('                  ');
  console.log('                  ');
  console.log('                  ');
  console.log('                  ');
  console.log('                  ');
  window.scrollTo(0, document.body.scrollHeight);

  // Call processTweets again using requestAnimationFrame to keep the loop going
  requestAnimationFrame(processTweets);
};

// Start the process initially
processTweets();

@cerkes78
Copy link

it works well. Thx Bossman556.

@antlionguard
Copy link
Author

This code does the same but you dont have to wait.

const timer = ms => new Promise(res => setTimeout(res, ms));

const unretweetTweet = async (tweet) => {
  try {
    await tweet.querySelector('div[data-testid="unretweet"]').click();
    await timer(250);
    await document.querySelector('div[data-testid="unretweetConfirm"]').click();
    console.log('****// Unretweeted Successfully //****');
  } catch (error) {
    console.error('Error during unretweet:', error);
  }
};

const unretweetUnretweetedTweet = async (tweet) => {
  try {
    await tweet.querySelector('div[data-testid="retweet"]').click();
    await timer(250);
    await document.querySelector('div[data-testid="retweetConfirm"]').click();
    console.log('****// Retweeted Successfully //****');
    await timer(250);
    unretweetTweet(tweet);
  } catch (error) {
    console.error('Error during unretweetUnretweetedTweet:', error);
  }
};

const processTweets = async () => {
  const retweetedTweetList = document.querySelectorAll('span[data-testid="socialContext"]');
  console.log('****// Retweeted Tweet List Collected //****');

  for (const retweet of retweetedTweetList) {
    const tweetWrapper = retweet.closest('[data-testid="tweet"]');
    tweetWrapper.scrollIntoView();

    const isRetweeted = tweetWrapper.querySelector('div[data-testid="unretweet"]');
    if (isRetweeted) {
      console.log('****// Green Retweet Button Found - Starting "unretweetTweet" process //****');
      await unretweetTweet(tweetWrapper);
    } else {
      console.log('****// Green Retweet Button Not Found - Starting "unretweetUnretweetedTweet" process //****');
      await unretweetUnretweetedTweet(tweetWrapper);
    }
    await timer(2000);
  }
  console.log('****// List Completed //****');
  console.log('****// Scrolling //****');
  console.log('                  ');
  console.log('                  ');
  console.log('                  ');
  console.log('                  ');
  console.log('                  ');
  console.log('                  ');
  console.log('                  ');
  console.log('                  ');
  window.scrollTo(0, document.body.scrollHeight);

  // Call processTweets again using requestAnimationFrame to keep the loop going
  requestAnimationFrame(processTweets);
};

// Start the process initially
processTweets();

no. i added delay for preventing to rate limit.

@ItIsInx
Copy link

ItIsInx commented Nov 8, 2023

Worked for me ♥
2023

@Lime-oss-hash
Copy link

Wait, so all the content is gone, but why is the number still there?

@maroz14
Copy link

maroz14 commented Jan 7, 2024

@antlionguard , @Bossman556 please would you mind to make this script as chrome addons extension but with popup read and show every thing as showing in console? please

@Mili4Matic
Copy link

Still working

@antlionguard
Copy link
Author

@antlionguard , @Bossman556 please would you mind to make this script as chrome addons extension but with popup read and show every thing as showing in console? please

good idea. i will work on it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment