Skip to content

Instantly share code, notes, and snippets.

@maury91
Last active June 27, 2022 15:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save maury91/d054d9f38650d70b64ec583845231f20 to your computer and use it in GitHub Desktop.
Save maury91/d054d9f38650d70b64ec583845231f20 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Goodbye new FB ads
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Remove facebook ads
// @author Maurizio Carboni
// @match https://www.facebook.com/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
function realText(elem) {
if (elem instanceof Text) {
return elem.textContent;
}
const visibleText = [...elem.childNodes]
.filter(elem => elem instanceof Text || window.getComputedStyle(elem, "").display !== 'none')
.flatMap(elem => realText(elem))
.join('')
return visibleText
}
function isSponsored(elem) {
return elem && realText(elem).toLowerCase().startsWith('sponsored')
}
function isArticleSponsored(elem) {
return isSponsored(elem.querySelector('div[id^=feed_subtitle]'))
}
const domInsertionObserver = new MutationObserver(function(mutationsList){
[...mutationsList]
.filter(mutation => mutation.type === 'childList' && mutation.addedNodes.length)
.flatMap(mutation => [...mutation.addedNodes])
.filter(addedNode => typeof addedNode.id === 'string' && addedNode.id.startsWith('u_fetchstream'))
.filter(isArticleSponsored)
.forEach(post => post.parentElement.removeChild(post))
});
domInsertionObserver.observe(document, { childList: true, subtree: true });
function getSponsoredPosts() {
const sponsoredArticles = [...document.querySelectorAll('[role=article]')]
.filter(isArticleSponsored)
return sponsoredArticles;
}
setTimeout(function() {
// Remove the first one (is added without ajax)
getSponsoredPosts().forEach(post => post.parentElement.removeChild(post))
}, 3000);
})();
@revepastrop
Copy link

must have worked 48hours I guess, installed today, already broken, they must have changed something this week-end

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