Skip to content

Instantly share code, notes, and snippets.

@cmoog
Last active April 2, 2023 01:17
Show Gist options
  • Save cmoog/a52a8107f7c99873ddcfa19e86ad3fb2 to your computer and use it in GitHub Desktop.
Save cmoog/a52a8107f7c99873ddcfa19e86ad3fb2 to your computer and use it in GitHub Desktop.
facebook ad killer for uBlockOrigin

Step 1

Navigate to uBlock origin Settings > Advanced Settings. Change the value of userResourcesLocation

https://gist.githubusercontent.com/cmoog/a52a8107f7c99873ddcfa19e86ad3fb2/raw/fde58e3163fa4c36aae5e3d6b1f9dbade21ee30d/facebook-ad-killer.js

Step 2

Update your uBlock origin My Filters to include the following "rules"

www.facebook.com##+js(facebook-hide-sponsored)

Done

/// facebook-hide-sponsored.js
(() => {
"use strict";
const INTERVAL = 1500;
setInterval(() => {
let element = Array.from(
document.querySelectorAll(
`span[style="-webkit-box-orient:vertical;-webkit-line-clamp:2;display:-webkit-box"]`
)
).find(({ innerHTML }) => innerHTML.trim() === "Sponsored");
if (!element) {
console.warn("could not find sponsored sidebar");
return;
}
console.log("found sponsored sidebar header", element);
const DEPTH = 8;
for (let i = 0; i < DEPTH; i++) {
element = element.parentElement;
}
console.log("hiding sidebar", element);
element.style.display = "none";
}, INTERVAL);
setInterval(() => {
Array.from(document.getElementsByTagName("text"))
.filter(({ innerHTML }) => innerHTML.match("Sponsored"))
.forEach(({ id }) => {
Array.from(document.getElementsByTagName("use"))
.filter(
({ attributes }) =>
attributes.getNamedItem("xlink:href")?.value === `#${id}`
)
.forEach((element) => {
const MAX_DEPTH = 20;
const getClass = (e) => e?.attributes.getNamedItem("class")?.value;
for (let i = 0; i < MAX_DEPTH; i++) {
element = element.parentElement;
if (!element) break;
if (element.tagName !== "DIV") continue;
if (
getClass(element) === getClass(element.previousSibling) &&
getClass(element) === getClass(element.nextSibling)
) {
console.log("hiding sponsored feed post", element);
element.style.display = "none";
console.count("ad-hidden");
}
}
});
});
}, INTERVAL);
})();
/// archive-redirect.js
(() => {
"use strict";
const newUrl = new URL("https://archive.ph");
newUrl.pathname = `/latest/${window.location.toString()}`;
newUrl.search = ''
window.location = newUrl.toString();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment