Skip to content

Instantly share code, notes, and snippets.

@nathansmith
Last active January 27, 2025 16:58
Show Gist options
  • Save nathansmith/a2ec33cdfdd78851feb860711b962001 to your computer and use it in GitHub Desktop.
Save nathansmith/a2ec33cdfdd78851feb860711b962001 to your computer and use it in GitHub Desktop.

🐦 Look, I'm not saying you should do this.

  1. Go to your Twitter profile page.

  2. Paste this into the console.

  3. Hit enter.

But if you did — and left it running for a long while — it might eventually delete all tweets and retweets from your Twitter account.

Again, not sure why anyone would do that. Perhaps as a fun little JavaScript experiment. Pretty sure it voids the warranty. Just sayin'.

// ===============
// START: Closure.
// ===============
(() => {
// ==========
// Set later.
// ==========
let timerForElement;
// =========================
// Helper: wait for element.
// =========================
const waitForElement = (selector) => {
// Clear timer.
clearInterval(timerForElement);
// Expose promise.
return new Promise((resolve) => {
// Set timer.
timerForElement = setInterval(() => {
// Get element.
const element = document.querySelector(selector);
// Element exists: YES.
if (element) {
// Clear timer.
clearInterval(timerForElement);
// Promise resolve.
resolve(element);
}
// Slight delay.
}, 16);
});
};
// ======================
// Helper: click buttons.
// ======================
const handleButtonClick = () => {
// Get elements.
const section = document.querySelector('section[aria-labelledby="accessible-list-0"]');
const buttonForMenu = section?.querySelector('[aria-haspopup="menu"]');
const buttonForUndoRetweet = section?.querySelector('[data-testid="unretweet"]');
// ======================================
// Button for "undo retweet" exists: YES.
// ======================================
if (buttonForUndoRetweet) {
// Fire events.
buttonForUndoRetweet.click();
// Get elements.
waitForElement('[data-testid="unretweetConfirm"]').then((buttonForConfirm) => {
// Button exists: YES.
if (buttonForConfirm) {
// Fire events.
buttonForConfirm.click();
}
});
// ==================================
// Button for "dot menu" exists: YES.
// ==================================
} else if (buttonForMenu) {
// Fire events.
buttonForMenu.click();
// Get elements.
waitForElement('[data-testid="Dropdown"] [role="menuitem"]').then((buttonForDelete) => {
// Button exists: YES.
if (buttonForDelete && buttonForDelete.textContent.match('Delete')) {
// Fire events.
buttonForDelete.click();
// Get elements.
waitForElement('[data-testid="confirmationSheetConfirm"]').then((buttonForConfirm) => {
// Button exists: YES.
if (buttonForConfirm) {
// Fire events.
buttonForConfirm.click();
}
});
}
});
}
}
// ========
// Kickoff.
// ========
setInterval(handleButtonClick, 5000);
// =============
// END: Closure.
// =============
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment