Skip to content

Instantly share code, notes, and snippets.

@mitchazj
Last active July 21, 2019 10:43
Show Gist options
  • Save mitchazj/5913491e846365028fa70bed1cae4acb to your computer and use it in GitHub Desktop.
Save mitchazj/5913491e846365028fa70bed1cae4acb to your computer and use it in GitHub Desktop.
Extract FB friends into JSON object via Devtools

Wrote this really quickly while prototyping some social tools. Might wrap it into a Chrome extension at some point.

let friends = [];
let delay = 2000;

const createFriend = (name, link, image) => {
  return {
    name,
    link,
    image
  };
}
const isLoadingFriends = () => {
  return document.querySelectorAll('.img.async_saving').length === 1;
}
const getFriendsOnPage = () => {
  let _friends = [];
  let links = document.querySelectorAll('a');	
  let friend_links = [...links].filter(x => x.href.endsWith('&hc_location=friends_tab'));
  for (let x = 0; x < friend_links.length - 1; x += 2) {
    let name = friend_links[x + 1].innerText;
    let link = friend_links[x + 1].href;
    let image = friend_links[x].firstElementChild.src;
    _friends.push(createFriend(name, link, image));
  }
  return _friends;
}
const getFriendsOnPageWithScroll = () => {
  window.scrollTo(0, document.body.scrollHeight);
  setTimeout(() => {
    while (isLoadingFriends()) { /* do nothing */ };
    let _friends = getFriendsOnPage();
    if (_friends.length == friends.length) {
      // All done!
      console.log("Finished loading friends.");
    } else {
      friends = _friends;
      getFriendsOnPageWithScroll();
    }
  }, delay);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment