Skip to content

Instantly share code, notes, and snippets.

@lovkyndig
Last active October 7, 2024 05:52
Show Gist options
  • Save lovkyndig/ddcceb90ac68a6cd7ce32dbaf5f1838f to your computer and use it in GitHub Desktop.
Save lovkyndig/ddcceb90ac68a6cd7ce32dbaf5f1838f to your computer and use it in GitHub Desktop.
Unfollow all on Facebook

"Unfollow all on facebook"-Gist does these three things

  1. Explains how to unfollow every friends (automatically) on facebook with Chrome, but do not explain how to use the console (developer tools) on other browsers or mobile phones.
  2. Give you a script that scroll down to the bottom of your friends list (which is required before you run the unfollow script).
  3. Give you a script that unfollow all of your friends on facebook.

I. Intnroduction - Start with this

The following script only works if you are in the friends list displayed in the main window. It doesn't work if you see your friends in the left sidebar.1

(1) Make sure you are at this URL:

https://www.facebook.com/username-or-facebook-number/friends

Change "username-or-facebook-number" with your own username or facebook-number.

(2) allow pasting

Execute the following line in the console:

allow pasting

Ignore the respond on this code.

You will now be able to paste the following code-snippets into the console.

II. The "scroll down"-script

This "scroll down script" can be used on many pages, not just on the friends page.

(function() { 
  'use strict'; 
  var lastScrollHeight = 0; 
  function autoScroll() { 
    var sh = document.documentElement.scrollHeight; 
    if (sh != lastScrollHeight) { 
      lastScrollHeight = sh; 
      document.documentElement.scrollTop = sh; 
    } 
  } 
  window.setInterval(autoScroll, 100); 
}) ();
console.log("THE SCROLLING JOB IS JUST STARTED!",
  " - - - W A I T  A  L O N G  T I M E - - -",
  " - - - ",
  "DO NOT PASTE THE UNFOLLOW SCRIPT INTO THE CONSOLE - ",
  "BEFORE THE BROWSER ARE FINISH WITH SCROLLING DOWN TO THE BOTTOM. ",
  "IT CAN TAKE A WHILE.")

III. Unfollow-script

NB! Don't past the script and execute it before you have scrolled down to the bottom of your friends list.

// START OF UNFOLLOWING SCRIPT

// Variables who is needed in the unfollow-function below
let friends = document.querySelectorAll('div[aria-label="Friends"]'); // three dots menu
let delay = 500;  // Delay between clicks to prevent being blocked by facebook
let i = 0;  // Set this value to 0 or another value where you want it to start
let un = "not unfollowed"; // Initialize the value of unfollowed
let timeOut = 3000; // Setting timeout for the first friend (else it isn't working)

console.log("STARTING THE UNFOLLOW SCRIPT (in 3 seconds)...",
  "- NB!",
  "THE JOB CAN TAKE MORE THAN ONE HOUR,",
  "IF YOU HAVE THOUSANDS OF FRIENDS.")

function unfollow() {
  // Cancel and stop the function when the work is done.
  if (i == friends.length) {
    console.log("FINISH WORKING! ",
      "- ALL POSSIBLY FRIENDS ARE NOW UNFOLLOWED.")
    return;
  }

  // Clicking on each friends with the following delay before the next click.
  if (i > 0) { 
    timeOut = Math.floor(500 + Math.random() * delay * 2);
  }

  // Click on friends open another menu with menuitems (the work is done below)
  friends[i].click(); 
  
  // Start of sub-script that click the unfollow button

  setTimeout(()=> {
    // The querySelector give the four menuitems (that opens after the click above)
    let menuItems = document.querySelectorAll('*[class^="__fb"] > div > div[tabindex] > div[class] > div[role="menu"] > div > div > div[aria-hidden] > div > div > div > div[role="menuitem"]');
    let itemsLength = menuItems.length
    let itemGoal = menuItems[itemsLength-2]
    // Calculate remaining time based on the num. of friends not yet unfollowed
    let remaining = ((friends.length - i) * (delay+500) / 1000).toFixed(0);
  
    // Unfollow the friend if possibly
    if (itemGoal.textContent.includes("Unfollow")) {
      un = "unfollowed";
      itemGoal.click(); 
    } else un = "not unfollowed";

    // Ends of subscript that selects the unfollow button and clicks it

    // The following code is about printing and the friend-list.

    console.log(
      'Friend ', i + 1, 'of ', friends.length + ' is ' + un + '. ~ ', remaining + 's ' + 'remaining…'
    );

    i = i + 1;  // Increment the counter of friends

    // Recursively call itself with a randomized delay of 500-1000ms
    unfollow()

    }, timeOut)

}

// run the unfollow function
unfollow();

// END OF UNFOLLOW SCRIPT

Screenshot

Here's a screenshot of what you'll see (after a while), in the console, when you run the "unfollow-script":

image

Source

The source (in the link below), the unfollowing function, didn't work in 2024. Therefore the new script above. https://gist.github.com/renestalder/c5b77635bfbec8f94d28?permalink_comment_id=3924885

Footnotes

  1. You can reprogram the script. There may only be two variables you need to change. The first one is the "friends"-variable in line 4. Change "Friends" to "More". The next one is probably "menuItems". I'm not sure. It's a lot of work, if you need to change it.

@lovkyndig
Copy link
Author

lovkyndig commented Oct 5, 2024

Facebook temporarily blocked the last script from running, after the "unfollow script" unfollowed more than 2,500 friends.
Youre Temporarily Blocked

How do I run the script again after the temporary block is over?

Answer:

Change the value of the variable i in line 6 to the number where you want it to start unfollowing next time. Example:

// Original line 4:
let i = 0;  // Set this value to 0 or another value where you want it to start
// Example of new line 4:
let i = 2500;  // Starts with this value the next time you run "Unfollow script"

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