Skip to content

Instantly share code, notes, and snippets.

@z0rs
Last active September 1, 2024 16:01
Show Gist options
  • Save z0rs/e052948ada580f784e30bae2d99caabe to your computer and use it in GitHub Desktop.
Save z0rs/e052948ada580f784e30bae2d99caabe to your computer and use it in GitHub Desktop.
const deleteTweetsBetweenDates = async (startDate, endDate) => {
const processedButtons = new Set();
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));
console.log("Script started");
while (true) {
console.log("Looking for tweets...");
const deleteButtons = Array.from(document.querySelectorAll('[data-testid="caret"]'))
.filter(button => !processedButtons.has(button));
console.log(`Found ${deleteButtons.length} new delete buttons`);
if (deleteButtons.length === 0) {
console.log("No more tweets to process. Exiting...");
break;
}
for (const button of deleteButtons) {
const tweetContainer = button.closest('[data-testid="tweet"]');
const dateElement = tweetContainer?.querySelector('time');
if (!dateElement) {
console.log("No date found, skipping tweet");
continue;
}
const tweetDate = new Date(dateElement.getAttribute('datetime'));
console.log(`Tweet date: ${tweetDate}`);
if (tweetDate < startDate || tweetDate > endDate) {
console.log("Tweet out of date range, skipping");
continue;
}
processedButtons.add(button);
console.log("Opening tweet menu");
button.click();
await delay(250);
const menuItems = Array.from(document.querySelectorAll('[role="menuitem"]'));
const deleteOption = menuItems.find(item => item.textContent === 'Delete');
if (deleteOption) {
console.log("Found delete option, deleting tweet");
deleteOption.click();
await delay(500);
const confirmButton = document.querySelector('[data-testid="confirmationSheetConfirm"]');
if (confirmButton) {
console.log("Confirming delete");
confirmButton.click();
await delay(3000);
} else {
console.log("Confirmation button not found, skipping");
}
} else {
const unretweetButton = tweetContainer?.querySelector('[data-testid="unretweet"]');
if (unretweetButton) {
console.log("Found unretweet option, unretweeting tweet");
unretweetButton.click();
await delay(500);
const confirmUnretweetButton = document.querySelector('[data-testid="unretweetConfirm"]');
if (confirmUnretweetButton) {
console.log("Confirming unretweet");
confirmUnretweetButton.click();
await delay(3000);
} else {
console.log("Unretweet confirmation button not found, skipping");
}
} else {
console.log("No delete or unretweet option found, skipping");
}
}
}
console.log("Finished processing batch of tweets.");
}
console.log("Selected tweets deleted successfully!");
};
// Replace with your desired date range (format: YYYY-MM-DD).
deleteTweetsBetweenDates(new Date('2024-08-10'), new Date('2024-08-31'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment