Created
October 8, 2021 01:25
-
-
Save mayankagarwal2402/b36e169d97b2b7fbcde5017cfae1ee52 to your computer and use it in GitHub Desktop.
script to help view and manage the list of accounts you follow using the the desktop website.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Usage: | |
// open https://www.instagram.com/ | |
// ensure you are logged in | |
// copy past script in your developer tools console | |
// or create a new saved snippet in the sources tab of developer tools, comes in handy for updates and reuse | |
// to start the script, edit configuration as per your requirement | |
// TO_OPEN_IN_TAB, TO_COPY_ONE_BY_ONE, TO_AUTOMATE, SKIP_FIRST_N | |
// then run the `main()` function in your console | |
// to kill the process in case it is causing an issue or stuck in a loop (if running in automated mode), run following cmd | |
// var timer = setInterval(handler, 2000) | |
var TO_OPEN_IN_TAB = true | |
var TO_COPY_ONE_BY_ONE = true | |
var TO_AUTOMATE = true | |
var SKIP_FIRST_N = 600 | |
var followersCountEl = document.querySelectorAll('span.g47SY')[2] | |
var followersCount = parseInt(followersCountEl.innerText.replaceAll(',', '')) | |
var followersDiv = document.querySelectorAll('div.isgrP')[0] | |
var futileAttempts = 0 | |
var allowedFutileAttempts = 5 | |
var oldCount = document.querySelectorAll('div.wo9IH').length | |
var copiedCount = 0 | |
var timer = null | |
function openInNewTab(url) { | |
window.open(url, '_blank').focus(); | |
} | |
function fallbackCopyTextToClipboard(text) { | |
var textArea = document.createElement("textarea"); | |
textArea.value = text; | |
// Avoid scrolling to bottom | |
textArea.style.top = "0"; | |
textArea.style.left = "0"; | |
textArea.style.position = "fixed"; | |
document.body.appendChild(textArea); | |
textArea.focus(); | |
textArea.select(); | |
try { | |
var successful = document.execCommand('copy'); | |
var msg = successful ? 'successful' : 'unsuccessful'; | |
console.log('Fallback: Copying text command was ' + msg); | |
} catch (err) { | |
console.error('Fallback: Oops, unable to copy', err); | |
} | |
document.body.removeChild(textArea); | |
} | |
async function copyTextToClipboard(text) { | |
if (!navigator.clipboard) { | |
fallbackCopyTextToClipboard(text); | |
return; | |
} | |
return navigator.clipboard.writeText(text) | |
.then(() => { | |
console.log('Copied...'); | |
}) | |
} | |
async function copyTextToClipboardWithRetries(txt) { | |
x = copyTextToClipboard(txt) | |
x.catch(function(err) { | |
console.error('Could not copy...', err) | |
copyTextToClipboardRetry = setTimeout(function() { | |
console.log({txt}) | |
copyTextToClipboardWithRetries(txt) | |
}, 1000) | |
}); | |
} | |
function handler() { | |
followersDiv = document.querySelectorAll('div.isgrP')[0] | |
followersDiv.scrollTop = followersDiv.scrollHeight; | |
var followers = document.querySelectorAll('.wo9IH'); | |
var currCount = followers.length | |
// if (copyOneByOne && currCount && copiedCount <= currCount) { | |
// copyTextToClipboardWithRetries() | |
// } | |
if (currCount > oldCount) { | |
futileAttempts = 0 | |
var diff = currCount - oldCount | |
console.log({currCount, oldCount, diff}) | |
var newFollowers = [...followers].slice(-diff) | |
console.log({newFollowers}) | |
for (let f of newFollowers) { | |
try { | |
var aTags = [...f.querySelectorAll('a.FPmhX')] | |
var link = aTags[0].href | |
console.log(link) | |
if (!SKIP_FIRST_N || currCount > SKIP_FIRST_N) { | |
if (TO_OPEN_IN_TAB) { | |
openInNewTab(link) | |
} | |
} | |
} catch(err) { | |
console.error(err) | |
} | |
} | |
} else { | |
futileAttempts++ | |
} | |
console.log('followers', currCount) | |
oldCount = currCount | |
if (timer && (currCount >= followersCount || futileAttempts >= allowedFutileAttempts || (SKIP_FIRST_N && currCount > SKIP_FIRST_N))) { | |
clearInterval(timer) | |
} | |
} | |
function run() { | |
if (!TO_AUTOMATE) { | |
handler() | |
} else { | |
timer = setInterval(handler, 2000) | |
} | |
} | |
function main() { | |
console.log({followersCount}) | |
if (!followersDiv) { | |
followersCountEl.click() | |
setTimeout(run, 3000) | |
} else { | |
run() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment