Skip to content

Instantly share code, notes, and snippets.

@pombadev
Last active July 8, 2024 06:03
Show Gist options
  • Save pombadev/644ffcdbacc2f6b382ad6b89ee57f56e to your computer and use it in GitHub Desktop.
Save pombadev/644ffcdbacc2f6b382ad6b89ee57f56e to your computer and use it in GitHub Desktop.
CrapCleaner: Clean sponsered post from feed
// ==UserScript==
// @name CrapCleaner
// @namespace https://github.com/pombadev
// @match https://www.facebook.com/
// @grant none
// @version 1.3
// @author pombadev
// @description Clean sponsered post from feed
// @require https://cdn.jsdelivr.net/npm/@violentmonkey/dom@2
// ==/UserScript==
let counter = 0;
const count = document.createElement("p");
const MAGIC_CLASS_NAME = `.x1lliihq`;
function adKeywords(pieces) {
return pieces.includes(" · Follow") || pieces.includes("Join")|| pieces.includes("Suggested for you")
}
VM.observe(document.body, () => {
const isMobile = document.getElementById("app-body");
const ads = isMobile
? Array.from(
document.querySelectorAll("div[data-tracking-duration-id]"),
).filter((el) => {
if (
el.innerText
.split("\n")
.find((s) => s.match(/^Follow|^Sponsored|^Suggested for you/))
) {
return true;
}
})
: Array.from(document.querySelectorAll(MAGIC_CLASS_NAME)).filter((el) => {
if (el.tagName !== "DIV") return false;
const text = el.innerText?.trim() ?? "";
if (text.length < 5) return false;
let hasAds = false;
if (text.split(/\s+/g).filter(adKeywords).length > 0) {
hasAds = true
} else if (text.split('\n').filter(adKeywords).length > 0) {
hasAds = true
}
return hasAds;
});
ads.forEach((el) => {
console.info("Removing", { el });
el.hidden = true;
el.remove();
counter++;
const logo = Array.from(
document.querySelectorAll(`[aria-label="Facebook"]`),
).find((el) => el.tagName === "A");
if (logo) {
count.innerText = counter;
count.style = "color: red;margin: -10px -50px 25px;";
logo.parentNode.appendChild(count);
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment