Skip to content

Instantly share code, notes, and snippets.

@luispcar
Created May 31, 2021 17:01
Show Gist options
  • Save luispcar/9d4f5fd43d01f5d39b453f03480f367f to your computer and use it in GitHub Desktop.
Save luispcar/9d4f5fd43d01f5d39b453f03480f367f to your computer and use it in GitHub Desktop.
const FOLLOWING_BUTTON_TEXT = 'Following' // CHANGE "FOLLOWING" TO LOCALIZED LANGUAGE AS DISPLAYED ON INSTAGRAM
const UNFOLLOW_BUTTON_TEXT = 'Unfollow' // CHANGE "UNFOLLOW" TO LOCALIZED LANGUAGE AS DISPLAYED ON INSTAGRAM
const MAX_ATTEMPTS_PER_UNFOLLOW = 3 // MAXIMUM # OF ATTEMPTS
const unfollowSomebody = () => {
const followingButton = document
.evaluate(`//button[text()="${FOLLOWING_BUTTON_TEXT}"]`, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null)
.singleNodeValue
if (followingButton) {
console.log('Found following button. Clicking ...')
followingButton.click()
console.log('Clicked following button.')
let unfollowButton = document.evaluate(`//button[text()="${UNFOLLOW_BUTTON_TEXT}"]`, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue
let attempts = 1
while (attempts < MAX_ATTEMPTS_PER_UNFOLLOW && !unfollowButton) {
console.log(`Attempted to find unfollowButton but could not. Retry #${attempts++}`)
unfollowButton = document.evaluate(`//button[text()="${UNFOLLOW_BUTTON_TEXT}"]`, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue
}
if (attempts < MAX_ATTEMPTS_PER_UNFOLLOW) {
console.log('Found unfollow button. Scrolling and clicking ...')
unfollowButton.scrollIntoView(true)
unfollowButton.click()
} else {
console.log(`Retried ${MAX_ATTEMPTS_PER_UNFOLLOW} times and didn't succeed`)
}
return false
}
return true
}
const timeout = (ms) => new Promise(resolve => setTimeout(resolve, ms))
// INCREASE TIMEOUT IF NECESSARY TO AVOID RESTRICTIONS/LIMITS PER DAY
const randomTimeout = () => 1
const unfollowEveryone = async () => {
let shouldStop = false
while (!shouldStop) {
shouldStop = unfollowSomebody()
const unfollowTimeout = randomTimeout()
console.log(`Waiting ${unfollowTimeout} seconds. Should stop: ${shouldStop}.`)
await timeout(unfollowTimeout)
}
console.log('You follow no one.')
}
unfollowEveryone()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment