Facebook
From Onur Özkır, 3 Years ago, written in JavaScript.
This paste is a reply to Untitled from Unique Panda - view diff
Embed
Download Paste or View Raw
Hits: 348
  1.  
  2. var followButtonInnerHTML = {
  3.   'en' : 'Following',
  4.   'tr' : 'Takiptesin'
  5. }
  6.  
  7.  
  8. openFollowersWindow().then(function () {
  9.  
  10.     populateUnfollowsPool();
  11.  
  12.     digestUnfollowsPool();
  13.  
  14. });
  15.  
  16.  
  17. function openFollowersWindow() {
  18.  
  19.     var onFollowersWindowWasOpenedListeners = [];
  20.  
  21.     var openWindowTimeout = 3000;
  22.  
  23.  
  24.     var followersElement = getFollowersElement();
  25.  
  26.  
  27.     followersElement.click();
  28.  
  29.  
  30.  
  31.     function digestOnFollowersWindowWasOpenedListeners() {
  32.  
  33.         onFollowersWindowWasOpenedListeners.forEach(function (onFollowersWindowWasOpenedListener) {
  34.  
  35.             onFollowersWindowWasOpenedListener();
  36.  
  37.         });
  38.  
  39.     }
  40.  
  41.     var wasOpened;
  42.  
  43.     setTimeout(function () {
  44.  
  45.         // TODO Verify that the window was indeed opened
  46.  
  47.         wasOpened = true;
  48.  
  49.         digestOnFollowersWindowWasOpenedListeners();
  50.  
  51.     }, openWindowTimeout);
  52.  
  53.     return {
  54.  
  55.         then: function (onFollowersWindowWasOpened) {
  56.  
  57.             if (wasOpened) {
  58.  
  59.                 onFollowersWindowWasOpened();
  60.  
  61.             } else {
  62.  
  63.                 onFollowersWindowWasOpenedListeners.push(onFollowersWindowWasOpened);
  64.  
  65.             }
  66.  
  67.         }
  68.  
  69.     };
  70.  
  71. }
  72.  
  73. function getFollowersElement() {
  74.  
  75.     return getFollowersElementWithUsername(getUsername());
  76.  
  77. }
  78.  
  79. function getUsername() {
  80.  
  81.     var pageTitleElement = document.getElementsByTagName('h1')[0];
  82.  
  83.     if (!pageTitleElement) throw new Error('No title to get username from');
  84.  
  85.     return pageTitleElement.innerHTML;
  86.  
  87. }
  88.  
  89. function getFollowersElementWithUsername(username) {
  90.  
  91.     var followersElement = document.getElementsByClassName('-nal3 ')[0];
  92.  
  93.     if (!followersElement) throw new Error('No followers element was found');
  94.  
  95.     return followersElement;
  96.  
  97. }
  98.  
  99. var unfollowsPool;
  100.  
  101. function populateUnfollowsPool() {
  102.     var buttons = document.getElementsByClassName('isgrP')[0].querySelectorAll('button');    
  103.  
  104.     unfollowsPool = [];
  105.  
  106.     for (var i = 0; i < buttons.length; i++) {
  107.  
  108.         var button = buttons[i];
  109.  
  110.         if (button.innerHTML.includes(followButtonInnerHTML[window.navigator.language || 'en'])) {
  111.  
  112.             var randomTimeoutForUnfollow = Math.floor((Math.random() * 10) + 1) * 1000;
  113.  
  114.             console.log('Following button!');
  115.  
  116.             var unfollow = {
  117.  
  118.                 buttonElement: button,
  119.  
  120.                 timeout: randomTimeoutForUnfollow
  121.  
  122.             };
  123.  
  124.             unfollowsPool.push(unfollow);
  125.  
  126.         }
  127.     }
  128.  
  129. }
  130.  
  131. function digestUnfollowsPool() {
  132.  
  133.     if (unfollowsPool.length === 0) {
  134.  
  135.         console.log('Unfollow pool empty, repopulating');
  136.  
  137.         populateUnfollowsPool();
  138.  
  139.     }
  140.  
  141.     var unfollow = unfollowsPool.shift();
  142.  
  143.     var unfollowTimeout = unfollowTimeout.timeout;
  144.  
  145.     console.log('Clicking unfollow button in ', unfollowTimeout);
  146.  
  147.     setTimeout(function () {
  148.  
  149.         var unfollowButtonElement = unfollow.buttonElement;
  150.  
  151.         unfollowButtonElement.scrollIntoView(true);
  152.    
  153.         unfollowButtonElement.click();
  154.      
  155.         var stopFollowingButton = document.getElementsByClassName('aOOlW -Cab_   ')[0];
  156.      
  157.         setTimeout(function(){
  158.                 if(stopFollowingButton) {
  159.                         stopFollowingButton.click();
  160.                 }
  161.      
  162.                 setTimeout(function(){
  163.                     digestUnfollowsPool();
  164.                 },unfollowTimeout / 2)
  165.              
  166.         },unfollowTimeout / 2)
  167.  
  168.  }, unfollowTimeout);
  169.  
  170. }