Facebook remove suggested sponsored ads blocks

Remove suggested/sponsored blocks from Facebook

当前为 2024-10-06 提交的版本,查看 最新版本

// ==UserScript==
// @name Facebook remove suggested sponsored ads blocks
// @version 1.20.0
// @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://gf.qytechs.cn/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 getWholePost(e, maxParentHeightDiff = 50) {
  let parent = e.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement;
  let h = parent.getBoundingClientRect().height;
  while (h < 90) {
    parent = parent.parentElement;
    h = parent.getBoundingClientRect().height;
  }
  let h2 = 0;
  while (true) {
    h2 = parent.parentElement.getBoundingClientRect().height;
    if (h2 > 10000 || h2 - h > maxParentHeightDiff) break;
    parent = parent.parentElement;
    h = h2;
  }
  return parent;
}

function RemoveAllSponsored()
{
  setTimeout(RemoveAllSponsored, 750);
  
  // Remove top right corner ads
  for (let e of Array.from(document.getElementsByTagName('h3')).filter(e => e.innerText === 'Sponsored')) {
    e.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.innerHTML = "";
    console.log('- Removing Fb top right ads');
  }

  // Remove the "suggestion" posts
  if (document.URL.match(/facebook.com\/*(\?.*)*/)) {
    let maybeAds = Array.from(document.getElementsByTagName('a')).filter(e => {
      let br = e.getBoundingClientRect(); return !e.style.display && br.left > window.innerWidth/2 && br.width > 100 && br.width < 200 && br.top > 100
    });
    for (let e of maybeAds) {
      const parent = getWholePost(e, 150);
      console.log('- Fb ads sponsored post: H=', parent.getBoundingClientRect().height, ' ', parent.innerText.replaceAll("\n"," ").replaceAll("Facebook Facebook ", ""));
      // console.log('   - H=', parent.getBoundingClientRect().height, ' / ', parent.parentElement.getBoundingClientRect().height);
      e.style.display = "none";
      parent.style.display = "none";
      // parent.innerHTML = '';
    }
  }

	// Unwanted elements
  var elts = Array.from(document.getElementsByTagName('span'));
  elts = elts.filter((e) => {var br = e.getBoundingClientRect(); return !e.display.style && br.bottom >= 0 && br.top <= window.innerHeight + 1000 && !e.style.display});
  elts = elts.filter((e) => {
    var t = e.innerText;
    return t === 'Suggested for you' || 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/) || t === 'Follow' || t === 'Join';
  });
  for (let e of elts) {
    const parent = getWholePost(e, 150);
    console.log('- Removing Fb ads/block ', parent.innerText.replaceAll("\n", "  ").substring(0, 200));
    e.style.display = "none";
    parent.style.display = "none";
  }
}

setTimeout(RemoveAllSponsored, 1000);

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址