NOTICE: By continued use of this site you understand and agree to the binding Terms of Service and Privacy Policy.
// ==UserScript==
// @namespace https://openuserjs.org/users/etaylib
// @name Hide Sponsored Ads in Facebook
// @description Finding those ads in your feed and hides them.
// @copyright 2019, etaylib (https://openuserjs.org/users/etaylib)
// @match https://www.facebook.com/*
// @license MIT
// @version 0.0.8
// @grant none
// ==/UserScript==
// ==OpenUserJS==
// @author etaylib
// ==/OpenUserJS==
// todo: a bit more modular
let blockedAds, div;
const targetNode = document.getElementById('contentArea');
console.log(targetNode);
if (!targetNode) {
return false;
}
const showCounter = false; // EXPERIMENTAL! REMOVE THE NOTIFICATION DIV BELOW IF YOU DON'T WANT TO KNOW HOW MANY ADS WERE BLOCKED //
if (showCounter) {
blockedAds = 0;
div = document.createElement('div');
div.style.cssText = 'position:fixed;top:20px;left:20px;border:1px solid #000; background-color:#fff; padding:5px;z-index:2147483647;';
div.innerText = 'Blocked 0 Sponsored ads on this page';
document.body.appendChild(div);
}
document.querySelectorAll('.userContentWrapper').forEach(wrapper => {
Array.from(wrapper.querySelectorAll('span')).some(e => {
if (e.innerText === 'Sponsored') {
wrapper.style.display = 'none';
if (showCounter) {
blockedAds++;
div.innerText = 'Blocked ' + blockedAds + ' Sponsored ads on this page';
}
return true;
}
});
});
const config = {
childList: true,
subtree: true,
attributeFilter: ['div']
};
const callback = (mutationsList, observer) => {
for (let mutation of mutationsList) {
if (mutation.type === 'childList') {
const target = mutation.target;
if (target.className.includes('userContent')) {
const wrapper = target.closest('.userContentWrapper');
Array.from(wrapper.querySelectorAll('span')).some(e => {
if (e.innerText === 'Sponsored') {
wrapper.style.display = 'none';
if (showCounter) {
blockedAds++;
div.innerText = 'Blocked ' + blockedAds + ' Sponsored ads on this page';
}
return true;
}
});
}
}
};
};
const newPosts = new MutationObserver(callback);
newPosts.observe(targetNode, config);