Skip to content

Instantly share code, notes, and snippets.

@Acesmndr
Last active February 3, 2024 20:47
Show Gist options
  • Star 20 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save Acesmndr/eb9fdc42b8fed0416ee658af692adb69 to your computer and use it in GitHub Desktop.
Save Acesmndr/eb9fdc42b8fed0416ee658af692adb69 to your computer and use it in GitHub Desktop.
Get Instagram Unfollowers: Get a list of people whom you follow but who do not follow you back on Instagram as well as the list of people who follow you but whom you don't follow back!
// Gist by acesmndr@gmail.com Rev 2.1.2 May 15, 2019
// go to your insta profile page (not the home page). It looks something like this: https://instagram.com/YOUR_PROFILE
// Open javascript console "Ctrl + Shift + i" in Windows/Linux and "Cmd + option + i" in Mac.
// Paste the entire code below to the console and hit enter
// You will get the list of people whom you follow but who don't follow you back and viceversa as well as the entire list of followers and following
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.clear();
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.clear();
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.clear();
console.debug(`Please wait...`);
scrollFollowing();
}, 2000);
}
function scrollFollowers(){
var scrollDiv = document.querySelector('div[role="presentation"]').querySelector('li').parentElement;
scrollDiv.lastElementChild.scrollIntoView();
console.clear();
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.clear();
console.debug(`Please wait while the following list is loaded...`);
setTimeout(function(){
console.clear();
console.info(`Scroll Followers completed with ${followersCount} people`);
console.debug(`Please wait...`);
scrollingCount = 0;
openFollowingDialog();
}, 3000);
} else {
scrollingCount = scrollDiv.childElementCount; // current count of loaded followers
scrollFollowers();
}
},1000);
}
function scrollFollowing() {
var scrollDiv = document.querySelector('div[role="presentation"]').querySelector('li').parentElement;
scrollDiv.lastElementChild.scrollIntoView();
console.clear();
console.debug(`Loaded ${scrollDiv.childElementCount} out of ${followingCount} following people. Please wait...`);
setTimeout(function () {
if (scrollDiv.childElementCount === followingCount || (scrollingCount === scrollDiv.childElementCount && (followingCount - scrollingCount) < hashtagCount)) {
scrolledFollowingCount = scrollDiv.childElementCount;
getFollowing();
console.info(`Scroll following completed with ${followingCount} people`);
setTimeout(function () {
document.querySelector('[aria-label="Close"]').click();
getResults();
}, 1000);
} else {
scrollingCount = scrollDiv.childElementCount; // current count of loaded following people
scrollFollowing();
}
}, 1000);
}
function getFollowers(){
var followersDiv = document.querySelector('div[role="presentation"]').querySelector('li').parentElement.children;
for (var i = 0;i<followersDiv.length;i++){
var tempUser = followersDiv[i].lastElementChild.children[0].children[1];
followers.push({
userID: tempUser.children[0].textContent,
userName: tempUser.children[1].textContent,
});
}
document.querySelector('[aria-label="Close"]').click();
}
function getFollowing(){
var followersDiv = document.querySelector('div[role="presentation"]').querySelector('li').parentElement.children;
for (var i = 0;i<followersDiv.length;i++){
var tempUser = followersDiv[i].lastElementChild.children[0].children[1];
following.push({
userID: tempUser.children[0].textContent,
userName: tempUser.children[1].textContent,
});
}
}
function getResults(){
console.clear();
var consoleColors = 'color:#bada55;background:black;font-weight:bold;font-size:20px';
console.info('The results might shock you! Keep calm!');
console.log('Your following list:', following);
console.log('Your followers list:', followers);
var unfollowing = following.filter(diff(followers));
console.log(`%c There are ${unfollowing.length} people who you are following but who don\'t follow you. They are:`, consoleColors);
console.table(unfollowing);
var unfollowers = followers.filter(diff(following));
console.log(`%c There are ${unfollowers.length} people who are following you but who you aren\'t following. They are:`, consoleColors);
console.table(unfollowers);
console.info(`You have loaded ${scrolledFollowersCount} out of ${followersCount} followers and ${scrolledFollowingCount} out of ${followingCount} of the people you follow.`);
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'`);
console.info(`%c For a social cause: acesmndr@gmail.com`, consoleColors);
}
var getInstagramStats = function(htagCount) {
if (/(http\:|https\:)\/\/(www.)instagram.com\//.test(location.href)) {
hashtagCount = htagCount + 1;
openFollowersDialog();
} else {
console.clear();
console.error('You should be in your instagram profile in order to execute the script');
}
}
getInstagramStats(10);
@Schludi
Copy link

Schludi commented Mar 12, 2018

Hi! I just tried it but get the following error message:
VM48:12 Uncaught TypeError: Cannot read property 'querySelector' of undefined
at goToSubscribers (:12:67)
at :83:1

@Acesmndr
Copy link
Author

Acesmndr commented Jan 2, 2019

@Schludi Since the last implementation was totally dependent on the classname of the tags present in the instagram site it got outdated when Instagram updated their site. I have updated the script to work irrespective of the tags now.

It's working now but the usernames are redacted as they are from my list.
screen shot 2019-01-02 at 5 19 58 pm

P.S. Sorry for the late reply. I don't remember getting notification for your comment on my gist. It must be due to my habit of doing "Mark all as read".

@sticks-stuff
Copy link

