Skip to content

Instantly share code, notes, and snippets.

@glsignal
Last active November 10, 2024 15:54
Show Gist options
  • Select an option

  • Save glsignal/d86c34ff0dab239f8e9925971f69c170 to your computer and use it in GitHub Desktop.

Select an option

Save glsignal/d86c34ff0dab239f8e9925971f69c170 to your computer and use it in GitHub Desktop.
twitter-unfollow.js
function asyncSleep(timeout) {
return new Promise(function (resolve) {
setTimeout(function () {
resolve();
}, timeout + Math.random() * 300 /* jitter */);
});
}
async function removeFollows() {
console.log("Looking for targets");
const targets = Array.from(
$x('//div[@aria-label="Timeline: Following"]//span[text()="Following"]')
);
if (targets.length > 0) {
console.log(`Found ${targets.length} targets`, { targets });
} else {
console.log("No targets: exiting");
return;
}
for (let target of targets) {
target.click();
await asyncSleep(300);
const unfollow = $x('//span[text()="Unfollow"]')[0];
unfollow.click();
await asyncSleep(1000);
}
removeFollows();
}
async function removeFollowers() {
console.log("Looking for targets");
// filter by "Following" text
const targets = Array.from(
$x(
'//div[@aria-label="Timeline: Followers"]//div[@data-testid="userFollowIndicator"]//ancestor-or-self::button[@data-testid="UserCell"]//button[@aria-label="More"]'
)
);
if (targets.length > 0) {
console.log(`Found ${targets.length} targets`, { targets });
} else {
console.log("No targets: exiting");
return;
}
for (let target of targets) {
target.scrollIntoView();
target.click();
await asyncSleep(300);
const removeFollower = $x(
'//div[@role="menuitem"]//span[text()="Remove this follower"]'
)[0];
removeFollower.click();
await asyncSleep(300);
const confirmation = $x(
'//div[@role="alertdialog"]//button//.[text()="Remove"]'
)[0];
confirmation.click();
await asyncSleep(1000);
}
removeFollowers();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment