Last active
June 27, 2022 15:16
-
-
Save maury91/d054d9f38650d70b64ec583845231f20 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==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); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
must have worked 48hours I guess, installed today, already broken, they must have changed something this week-end