SCRIPT FOR DELETE TWEETS FIXED
I was trying to use the scripts you shared here. Some of them worked for me but they had a problem, they stop in a moment, often after deleting 10 or 11 tweets. I made this version. Hope you like it. Thanks for sharing.
// TO DELETE TWEETS MUST BE RUN FROM https://twitter.com/{yourTwitterHandle}
// TO DELETE REPLIES MUST BE RUN FROM https://twitter.com/{yourTwitterHandle}/with_replies
// IMPORTANT IMPORTANT IMPORTANT - SET YOUR TWITTER HANDLE IN THE NEXT LINE!
const yourTwitterHandle = "@YourAccount";
// one every 10 seconds to avoid Twitter noticing
const waitTimeSeconds = 10;
const sleep = async (seconds) => new Promise(resolve => setTimeout(resolve, seconds * 1000));
const main = async () => {
while (true) {
try {
await walkTweets();
} catch (error) {
console.error("An error occurred in main loop:", error);
}
console.log(`Restarting in ${waitTimeSeconds} seconds...`);
await sleep(waitTimeSeconds);
}
};
const walkTweets = async () => {
let articles = document.getElementsByTagName('article');
for (let article of articles) {
article.scrollIntoView();
if (article.textContent.includes(yourTwitterHandle)) {
console.log(`Found tweet or reply, sleeping for ${waitTimeSeconds} seconds...`);
await sleep(waitTimeSeconds);
try {
const tweetElement = article.querySelector('[aria-label="More"]');
if (tweetElement) {
article.scrollIntoView();
console.log('Clicking "More" button...');
tweetElement.click();
await waitForMenuItem();
console.log('Clicking "Delete" confirmation button...');
const confirmBtn = document.querySelector('[data-testid="confirmationSheetConfirm"]');
if (confirmBtn) {
confirmBtn.click();
console.log('Delete confirmed.');
} else {
console.warn("Delete confirmation button not found.");
}
}
} catch (e) {
console.error("Error processing tweet:", e);
}
}
}
};
const waitForMenuItem = async () => {
return new Promise((resolve, reject) => {
const observer = new MutationObserver(() => {
const menuItem = document.querySelector('div[role="menuitem"][tabindex="0"]');
if (menuItem) {
console.log('Delete menu item found, clicking...');
menuItem.click();
observer.disconnect();
resolve();
}
});
observer.observe(document.body, {
childList: true,
subtree: true
});
// Timeout after 10 seconds
setTimeout(() => {
observer.disconnect();
reject(new Error('Delete menu item not found within timeout.'));
}, 10000);
});
};
// Start the main loop
main();