All the
document.querySelector('[aria-label="Close"]').click();
should be changed to
document.querySelector('[aria-label="Close"]').parentElement.parentElement.click();

@crypt096
Copy link

Not working, just tested it.

@tsvetkovv
Copy link

updated:

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(){
  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(`Please wait while the following list is loaded...`);
      setTimeout(function(){
        
        console.info(`Scroll Followers completed with ${followersCount} people`);
        console.debug(`Please wait...`);
        scrollingCount = 0;
        openFollowingDialog();
      }, 3000);
    } else {
      scrollingCount = scrollDiv.childElementCount; // current count of loaded followers
      scrollFollowers();
    }
  },1000);
}

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(`Loaded ${scrollDiv.childElementCount} out of ${followingCount} following people. Please wait...`);
  setTimeout(function () {
    if (scrollDiv.childElementCount === followingCount || (scrollingCount === scrollDiv.childElementCount && (followingCount - scrollingCount) < hashtagCount)) {
      scrolledFollowingCount = scrollDiv.childElementCount;
      getFollowing();
      console.info(`Scroll following completed with ${followingCount} people`);
      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[1];
    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[1];
    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('The results might shock you! Keep calm!');
  console.log('Your following list:', following);
  console.log('Your followers list:', followers);
  var unfollowing = following.filter(diff(followers));
  console.log(`%c There are ${unfollowing.length} people who you are following but who don\'t follow you. They are:`, consoleColors);
  console.table(unfollowing);
  var unfollowers = followers.filter(diff(following));
  console.log(`%c There are ${unfollowers.length} people who are following you but who you aren\'t following. They are:`, consoleColors);
  console.table(unfollowers);
  console.info(`You have loaded ${scrolledFollowersCount} out of ${followersCount} followers and ${scrolledFollowingCount} out of ${followingCount} of the people you follow.`);
  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('You should be in your instagram profile in order to execute the script');
  }
}
getInstagramStats(10);

@Dhee2211
Copy link

Dhee2211 commented Mar 2, 2023

getting this error "Cannot read properties of null (reading 'scrollIntoView')"

@srsivaprasad
Copy link

srsivaprasad commented Mar 19, 2023

Thank you for the code but i am getting an error at the end. Please see the screenshot

image

Thank you
.

@sticks-stuff
Copy link

Updated (until IG breaks it again)

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(){
  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(`Please wait while the following list is loaded...`);
      setTimeout(function(){
        
        console.info(`Scroll Followers completed with ${followersCount} people`);
        console.debug(`Please wait...`);
        scrollingCount = 0;
        openFollowingDialog();
      }, 3000);
    } else {
      scrollingCount = scrollDiv.childElementCount; // current count of loaded followers
      scrollFollowers();
    }
  },1000);
}

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(`Loaded ${scrollDiv.childElementCount} out of ${followingCount} following people. Please wait...`);
  setTimeout(function () {
    if (scrollDiv.childElementCount === followingCount || (scrollingCount === scrollDiv.childElementCount && (followingCount - scrollingCount) < hashtagCount)) {
      scrolledFollowingCount = scrollDiv.childElementCount;
      getFollowing();
      console.info(`Scroll following completed with ${followingCount} people`);
      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('The results might shock you! Keep calm!');
  console.log('Your following list:', following);
  console.log('Your followers list:', followers);
  var unfollowing = following.filter(diff(followers));
  console.log(`%c There are ${unfollowing.length} people who you are following but who don\'t follow you. They are:`, consoleColors);
  console.table(unfollowing);
  var unfollowers = followers.filter(diff(following));
  console.log(`%c There are ${unfollowers.length} people who are following you but who you aren\'t following. They are:`, consoleColors);
  console.table(unfollowers);
  console.info(`You have loaded ${scrolledFollowersCount} out of ${followersCount} followers and ${scrolledFollowingCount} out of ${followingCount} of the people you follow.`);
  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('You should be in your instagram profile in order to execute the script');
  }
}
getInstagramStats(10);

@sanasiddiqee
Copy link

hey how should i use this

@red-ruby-quay
Copy link

How to use it (27-04-2023, still worked):

  1. Login to your instagram account (via web), go to Profile tab
  2. Click the Followers text, then open Console tab of your any web browser (google it if you do not understand this dev-tools)
  3. Copy the code to the Console textarea and hit enter; Or run the .js file by download the javascript code, save the file (.js ext.) and open with your web browser that open the instagram Profile tab menu
  4. Wait for the result, let the code's running until finish.
  5. Run one of the functions provided on the .js code, for example: type getResults() or getInstagramStats(10) in your Console textarea, then hit enter to get the result
  6. Good luck! The latest code you would run on is the latest, the most bottom one in this comment section (look at the comment timestamp)

@khokonm
Copy link

khokonm commented Jul 1, 2023

great work, works like charm!
one tip: you may add some random wait time in between scrolls, just to make it a lot more like human interaction.

@soul-808
Copy link

soul-808 commented Jul 21, 2023

Line 30 and 40 breaks if you have more than 1k followers

https://gist.github.com/Acesmndr/eb9fdc42b8fed0416ee658af692adb69#file-instagramunfollowers-js-L30
https://gist.github.com/Acesmndr/eb9fdc42b8fed0416ee658af692adb69#file-instagramunfollowers-js-L40

Ex: for line 40
it should be parseInt(document.querySelector('a[href*="/followers/"]').firstElementChild.textContent.replace(',', ''))

Add .replace(',', '') in line 30 the same way.

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