Advertisement
Zalati

Unfollow twitter

Jan 16th, 2020
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var LANGUAGE = "FR"; //NOTE: change it to use your language!
  2. var WORDS =
  3. {
  4.     //English language:
  5.     EN:
  6.     {
  7.         followsYouText: "Follows you", //Text that informs that follows you.
  8.         followingButtonText: "Following", //Text of the "Following" button.
  9.         confirmationButtonText: "Unfollow" //Text of the confirmation button. I am not totally sure.
  10.     },
  11.     //French language:
  12.     FR:
  13.     {
  14.         followsYouText: "Vous suit", //Text that informs that follows you.
  15.         followingButtonText: "Abonné", //Text of the "Following" button.
  16.         confirmationButtonText: "Se désabonner" //Text of the confirmation button. I am not totally sure.
  17.     }
  18.     //NOTE: if needed, add your language here...
  19. }
  20. var UNFOLLOW_FOLLOWERS = false; //If set to true, it will also remove followers (unless they are skipped).
  21. var MS_PER_CYCLE = 10; //Milliseconds per cycle (each call to 'performUnfollow').
  22. var MAXIMUM_UNFOLLOW_ACTIONS_PER_CYCLE = null; //Maximum of unfollow actions to perform, per cycle (each call to 'performUnfollow'). Set to 'null' to have no limit.
  23. var MAXIMUM_UNFOLLOW_ACTIONS_TOTAL = null; //Maximum of unfollow actions to perform, in total (among all calls to 'performUnfollow'). Set to 'null' to have no limit.
  24. var SKIP_USERS = //Users that we do not want to unfollow (even if they are not following you back):
  25. [
  26.     //Place the user names that you want to skip here (they will not be unfollowed):
  27.     "user_name_to_skip_example_1",
  28.     "user_name_to_skip_example_2",
  29.     "user_name_to_skip_example_3"
  30. ];
  31. SKIP_USERS.forEach(function(value, index) { SKIP_USERS[index] = value.toLowerCase(); }); //Transforms all the user names to lower case as it will be case insensitive.
  32.  
  33. var _UNFOLLOWED_TOTAL = 0; //Keeps the number of total unfollow actions performed. Read-only (do not modify).
  34.  
  35. //Function that unfollows non-followers on Twitter:
  36. var performUnfollow = function(followsYouText, followingButtonText, confirmationButtonText, unfollowFollowers, maximumUnfollowActionsPerCycle, maximumUnfollowActionsTotal)
  37. {
  38.     var unfollowed = 0;
  39.     followsYouText = followsYouText || WORDS.EN.followsYouText; //Text that informs that follows you.
  40.     followingButtonText = followingButtonText || WORDS.EN.followingButtonText; //Text of the "Following" button.
  41.     confirmationButtonText = confirmationButtonText || WORDS.EN.confirmationButtonText; //Text of the confirmation button.
  42.     unfollowFollowers = typeof(unfollowFollowers) === "undefined" || unfollowFollowers === null ? UNFOLLOW_FOLLOWERS : unfollowFollowers;
  43.     maximumUnfollowActionsTotal = maximumUnfollowActionsTotal === null || !isNaN(parseInt(maximumUnfollowActionsTotal)) ? maximumUnfollowActionsTotal : MAXIMUM_UNFOLLOW_ACTIONS_TOTAL || null;
  44.     maximumUnfollowActionsTotal = !isNaN(parseInt(maximumUnfollowActionsTotal)) ? parseInt(maximumUnfollowActionsTotal) : null;
  45.     maximumUnfollowActionsPerCycle = maximumUnfollowActionsPerCycle === null || !isNaN(parseInt(maximumUnfollowActionsPerCycle)) ? maximumUnfollowActionsPerCycle : MAXIMUM_UNFOLLOW_ACTIONS_PER_CYCLE || null;
  46.     maximumUnfollowActionsPerCycle = !isNaN(parseInt(maximumUnfollowActionsPerCycle)) ? parseInt(maximumUnfollowActionsPerCycle) : null;
  47.    
  48.     //Looks through all the containers of each user:
  49.     var totalLimitReached = false;
  50.     var localLimitReached = false;
  51.     var userContainers = document.querySelectorAll('[data-testid=UserCell]');
  52.     Array.prototype.filter.call
  53.     (
  54.         userContainers,
  55.         function(userContainer)
  56.         {
  57.             //If we have reached a limit previously, exits silently:
  58.             if (totalLimitReached || localLimitReached) { return; }
  59.             //If we have reached the maximum desired number of total unfollow actions, exits:
  60.             else if (maximumUnfollowActionsTotal !== null && _UNFOLLOWED_TOTAL >= maximumUnfollowActionsTotal) { console.log("Exiting! Limit of unfollow actions in total reached: " + maximumUnfollowActionsTotal); totalLimitReached = true; return;  }
  61.             //...otherwise, if we have reached the maximum desired number of local unfollow actions, exits:
  62.             else if (maximumUnfollowActionsPerCycle !== null && unfollowed >= maximumUnfollowActionsPerCycle) { console.log("Exiting! Limit of unfollow actions per cycle reached: " + maximumUnfollowActionsPerCycle); localLimitReached = true; return;  }
  63.            
  64.             //Checks whether the user is following you:
  65.             if (!unfollowFollowers)
  66.             {
  67.                 var followsYou = false;
  68.                 Array.from(userContainer.querySelectorAll("*")).find
  69.                 (
  70.                     function(element)
  71.                     {
  72.                         if (element.textContent === followsYouText) { followsYou = true; }
  73.                     }
  74.                 );
  75.             }
  76.             else { followsYou = false; } //If we want to also unfollow followers, we consider it is not a follower.
  77.  
  78.             //If the user is not following you (or we also want to unfollow followers):
  79.             if (!followsYou)
  80.             {
  81.                 //Finds the user name and checks whether we want to skip this user or not:
  82.                 var skipUser = false;
  83.                 var userName = "";
  84.                 Array.from(userContainer.querySelectorAll("[href^='/']")).find
  85.                 (
  86.                     function (element)
  87.                     {
  88.                         if (skipUser) { return; }
  89.                         if (element.href.indexOf("search?q=") !== -1 || element.href.indexOf("/") === -1) { return; }
  90.                         userName = element.href.substring(element.href.lastIndexOf("/") + 1).toLowerCase();
  91.                         Array.from(element.querySelectorAll("*")).find
  92.                         (
  93.                             function (subElement)
  94.                             {
  95.                                 if (subElement.textContent.toLowerCase() === "@" + userName)
  96.                                 {
  97.                                     if (SKIP_USERS.indexOf(userName) !== -1)
  98.                                     {
  99.                                         console.log("We want to skip: " + userName);
  100.                                         skipUser = true;
  101.                                     }
  102.                                 }
  103.                             }
  104.                         );
  105.                     }
  106.                 );
  107.                
  108.                 //If we do not want to skip the user:
  109.                 if (!skipUser)
  110.                 {
  111.                     //Finds the unfollow button:
  112.                     Array.from(userContainer.querySelectorAll('[role=button]')).find
  113.                     (
  114.                         function(element)
  115.                         {
  116.                             //If the unfollow button is found, clicks it:
  117.                             if (element.textContent === followingButtonText)
  118.                             {
  119.                                 console.log("* Unfollowing: " + userName);
  120.                                 element.click();
  121.                                 unfollowed++;
  122.                                 _UNFOLLOWED_TOTAL++;
  123.                             }
  124.                         }
  125.                     );
  126.                 }
  127.             }
  128.         }
  129.     );
  130.    
  131.     //If there is a confirmation dialog, press it automatically:
  132.     Array.from(document.querySelectorAll('[role=button]')).find //Finds the confirmation button.
  133.     (
  134.         function(element)
  135.         {
  136.             //If the confirmation button is found, clicks it:
  137.             if (element.textContent === confirmationButtonText)
  138.             {
  139.                 element.click();
  140.             }
  141.         }
  142.     );
  143.    
  144.     return totalLimitReached ? null : unfollowed; //If the total limit has been reached, returns null. Otherwise, returns the number of unfollowed people.
  145. }
  146.  
  147.  
  148. //Scrolls and unfollows non-followers, constantly:
  149. var scrollAndUnfollow = function()
  150. {
  151.     window.scrollTo(0, document.body.scrollHeight);
  152.     var unfollowed = performUnfollow(WORDS[LANGUAGE].followsYouText, WORDS[LANGUAGE].followingButtonText, WORDS[LANGUAGE].confirmationButtonText, UNFOLLOW_FOLLOWERS, MAXIMUM_UNFOLLOW_ACTIONS_PER_CYCLE, MAXIMUM_UNFOLLOW_ACTIONS_TOTAL); //For English, you can try to call it without parameters.
  153.     if (unfollowed !== null) { setTimeout(scrollAndUnfollow, MS_PER_CYCLE); }
  154.     else { console.log("Total desire of unfollow actions performed!"); }
  155. };
  156. scrollAndUnfollow();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement