Skip to content

Instantly share code, notes, and snippets.

@oatycreates
Created August 26, 2021 20:35
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save oatycreates/7ebd72fe8cd3c0b719515dff430055a2 to your computer and use it in GitHub Desktop.
Save oatycreates/7ebd72fe8cd3c0b719515dff430055a2 to your computer and use it in GitHub Desktop.
Unfollow All for Twitter
/*
* Unfollow All for Twitter
*
* Go to https://twitter.com/<YourUserName>/following and run the
* below code in the console to gradually unfollow all accounts
* you are following, this should play nice with any rate limiting.
* Built on current Firefox (2021-08-26) though may work for others.
*
* Author: https://twitter.com/OatyCreates
* Copyright 2021-Present @oatycreates
*/
toUnfollow = [];
setInterval(() => {
if (toUnfollow.length === 0) {
// Ran out of data, fetch new
// NOTE - This uses the accessibility label text and will need to be updated for other languages
toUnfollow = Array.from(document.querySelectorAll('[aria-label^="Following"]'));
}
if (toUnfollow.length >= 0) {
// Unfollow one by one
unfollowPerson(toUnfollow.shift());
}
if (toUnfollow.length === 0) {
// Finished data, bring new elements into view
window.scrollTo(0, document.body.scrollHeight); // Scroll to load fresh data over time
}
}, 500); // Load data or unfollow an account every interval
unfollowPerson = (unfollowBtn) => {
unfollowBtn.click();
// There is a follow-up confirmation prompt so click that too
// NOTE - This ID may change, it is trying to find the button element for the unfollow confirmation
unfollowConfirmBtns = document.querySelectorAll('[data-testid="confirmationSheetConfirm"]');
unfollowConfirmBtns.forEach((btn) => btn.click());
};
@oatycreates
Copy link
Author

oatycreates commented Aug 26, 2021

Sometimes the scrolling gets stuck and the script will error several times (e.g. 'unfollowBtn not defined').
Refresh the page and run the script again when this happens. You might also need to increase the interval time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment