Skip to content

Instantly share code, notes, and snippets.

@AhsanAyaz
Last active January 24, 2022 17:44
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save AhsanAyaz/5e0c7884b9fa9eb8874f01358f4425ea to your computer and use it in GitHub Desktop.
Save AhsanAyaz/5e0c7884b9fa9eb8874f01358f4425ea to your computer and use it in GitHub Desktop.
Automagically accept all friend requests on your profile
/**
* @author Muhammad Ahsan Ayaz
* Usage:
* Go to https://www.facebook.com/friends/requests/
* Run the following code
*/
function removeFriendRequest({row, removeRequests, counter, timerDelay}) {
if (removeRequests) {
const removeButton = row.closest('a').querySelector('[aria-label="Remove"]:not([aria-disabled="true"])')
setTimeout(() => {
removeButton.click();
}, counter * timerDelay)
}
}
function acceptRequests ({container, mutualThreshold, timerDelay, removeRequests}) {
let counter = 0;
var rows = container.querySelectorAll('[aria-label="Confirm"]:not([aria-disabled="true"])')
rows.forEach((row, index) => {
try {
if (mutualThreshold > 0) {
const friendsCountLabel = row.closest('a').querySelector('[aria-labelledby$="friends"]')
if (!friendsCountLabel) {
removeFriendRequest({row, removeRequests, counter: ++counter, timerDelay});
return;
}
const friendsCount = Number(friendsCountLabel.getAttribute('aria-labelledby').split(' mutual')[0]);
if (friendsCount < mutualThreshold) {
removeFriendRequest({row, removeRequests, counter: ++counter, timerDelay});
return;
}
}
setTimeout(() => {
row.click();
}, ++counter * timerDelay)
}
catch (e) {
// no need to do anything here. You could use `console.log(e);` though.
}
})
setTimeout(() => {
container.scrollTop = container.scrollHeight;
}, rows.length -1 * timerDelay)
}
function acceptAllFriendRequests (config) {
const scriptConfig = {
mutualThreshold: 0,
timerDelay: 500,
removeRequests: false,
...config
}
let container = document.querySelectorAll(config.containerClass);
container = container[container.length - 1];
scriptConfig.container = container;
acceptRequests(scriptConfig);
setInterval(() => {
acceptRequests(scriptConfig);
}, 10000)
}
/**
* Make sure to pass the correct css selector for the container element of the friend requests row
*/
acceptAllFriendRequests({
containerClass: '.rpm2j7zs.k7i0oixp.gvuykj2m.j83agx80',
mutualThreshold: 10,
timerDelay: 500,
removeRequests: true
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment