Facebook remove suggested sponsored ads blocks

Remove suggested/sponsored blocks from Facebook

Tính đến 12-10-2024. Xem phiên bản mới nhất.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(Tôi đã có Trình quản lý tập lệnh người dùng, hãy cài đặt nó!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name Facebook remove suggested sponsored ads blocks
// @version 1.20.7
// @description Remove suggested/sponsored blocks from Facebook
// @author Sly_North
// @match https://www.facebook.com/*
// @exclude https://www.facebook.com/login/*
// @exclude https://www.facebook.com/groups/*
// @exclude https://www.facebook.com/privacy/*
// @exclude https://www.facebook.com/*/about
// @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');

const maxPostHeight = 1000;

function getWholePost(e, maxParentHeightDiff = 150) {
  let parent = e.parentElement.parentElement.parentElement.parentElement;
  let h = parent.getBoundingClientRect().height;
  if (h > maxPostHeight) return null;
  while (h < 90) {
    parent = parent.parentElement;
    h = parent.getBoundingClientRect().height;
  }
  let h2 = 0;
  while (true) {
    if (!parent.parentElement) { console.warn('Err: element has no parent H=', parent.getBoundingClientRect().height); break; }
    h2 = parent.parentElement.getBoundingClientRect().height;
    if (h2 > maxPostHeight || h2 - h > maxParentHeightDiff) break;
    parent = parent.parentElement;
    h = h2;
  }
  return parent;
}

function removeElement(type, e, parent) {
  const height = parent.getBoundingClientRect().height;
  if (height > maxPostHeight) {
    console.log('- Fb ads - could not find parent for ', type, ' tag= ', parent.tagName, ' H=', height, ' ', e.innerText);
    return;
  }
  console.log('- Fb ads ', type, ' tag= ', parent.tagName, ' H=', height, ' ',
              parent.innerText.replaceAll("\n"," ").replaceAll(/Facebook  *Facebook  */g, ""), ' from ', e.innerText);
  e.style.display = "none";
  parent.style.display = "none";
}

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 => {
      const 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);
	    if (!parent) { console.log(' - Fb ads sponsored element: parent element not found!'); continue; }
      if (parent.innerText.match(/Create new account/) || parent.innerText.match(/shared a memory/)) continue;
      removeElement('sponsored post', e, parent);
    }
  
    // "Learn more" suggestions
    maybeAds = Array.from(document.getElementsByTagName('span')).filter(e => e.textContent === 'Learn more');
    for (let e of maybeAds) {
      const parent = getWholePost(e);
	    if (!parent) { console.log(' - Fb ads sponsored element: parent element not found!'); continue; }
      removeElement('learn more', e, parent);
    }
  }

	// Unwanted elements
  var elts = Array.from(document.getElementsByTagName('span'));
  elts = elts.filter((e) => {var br = e.getBoundingClientRect(); return !e.style.display && br.bottom >= 0 && br.top <= window.innerHeight + 1000 && !e.style.display});
  elts = elts.filter((e) => {
    const t = e.innerText;
    const br = e.getBoundingClientRect();
    if (br.height === 0) return false;
    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);
    if (!parent) { console.log(' - Fb ads/block remover: parent element not found!'); continue; }
    const br = e.getBoundingClientRect();
    const parentBr = parent.getBoundingClientRect();
    if (br.height === 0 || parentBr.height > 1000 || br.top-parentBr.top > 500) { // Ignore post citing post
      // console.log(' - Fb ads/block: NOT removing tag=', e.tagName, ' H=', e.getBoundingClientRect().height, ' parent tag= ', parent.tagName, 'H=', parent.getBoundingClientRect().height, ' ', parent.innerText.replaceAll("\n", "  ").substring(0, 200));
      continue;
    }
    removeElement('ads/block', e, parent);
  }
}

setTimeout(RemoveAllSponsored, 1000);