Skip to content

Instantly share code, notes, and snippets.

@JamesTheHacker
Created May 8, 2018 09:53
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JamesTheHacker/fd344851e24590aa47e6882a2663115a to your computer and use it in GitHub Desktop.
Save JamesTheHacker/fd344851e24590aa47e6882a2663115a to your computer and use it in GitHub Desktop.
Facebook Group Scraper with Tampermonkey and JavaScript
// ==UserScript==
// @name Facebook Group Scraper
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author James Jeffery <jamesjefferyuk@protonmail.com>
// @match https://www.facebook.com/search/str/*/keywords_groups
// @grant GM_setClipboard
// ==/UserScript==
let urls = null;
function MutationObserverCallback(mutations) {
for(let mutation of mutations) {
const groups = [].slice.call(mutation.target.querySelectorAll('a.lfloat[href*=groups]'));
urls = groups.map(function(group) {
return 'https://facebook.com' + group.getAttribute('href');
});
}
}
function injectButton() {
const filterContainer = document.querySelector('div[data-testid=filters_container]');
const button = document.createElement('button');
button.innerText = 'Copy Groups';
button.addEventListener('click', function(e) {
GM_setClipboard(urls.join('\n'));
});
const filterSection = document.createElement('div');
filterSection.setAttribute('data-testid', 'filters_section');
const filterHeader = document.createElement('h4');
filterHeader.innerText = 'COPY TO CLIPBOARD';
filterSection.appendChild(filterHeader);
filterSection.appendChild(button);
filterContainer.appendChild(filterSection);
}
window.addEventListener('load', function() {
injectButton();
const targetNode = document.querySelector('#pagelet_loader_initial_browse_result > div > div');
const config = { childList: true };
const observer = new MutationObserver(MutationObserverCallback);
observer.observe(targetNode, config);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment