Skip to content

Instantly share code, notes, and snippets.

@0x3n0
Last active August 15, 2024 11:57
Show Gist options
  • Select an option

  • Save 0x3n0/5baa4d71a7cb1bd88b98b1e232b01e8a to your computer and use it in GitHub Desktop.

Select an option

Save 0x3n0/5baa4d71a7cb1bd88b98b1e232b01e8a to your computer and use it in GitHub Desktop.
const startDate = new Date('2022-01-01');
const endDate = new Date('2023-01-01');
const waitForElemToExist = async (selector, timeout = 5000) => {
return new Promise((resolve) => {
const element = document.querySelector(selector);
if (element) return resolve(element);
const observer = new MutationObserver(() => {
const element = document.querySelector(selector);
if (element) {
resolve(element);
observer.disconnect();
}
});
observer.observe(document.body, { subtree: true, childList: true });
setTimeout(() => {
observer.disconnect();
resolve(null);
}, timeout);
});
};
const deleteTweets = async () => {
const more = '[data-testid="tweet"] [aria-label="More"][data-testid="caret"]';
let delCount = 0;
while (document.querySelectorAll(more).length > 0) {
await new Promise(r => setTimeout(r, 1200 + Math.random() * 800));
// Cleanup before scrolling
document.querySelectorAll('[aria-label="Profile timelines"]+section [data-testid="cellInnerDiv"]>div>div>div').forEach(x => x.remove());
document.querySelectorAll('[aria-label="Profile timelines"]+section [data-testid="cellInnerDiv"]>div>div>[role="link"]').forEach(x => x.remove());
document.querySelector('[aria-label="Profile timelines"]').scrollIntoView({ behavior: 'smooth' });
try {
const tweetDateElem = await waitForElemToExist('[data-testid="tweet"] time');
if (!tweetDateElem) continue;
const tweetDate = new Date(tweetDateElem.getAttribute('datetime'));
if (tweetDate >= startDate && tweetDate < endDate) {
const caret = await waitForElemToExist(more);
if (!caret) continue;
caret.click();
const menu = await waitForElemToExist('[role="menuitem"]');
if (!menu) continue;
menu.click();
const confirmation = await waitForElemToExist('[data-testid="confirmationSheetConfirm"]');
if (confirmation) confirmation.click();
delCount++;
if (delCount % 10 === 0) {
console.log(`${new Date().toUTCString()} Deleted ${delCount} Tweets`);
}
} else if (tweetDate < startDate) {
break;
}
} catch (error) {
console.error(`Error during deletion process: ${error.message}`);
break;
}
}
console.log(`Completed deletion. Total tweets deleted: ${delCount}`);
};
deleteTweets();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment