Skip to content

Instantly share code, notes, and snippets.

@wilcollins
Created December 14, 2024 16:28
Show Gist options
  • Select an option

  • Save wilcollins/ca18ebe86e7e7bbe08bd6160886087ef to your computer and use it in GitHub Desktop.

Select an option

Save wilcollins/ca18ebe86e7e7bbe08bd6160886087ef to your computer and use it in GitHub Desktop.
simple script to unfollow everyone on twitter/x.com
(async () => {
// Helper function to introduce delays
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));
// Helper function to wait for an element
const waitForElement = async (selector, timeout = 5000) => {
const startTime = Date.now();
while (Date.now() - startTime < timeout) {
const element = document.querySelector(selector);
if (element) return element;
await delay(100); // Check every 100ms
}
throw new Error(`Element with selector "${selector}" not found within ${timeout}ms`);
};
console.log("Starting the unfollow process...");
while (true) {
// Select all buttons where the `data-testid` ends with "-unfollow"
let unfollowButtons = Array.from(document.querySelectorAll('button[data-testid$="-unfollow"]'));
if (unfollowButtons.length === 0) {
console.log("No more accounts to unfollow on the current screen. Scrolling down...");
window.scrollBy(0, 1000); // Scroll down slightly to load more accounts
await delay(2000); // Allow time for new accounts to load
continue;
}
console.log(`Found ${unfollowButtons.length} accounts to unfollow.`);
// Loop through each button and click it
for (let i = 0; i < unfollowButtons.length; i++) {
try {
// Click the "Unfollow" button
unfollowButtons[i].click();
// Wait for the confirmation modal to appear
const confirmButton = await waitForElement('button[data-testid="confirmationSheetConfirm"]');
confirmButton.click();
console.log(`Unfollowed ${i + 1} of ${unfollowButtons.length}`);
// Sleep for 2 seconds after each unfollow action to mimic natural behavior
await delay(2000);
// Scroll down slightly to ensure smooth page updates
window.scrollBy(0, 100); // Scroll down a little to load new elements dynamically
} catch (error) {
console.error(`Error unfollowing account ${i + 1}:`, error);
}
}
// Update unfollow buttons in case of dynamic DOM changes
await delay(2000); // Allow time for additional elements to load
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment