Skip to content

Instantly share code, notes, and snippets.

@jeffmikels
Created January 13, 2021 20:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeffmikels/445ea229563a1600152a1e30b277cd4c to your computer and use it in GitHub Desktop.
Save jeffmikels/445ea229563a1600152a1e30b277cd4c to your computer and use it in GitHub Desktop.
browser script to process facebook friends in bulk
// THIS SCRIPT WORKS ON THE FOLLOWING FACEBOOK PAGE
// https://www.facebook.com/[yourprofile]/friends
let loopcancel = false;
let next_friend = 0;
// not reliable... will also return liked pages and events
let friendTileClasses = 'bp9cbjyn ue3kfks5 pw54ja7n uo3d90p7 l82x9zwi n1f8r23x rq0escxv j83agx80 bi6gxh9e discj3wi hv4rvrfc ihqw7lf3 dati1w0a gfomwglr';
function get_friends() {
let friends = [];
let buttons = get_friend_buttons()
for (let button of buttons) {
let el = button.parentElement.parentElement.parentElement.parentElement;
let text = el.innerText;
let [name, mutual] = text.split('\n');
friends.push({ el, text, name, mutual, button })
}
return friends;
}
function cancel() {
loopcancel = true;
}
function scroll() {
window.scrollTo(0, document.getElementsByTagName('html')[0].offsetHeight)
}
function start(start_at = 0){
loopcancel = false;
next_friend = start_at;
loop();
}
function get_friend_buttons() {
return document.querySelectorAll('[aria-label=Friends][role=button]');
}
function loop() {
// abort and reset the cancel flag
if (loopcancel) return;
let friends = get_friends()
// should we quit?
if (next_friend >= friends.length) {
console.log('done');
return;
}
// should we try to grab more friends?
if (next_friend >= friends.length-5) {
scroll();
}
// do the clicking
let friend = friends[next_friend];
console.log(`\nCHECKING: ${friend.name} (#${next_friend})`)
friend.el.scrollIntoViewIfNeeded()
friend.button.click();
// follow timeout
setTimeout(()=>{
click_follow();
// close_menu timeout
setTimeout(()=>{
close_menu();
next_friend++;
setTimeout(loop, 200); // the menu closes really fast
},1200); // the follow operation might take a smidge
},200); // menu opens really fast
}
function click_follow() {
click_ok(); // clear any dialogs that popped up
let menuitems = document.querySelectorAll('[role=menuitem]')
for (let item of menuitems) {
if (item.innerText == 'Follow') {
console.log('clicking follow');
item.click();
return;
}
}
console.log('already following');
}
function click_ok() {
let okbutton = document.querySelectorAll('[aria-label="OK"]');
if (okbutton.length > 0) okbutton[0].click()
}
function close_menu() {
document.getElementsByTagName('body')[0].click();
}
@jeffmikels
Copy link
Author

I find greater success when I make the timings a bit longer.

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