Skip to content

Instantly share code, notes, and snippets.

@pankaj28843
Last active November 22, 2024 06:44
Show Gist options
  • Save pankaj28843/df69413be464756c63506bbaed97dde2 to your computer and use it in GitHub Desktop.
Save pankaj28843/df69413be464756c63506bbaed97dde2 to your computer and use it in GitHub Desktop.
Automate Unfollowing on LinkedIn: Script for Followers and Following
  1. To unfollow new connections (followers):

    • Go to: LinkedIn Followers
    • Open Dev Tools > Console (Cmd+Option+J or Ctrl+Shift+J).
    • Paste the script below into the console and press enter.
  2. To unfollow people you are explicitly following:

    • Go to: LinkedIn Following
    • Open Dev Tools > Console (Cmd+Option+J or Ctrl+Shift+J).
    • Paste the script below into the console and press enter.

Code

(() => {
  let count = 0;

  // Function to get all buttons matching the aria-label pattern
  function getAllButtons() {
    return Array.from(
      document.querySelectorAll('button[aria-label^="Click to stop following"]')
    );
  }

  // Function to handle the unfollow process
  async function unfollowAll() {
    const buttons = getAllButtons();

    for (let button of buttons) {
      count += 1;

      // Extract name if available for logging purposes
      const name = button.closest('div')?.querySelector(
        '.follows-recommendation-card__name'
      )?.innerText || 'Unknown';

      console.log(`Unfollow #${count}:`, name);

      // Scroll to the button and click it
      window.scrollTo(0, button.offsetTop - 260);
      button.click();

      // Wait for the modal to appear
      await new Promise((resolve) => setTimeout(resolve, 500));

      // Find and click the 'Unfollow' button in the modal
      const unfollowButton = Array.from(document.querySelectorAll('button')).find(
        (btn) => btn.innerText.trim() === 'Unfollow'
      );

      if (unfollowButton) {
        unfollowButton.click();
      } else {
        console.warn(`Unfollow button not found for #${count}:`, name);
      }

      // Delay between actions to avoid overwhelming the website
      await new Promise((resolve) => setTimeout(resolve, 1000));
    }
  }

  // Main function to iterate over the buttons until all are processed
  async function run() {
    await unfollowAll();
    window.scrollTo(0, document.body.scrollHeight);

    // Wait and recheck for new buttons (if dynamically loaded)
    await new Promise((resolve) => setTimeout(resolve, 2000));

    const buttons = getAllButtons();
    if (buttons.length) {
      console.log('More buttons detected, continuing...');
      run();
    } else {
      console.log('No more buttons to process.');
    }
  }

  // Start the script
  run();
})();

Notes

  • This script handles dynamically loaded buttons as well.
  • Ensure you have a stable internet connection while running the script.
  • Use responsibly.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment