Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance script to handle infinite scrolling #4

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 70 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,48 +1,83 @@
Script to delete all X / Twitter tweets. (Credits go to ChatGPT, I didn't write any of this code)

### How to run:
- Open Chrome and go to your X profile (https://x.com/USERNAME/with_replies)
- Open Chrome Developer Console. Copy/Paste the code:

```
- Open Chrome and go to your X profile (https://x.com/USERNAME/with_replies)
- Open Chrome Developer Console. Copy/Paste the code:

```javascript
const deleteAllTweets = async () => {
const processedButtons = new Set();
const getDeleteButtons = () => Array.from(document.querySelectorAll('[data-testid="tweet"] [data-testid="caret"]'));
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));

while (true) {
const deleteButtons = getDeleteButtons().filter(button => !processedButtons.has(button));
if (deleteButtons.length === 0) break;

for (const button of deleteButtons) {
processedButtons.add(button);
button.click();
await delay(250);

const menuItems = Array.from(document.querySelectorAll('[role="menuitem"]'));
const deleteOption = menuItems.find(item => item.textContent === 'Delete');

if (deleteOption) {
deleteOption.click();
document.querySelector('[data-testid="confirmationSheetConfirm"]')?.click();
await delay(3000);
} else {
const tweetContainer = button.closest('[data-testid="tweet"]');
const unretweetButton = tweetContainer?.querySelector('[data-testid="unretweet"]');

if (unretweetButton) {
unretweetButton.click();
await delay(250);
document.querySelector('[data-testid="unretweetConfirm"]')?.click();
await delay(3000);
const processedButtons = new Set();
const getDeleteButtons = () =>
Array.from(
document.querySelectorAll(
'[data-testid="tweet"] [data-testid="caret"]'
)
);
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
const scrollToEnd = () => window.scrollTo(0, document.body.scrollHeight);

const attemptDelete = async (button) => {
processedButtons.add(button);
button.click();
await delay(250 + Math.random() * 250); // Adding some randomness to mimic human behavior

const menuItems = Array.from(
document.querySelectorAll('[role="menuitem"]')
);
const deleteOption = menuItems.find((item) =>
item.textContent.includes("Delete")
);

if (deleteOption) {
deleteOption.click();
document
.querySelector('[data-testid="confirmationSheetConfirm"]')
?.click();
await delay(3000 + Math.random() * 1000);
} else {
const tweetContainer = button.closest('[data-testid="tweet"]');
const unretweetButton = tweetContainer?.querySelector(
'[data-testid="unretweet"]'
);
if (unretweetButton) {
unretweetButton.click();
await delay(250 + Math.random() * 250);
document
.querySelector('[data-testid="unretweetConfirm"]')
?.click();
await delay(3000 + Math.random() * 1000);
}
}
};

while (true) {
const deleteButtons = getDeleteButtons().filter(
(button) => !processedButtons.has(button)
);
if (deleteButtons.length === 0) {
scrollToEnd();
await delay(5000); // Wait for more tweets to load
if (
getDeleteButtons().filter(
(button) => !processedButtons.has(button)
).length === 0
) {
break; // Exit if no new tweets are loaded
} else {
continue; // Otherwise, continue processing new tweets
}
}

for (const button of deleteButtons) {
await attemptDelete(button);
}
}
}
}

console.log('All tweets deleted successfully!');
console.log("All tweets deleted successfully!");
};

deleteAllTweets();
```

<img width="813" alt="Screenshot 2023-12-31 at 7 18 45 PM" src="https://github.com/techleadhd/XDelete/assets/61847557/473165c5-9b7c-4065-98fd-5856fcbfb3a8">
58 changes: 35 additions & 23 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,50 @@ const deleteAllTweets = async () => {
const processedButtons = new Set();
const getDeleteButtons = () => Array.from(document.querySelectorAll('[data-testid="tweet"] [data-testid="caret"]'));
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));
const scrollToEnd = () => window.scrollTo(0, document.body.scrollHeight);

while (true) {
const deleteButtons = getDeleteButtons().filter(button => !processedButtons.has(button));
if (deleteButtons.length === 0) break;
const attemptDelete = async (button) => {
processedButtons.add(button);
button.click();
await delay(250 + Math.random() * 250); // Adding some randomness to mimic human behavior

for (const button of deleteButtons) {
processedButtons.add(button);
button.click();
await delay(250);
const menuItems = Array.from(document.querySelectorAll('[role="menuitem"]'));
const deleteOption = menuItems.find(item => item.textContent.includes('Delete'));

const menuItems = Array.from(document.querySelectorAll('[role="menuitem"]'));
const deleteOption = menuItems.find(item => item.textContent === 'Delete');
if (deleteOption) {
deleteOption.click();
document.querySelector('[data-testid="confirmationSheetConfirm"]')?.click();
await delay(3000 + Math.random() * 1000);
} else {
const tweetContainer = button.closest('[data-testid="tweet"]');
const unretweetButton = tweetContainer?.querySelector('[data-testid="unretweet"]');
if (unretweetButton) {
unretweetButton.click();
await delay(250 + Math.random() * 250);
document.querySelector('[data-testid="unretweetConfirm"]')?.click();
await delay(3000 + Math.random() * 1000);
}
}
};

if (deleteOption) {
deleteOption.click();
document.querySelector('[data-testid="confirmationSheetConfirm"]')?.click();
await delay(3000);
while (true) {
const deleteButtons = getDeleteButtons().filter(button => !processedButtons.has(button));
if (deleteButtons.length === 0) {
scrollToEnd();
await delay(5000); // Wait for more tweets to load
if (getDeleteButtons().filter(button => !processedButtons.has(button)).length === 0) {
break; // Exit if no new tweets are loaded
} else {
const tweetContainer = button.closest('[data-testid="tweet"]');
const unretweetButton = tweetContainer?.querySelector('[data-testid="unretweet"]');

if (unretweetButton) {
unretweetButton.click();
await delay(250);
document.querySelector('[data-testid="unretweetConfirm"]')?.click();
await delay(3000);
}
continue; // Otherwise, continue processing new tweets
}
}

for (const button of deleteButtons) {
await attemptDelete(button);
}
}

console.log('All tweets deleted successfully!');
};

deleteAllTweets();
deleteAllTweets();