Skip to content

Instantly share code, notes, and snippets.

@eznix86
Forked from darraghoriordan/unfollow.js
Last active January 7, 2025 17:29
Show Gist options
  • Save eznix86/bc8acaa8f2bb4fd78da9ef40e661c47c to your computer and use it in GitHub Desktop.
Save eznix86/bc8acaa8f2bb4fd78da9ef40e661c47c to your computer and use it in GitHub Desktop.
Unfollow everyone on twitter.com who doesn't follow you
// Unfollow everyone on twitter who doesn't follow you.
// based on:
// Unfollow everyone on twitter.com, by Jamie Mason (https://twitter.com/fold_left)
// https://gist.github.com/JamieMason/7580315
//
// 1. Go to https://twitter.com/YOUR_USER_NAME/following
// 2. Open the Developer Console. (COMMAND+ALT+I on Mac)
// 3. Paste this into the Developer Console and run it
//
//
(() => {
const $followButtons = '[data-testid$="-unfollow"]';
const $confirmButton = '[data-testid="confirmationSheetConfirm"]';
const retry = {
count: 0,
limit: 3,
};
const scrollToTheBottom = () => window.scrollTo(0, document.body.scrollHeight);
const retryLimitReached = () => retry.count === retry.limit;
const addNewRetry = () => retry.count++;
const waitForPopup = async () => {
// Wait for the confirmation popup to appear
let confirmButton;
let tries = 0;
while (!confirmButton && tries < 5) {
confirmButton = document.querySelector($confirmButton);
if (!confirmButton) {
tries++;
await new Promise(resolve => setTimeout(resolve, 500)); // Wait for 0.5 seconds
}
}
return confirmButton;
};
const unfollowAll = async (followButtons) => {
console.log(`UNFOLLOWING ${followButtons.length} USERS...`);
for (const followButton of followButtons) {
if (followButton) {
followButton.click();
const confirmButton = await waitForPopup();
if (confirmButton) {
confirmButton.click();
} else {
console.log('Confirmation popup not found, skipping this user.');
}
}
}
};
const nextBatch = async () => {
scrollToTheBottom();
await new Promise(resolve => setTimeout(resolve, 1000)); // Wait for 1 second to allow the page to load more buttons
let followButtons = Array.from(document.querySelectorAll($followButtons));
followButtons = followButtons.filter(b => b.parentElement?.parentElement?.querySelector('[data-testid="userFollowIndicator"]') === null);
const followButtonsWereFound = followButtons.length > 0;
if (followButtonsWereFound) {
await unfollowAll(followButtons);
await new Promise(resolve => setTimeout(resolve, 2000)); // Wait for 2 seconds to allow changes to propagate
return nextBatch();
} else {
addNewRetry();
}
if (retryLimitReached()) {
console.log(`NO ACCOUNTS FOUND, SO I THINK WE'RE DONE`);
console.log(`RELOAD PAGE AND RE-RUN SCRIPT IF ANY WERE MISSED`);
} else {
await new Promise(resolve => setTimeout(resolve, 2000)); // Wait for 2 seconds
return nextBatch();
}
};
nextBatch();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment