Untitled

 avatar
unknown
javascript
2 years ago
6.1 kB
5
Indexable
var following = [],
  followers = [],
  followersCount = 0,
  followingCount = 0,
  scrolledFollowersCount = 0,
  scrolledFollowingCount = 0,
  hashtagCount,
  scrollingCount = 0; // temp variable to store the count for following and followers while scrolling

function diff(array) {
  return function (current) {
    return array.filter(function (element) {
      return element.userId === current.userId && element.userName == current.userName;
    }).length == 0;
  }
}

function openFollowersDialog(){
  if (!document.querySelector('a[href*="/followers/"]')) {
    
    console.error('You are most likely in the homepage and not the profile page. It is the page with your photos only.');
    return;
  }
  followersCount = parseInt(document.querySelector('a[href*="/followers/"]').firstElementChild.textContent);
  document.querySelector('a[href*="/followers/"]').click() // go to Followers
  setTimeout(function(){
    
    console.debug(`Please wait...`);
    scrollFollowers();
  },1000);
}

function openFollowingDialog() {
  followingCount = parseInt(document.querySelector('a[href*="/following/"]').firstElementChild.textContent);
  document.querySelector('a[href*="/following/"]').click(); // go to followers
  setTimeout(function () {
    
    console.debug(`Please wait...`);
    scrollFollowing();
  }, 2000);
}

function scrollFollowers(){
  try {
    var scrollDiv = document.querySelector('div[role="dialog"] div[role="dialog"] > div > div > div:last-child > div:first-child > div');
    scrollDiv.lastElementChild.scrollIntoView();
  
    console.debug(`Loaded ${scrollDiv.childElementCount} out of ${followersCount} followers. Please wait...`);
    setTimeout(function(){
      if (scrollDiv.childElementCount === followersCount || (scrollingCount === scrollDiv.childElementCount && (followersCount - scrollingCount) < hashtagCount)) {
        scrolledFollowersCount = scrollDiv.childElementCount;
        getFollowers();
        
        console.debug(`Perimenete...`);
        setTimeout(function(){
          
          console.info(`Vrikame sinolika ${followersCount} accounts`);
          console.debug(`Perimene Vasia...`);
          scrollingCount = 0;
          openFollowingDialog();
        }, 3000);
      } else {
        scrollingCount = scrollDiv.childElementCount; // current count of loaded followers
        scrollFollowers();
      }
    },1000);
  } catch {
    
  }
 
}

function scrollFollowing() {
  var scrollDiv = document.querySelector('div[role="dialog"] div[role="dialog"] > div > div > div:last-child > div:first-child > div');
  scrollDiv.lastElementChild.scrollIntoView();
  
  console.debug(`Fortose ${scrollDiv.childElementCount} apo ${followingCount} atoma. Araxe kai perimene...`);
  setTimeout(function () {
    if (scrollDiv.childElementCount === followingCount || (scrollingCount === scrollDiv.childElementCount && (followingCount - scrollingCount) < hashtagCount)) {
      scrolledFollowingCount = scrollDiv.childElementCount;
      getFollowing();
      console.info(`Oloklirothike, fortose ${followingCount} accounts`);
      setTimeout(function () {
        document.querySelector('[aria-label="Close"]').parentElement.parentElement.click();
        getResults();
      }, 1000);
    } else {
      scrollingCount = scrollDiv.childElementCount; // current count of loaded following people
      scrollFollowing();
    }
  }, 1000);
}

function getFollowers(){
  var scrollDiv = document.querySelector('div[role="dialog"] div[role="dialog"] > div > div > div:last-child > div:first-child > div');
  var followersDiv = scrollDiv.children;
  for (var i = 0;i<followersDiv.length;i++){
    var tempUser = followersDiv[i].children[0].children[0].children[0].children[1].children[0].children[0];
    followers.push({
      userID: tempUser.children[0]?.textContent,
      userName: tempUser.children[1]?.textContent,
      url: `https://www.instagram.com/${tempUser.children[0]?.textContent}`
    });
  }
  document.querySelector('[aria-label="Close"]').parentElement.parentElement.click();
}

function getFollowing(){
  var scrollDiv = document.querySelector('div[role="dialog"] div[role="dialog"] > div > div > div:last-child > div:first-child > div');
  var followersDiv = scrollDiv.children;
  for (var i = 0;i<followersDiv.length;i++){
    var tempUser = followersDiv[i].children[0].children[0].children[0].children[1].children[0].children[0];
    following.push({
      userID: tempUser.children[0]?.textContent,
      userName: tempUser.children[1]?.textContent,
      url: `https://www.instagram.com/${tempUser.children[0]?.textContent}`
    });
  }
}

function getResults() {
  var consoleColors = 'color:#bada55;background:black;font-weight:bold;font-size:20px';
  console.info('Etoimasou gia ta apotelesmata!');
  console.log('Akoloutheis:', following);
  console.log('Se akolouthoun:', followers);
  var unfollowing = following.filter(diff(followers));
  console.log(`%c Entopisame ${unfollowing.length} mounopana pou de se kanoun follow back:`, consoleColors);
  console.table(unfollowing);
  var unfollowers = followers.filter(diff(following));
  console.log(`%c Entopisame ${unfollowers.length} koroida pou se akolouthoun enw esi oxi:`, consoleColors);
  console.table(unfollowers);
  console.info(`Fortosan ${scrolledFollowersCount} apo ${followersCount} followers kai ${scrolledFollowingCount} apo ${followingCount} atoma pou akoloutheis.`);
  console.info('If the above numbers differ by a huge amount then you need to run the script again. Don\'t switch to another page while the script is running');
  console.info(`There are hashtags in people you follow. It's based on the assumption that you follow hashtags below 10. If you follow somewhere above 10. then run 'getInstagramStats(20) if you follow 20 hashtags'`);
}

var getInstagramStats = function(htagCount) {
  if (/(http\:|https\:)\/\/(www.)instagram.com\//.test(location.href)) {
    hashtagCount = htagCount + 1;
    openFollowersDialog();
  } else {
    
    console.error('Prepei na mpeis sto profil sou gia na to trexeis');
  }
}
getInstagramStats(10);
Editor is loading...