Skip to content

Instantly share code, notes, and snippets.

@christophercalm
Last active July 2, 2022 18:39
Show Gist options
  • Save christophercalm/284f07fa75ae1f2666249d35a54e3990 to your computer and use it in GitHub Desktop.
Save christophercalm/284f07fa75ae1f2666249d35a54e3990 to your computer and use it in GitHub Desktop.
Finds all html elements with fbclid in them to block ads.
// ==UserScript==
// @name Remove Facebook Sponsored Elements
// @namespace http://tampermonkey.net/
// @version 0.1.1
// @description Remove Facebook Sponsored Elements
// @author You
// @match https://www.facebook.com/
// @icon https://www.google.com/s2/favicons?sz=64&domain=tampermonkey.net
// @grant none
// @run-at document-idle
// ==/UserScript==
(function() {
'use strict';
// Your code here...
// get all elements with href that container fbclid
const getFbAdElementsAndBlock = () =>{
const elements = document.getElementsByTagName('a');
const adElements = [];
for (const element of elements) {
if (element.href.indexOf('fbclid') > -1 || element.href.indexOf('l.php') > -1) {
adElements.push(element);
}
const isMainPageSponsored = (element) => element.href.indexOf('fbclid') > -1;
const isMarketplaceSponsored = (element) => window.location.href.indexOf('marketplace/') > -1 && element.href.indexOf('l.php') > -1;
if (isMainPageSponsored(element) || isMarketplaceSponsored(element)) {
adElements.push(element);
}
}
for (const element of adElements) {
element.style.display = 'none';
}
setTimeout(getFbAdElementsAndBlock, 3000);
}
getFbAdElementsAndBlock();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment