// ==UserScript==
// @name Facebook remove suggested sponsored ads blocks
// @version 1.16.2
// @description Remove suggested/sponsored blocks from Facebook
// @author Sly_North
// @match https://www.facebook.com/*
// @exclude https://www.facebook.com/privacy/*
// @exclude https://www.facebook.com/settings/*
// @exclude https://www.facebook.com/marketplace/*
// @namespace https://greasyfork.org/en/users/759669-sly-north
// @license MIT
// @grant none
// @icon https://www.facebook.com/favicon.ico
// @run-at document-start
// ==/UserScript==
console.log('Start RemoveAllSponsored Facebook');
function getVisibleContent(e) {
var t = e.innerText.replaceAll("\n","");
var children = e.childNodes;
while (children.length == 1) children = children[0].childNodes;
var spanBottom = e.getBoundingClientRect().bottom;
children = Array.from(children).filter((c) => c.getBoundingClientRect && c.getBoundingClientRect().top <= spanBottom);
var a = [];
for (var c of children) {
var order = getComputedStyle(c).order;
if (order && window.getComputedStyle(c).display !== 'none') {
a[order] = c.innerText;
// console.log(c.style.order,'=',c.innerText, ' y=',c.getBoundingClientRect().top,' vs span.bottom=', spanBottom);
}
}
if (a.length == 0) a = t.split();
var intext = a.join().replaceAll(",","").replaceAll("\n","");
// if (a.length > 0) console.log('Array = ', intext, ' -OR- ', a.join().replaceAll(",","").replaceAll("\n",""));
if (intext == "") intext = t;
return intext;
}
function RemoveAllSponsored()
{
setTimeout(RemoveAllSponsored, 750);
// TODO support more languages if there's interest.
const keywords = [ /Sponsored/,/Commandvit/, /Sponsori/,/Reklamo/, /Publicid/,/Gesponser/, /Patrocinado/,/sugerisdos/, /贊助/, /RememberPassword/];
var nbrRemovedAds = 0;
var nbrSuggestedRemoved = 0;
if (document.URL.match(/facebook.com\/*(\?.*)*/)) {
/*
// New ads style - a <use> with a closed shadow-root we cannot inspect.
var useNodes = Array.from(document.getElementsByTagName('use'))
.filter((e) => {var br = e.getBoundingClientRect(); return br.bottom >= 0 && br.top <= window.innerHeight + 1000 && !e.style.display});
for (let e of useNodes) {
let w = e.getBoundingClientRect().width;
if (w == 0) continue;
if (w > 62 && w < 66) { // TODO: find a more robust way of identifying the ads <use...> items.
let parent = e.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement
.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement;
while (parent) {
let h = parent.getBoundingClientRect().height;
if (h > 100) break;
parent = parent.parentElement;
}
if (parent.parentElement.innerText.match(/your memories/)) continue;
if (parent.style.maxHeight) continue;
parent.style.maxHeight = "0px";
console.log('- Removing new style ads - w=', w, ': ', parent.innerText.replaceAll("\n", " ").substring(0, 200));
++nbrRemovedAds;
}
}
/**/
}
// Old Style
var elts = Array.from(document.getElementsByTagName('span'));
elts = elts.filter((e) => {var br = e.getBoundingClientRect(); return br.bottom >= 0 && br.top <= window.innerHeight + 1000 && !e.style.display});
var nbrSpans = elts.length;
for (var i = elts.length - 1; i >= 0; --i)
{
var e = elts[i];
var boundingRect = e.getBoundingClientRect();
if (boundingRect.width == 0 || boundingRect.height == 0) continue; // Not visible
var t = e.innerText;
if (t === 'Suggested for you' || t === 'Follow' || t === 'Remember Password' || t === 'Reels and short videos' || t === 'Suggested for you' || t === 'People You May Know' ||
t.match(/groups you might like/) || t.match(/groups suggested just for you/)) {
let parent = e.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement;
while (parent.getBoundingClientRect().height < 90) parent = parent.parentElement;
console.log('- Removing Fb suggestion h=', parent.getBoundingClientRect().height, ' reason=', t, ' text=', parent.innerText.replaceAll("\n", " ").substring(0, 50));
// parent.style.background = "red";
parent.style.display = "none";
e.style.display = "none";
++nbrSuggestedRemoved;
continue;
}
if (e.parentElement.tagName != 'SPAN') {
continue;
}
var intext = getVisibleContent(e);
if (intext.length > 0) for (k of keywords) {
if (intext.match(k)) {
let parent = e.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement
.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement;
// parent.style.background = "blue";
parent.style.display = "none";
e.style.display = "none";
++nbrRemovedAds;
console.log('- Removing Fb ads ', i, '[', parent.innerText.replaceAll("\n", " ").substring(0, 200), '] matching ', k, ' at ', intext);
break;
}
}
}
}
setTimeout(RemoveAllSponsored, 1000);