-
-
Save dmitriy1980-zz/886ce94a8a2eae69d98ddd1dbb40989e to your computer and use it in GitHub Desktop.
This file contains 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