(() => {const $followButtons = '[data-testid$="-unfollow"]';
const $confirmButton = '[data-testid="confirmationSheetConfirm"]';

const retry = {
count: 0,
limit: 3,
};

const scrollToTheBottom = () => window.scrollTo(0, document.body.scrollHeight);
const retryLimitReached = () => retry.count === retry.limit;
const addNewRetry = () => retry.count++;

const sleep = ({ seconds }) =>
new Promise((proceed) => {
console.log(`WAITING FOR ${seconds} SECONDS...`);
setTimeout(proceed, seconds * 1000);
});

const unfollowAll = async (followButtons) => {
console.log(`UNFOLLOWING ${followButtons.length} USERS...`);
await Promise.all(
followButtons.map(async (followButton) => {
var elements = followButton.parentNode.parentNode.parentNode.quer ySelectorAll(".css-901oao.css-16my406.r-1qd0xha.r-ad9z0x.r-bcqeeo.r-qvutc0");
var isFollowingMe = false;
for(var i =0; i < elements.length; i++)
if(elements[i].innerText == "Seni takip ediyor")
{
isFollowingMe = true;
break;
}
if(!isFollowingMe){
followButton && followButton.click();
await sleep({ seconds: 1 });
const confirmButton = document.querySelector($confirmButton);
confirmButton && confirmButton.click();
await sleep({ seconds: 2 });
}
else
console.log(followButton.parentNode.parentNode.inn erText);
})
);
};

const nextBatch = async () => {
scrollToTheBottom();
await sleep({ seconds: 1 });

const followButtons = Array.from(document.querySelectorAll($followButton s));
const followButtonsWereFound = followButtons.length > 0;

if (followButtonsWereFound) {
await unfollowAll(followButtons);
await sleep({ seconds: 2 });
return nextBatch();
} else {
addNewRetry();
}

if (retryLimitReached()) {
console.log(`NO ACCOUNTS FOUND, SO I THINK WE'RE DONE`);
console.log(`RELOAD PAGE AND RE-RUN SCRIPT IF ANY WERE MISSED`);
} else {
await sleep({ seconds: 2 });
return nextBatch();
}
};

nextBatch();
})();