Last active
September 14, 2023 21:20
-
-
Save adrianiainlam/75ea1f92b763f4c1c6297157591f0d56 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 Hide Ads on Facebook | |
// @namespace https://tampermonkey.net/ | |
// @version 1.27.20 | |
// @description Hide sponsored feeds on Facebook | |
// @author KudoAmine edited by A I Lam | |
// @downloadURL https://gist.github.com/adrianiainlam/75ea1f92b763f4c1c6297157591f0d56/raw/hide-fb-ads.user.js | |
// @updateURL https://gist.github.com/adrianiainlam/75ea1f92b763f4c1c6297157591f0d56/raw/hide-fb-ads.user.js | |
// @match https://www.facebook.com/* | |
// @license MIT | |
// @grant none | |
// ==/UserScript== | |
const HideAds = () => { | |
var feeds = document.querySelectorAll("div[role=feed] > *:not([data-hide-ads-on-facebook-checked=true])"); | |
if (feeds.length == 0) { | |
feeds = document.querySelectorAll("#ssrb_feed_start ~ div > div > div:not([data-hide-ads-on-facebook-checked=true])"); | |
} | |
if (feeds.length == 0) { | |
feeds = document.querySelectorAll("#ssrb_feed_start ~ div h3 ~ div > div:not([data-hide-ads-on-facebook-checked=true])"); | |
} | |
if (feeds.length == 0) { | |
var h3 = Array.from(document.querySelectorAll("h3")).filter(x => x.innerText == "News Feed posts")[0]; | |
if (h3) { | |
var tmpArray = Array.from(h3.parentElement.children).filter(x => x.tagName == "DIV"); | |
feeds = Array.from(tmpArray[0].children).filter(x => x.getAttribute("data-hide-ads-on-facebook-checked") === null); | |
if (feeds.length == 0 && tmpArray.length > 1) { | |
feeds = Array.from(tmpArray[1].children).filter(x => x.getAttribute("data-hide-ads-on-facebook-checked") === null); | |
} | |
} | |
else { | |
var h2 = Array.from(document.querySelectorAll("h2")).filter(x => x.innerText == "News Feed posts")[0]; | |
if (h2) { | |
var tmpArray = Array.from(h2.parentElement.children).filter(x => x.tagName == "DIV"); | |
feeds = Array.from(tmpArray[0].children).filter(x => x.getAttribute("data-hide-ads-on-facebook-checked") === null); | |
if (feeds.length == 0 && tmpArray.length > 1) { | |
feeds = Array.from(tmpArray[1].children).filter(x => x.getAttribute("data-hide-ads-on-facebook-checked") === null); | |
} | |
} | |
} | |
} | |
for (let feed of feeds) { | |
if (feed.style.display != "none") { | |
for (let anchor of feed.getElementsByTagName("a")) { | |
if (anchor.innerText == "Sponsored" || anchor.href.indexOf("facebook.com/ads/") >= 0) { | |
feed.style.display = "none"; | |
return; | |
} | |
const flexboxes = anchor.querySelectorAll(["*[style*='display: flex']", "*[style*='display:flex']"]); | |
for (let box of flexboxes) { | |
const text = Array.from(box.childNodes) | |
.filter(x => getComputedStyle(x).display != "none" && parseFloat(getComputedStyle(x).top) < 1) | |
.sort((a, b) => getComputedStyle(a).order - getComputedStyle(b).order) | |
.map(x => x.innerText.trim()) | |
.join(""); | |
if (text == "Sponsored") { | |
feed.style.display = "none"; | |
return; | |
} | |
} | |
const svguses = anchor.querySelectorAll("svg > use"); | |
for (let svguse of svguses) { | |
const id = svguse.href.baseVal; | |
let elem = document.querySelector(id); | |
while (elem) { | |
if (elem.innerHTML == "Sponsored") { | |
feed.style.display = "none"; | |
return; | |
} | |
else { | |
let use = elem.querySelector("use"); | |
elem = use ? document.querySelector(use.href.baseVal) : null; | |
} | |
} | |
} | |
} | |
for (let div of feed.getElementsByTagName("div")) { | |
if (div.innerText.startsWith("Sponsored · Paid for by")) { | |
feed.style.display = "none"; | |
return; | |
} | |
} | |
} | |
feed.setAttribute("data-hide-ads-on-facebook-checked", "true"); | |
} | |
}; | |
HideAds(); | |
setTimeout(HideAds, 1000); | |
(function () { | |
window.addEventListener("scroll", () => { | |
HideAds(); | |
setTimeout(HideAds, 1000); | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment