Skip to content

Instantly share code, notes, and snippets.

@CezaryDanielNowak
Last active September 24, 2024 10:13
Show Gist options
  • Save CezaryDanielNowak/41cd6e633a5cd7db70c5b260701091d2 to your computer and use it in GitHub Desktop.
Save CezaryDanielNowak/41cd6e633a5cd7db70c5b260701091d2 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Remove junk
// @namespace http://tampermonkey.net/
// @version 2024-09-23
// @author Cezary Daniel Nowak
// @match https://www.facebook.com/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=facebook.com
// @grant none
// @run-at document-end
// ==/UserScript==
(() => {
let mainNode;
setInterval(() => {
mainNode = document.querySelector('[role="main"]');
removeReels();
removeSponsored();
removeUnknownPagesFromTheFeed();
removeUnknownGroupsFromTheFeed();
}, 750);
removeSponsoredFromRightPanelOnce();
function removeReels() {
const nodes = document.evaluate('//*[text()="Reels and short videos"]', mainNode);
let node;
while (node = nodes.iterateNext()) {
hideNode(parentNode(node, 8));
}
}
function removeUnknownPagesFromTheFeed() {
const nodes = document.evaluate('//span[text()="Follow"]', mainNode);
let node;
while (node = nodes.iterateNext()) {
hideNode(parentNode(node, 11));
}
}
function removeUnknownGroupsFromTheFeed() {
const nodes = document.evaluate('//span[text()="Join"]', mainNode);
let node;
while (node = nodes.iterateNext()) {
hideNode(parentNode(node, 13));
}
}
function removeSponsored() {
// TODO: use findParentUntil for every selection
const nodes = [...mainNode.querySelectorAll('[aria-label="Sponsored"]')];
nodes.forEach((node) => {
const elementToRemove = findParentUntil(node, (nodeParent) => {
if (
nodeParent &&
nodeParent.parentNode.children.length !== 2 &&
nodeParent.previousElementSibling?.className === nodeParent.className
) {
return true;
}
});
hideNode(elementToRemove);
});
}
function removeSponsoredFromRightPanelOnce() {
const contextNode = document.querySelector('[role="complementary"]');
const node = document.evaluate('//span[text()="Your Pages and profiles"]', contextNode).iterateNext();
const elementToRemove = findParentUntil(node, (nodeParent) => {
if (nodeParent && nodeParent.previousElementSibling?.nodeName === 'SPAN') {
const nodeToTest = nodeParent.previousElementSibling;
if (nodeToTest.firstChild === nodeToTest.lastChild) {
return true;
}
}
});
hideNode(elementToRemove.previousElementSibling, true);
}
function parentNode(startNode, levels) {
if (!startNode) return;
let result = startNode;
for (let i = 0; i < levels; ++i) {
result = result.parentNode || result;
}
return result;
}
function findParentUntil(startNode, condition) {
if (!startNode) return;
let node = startNode;
while (node) {
if (condition(node)) {
return node;
}
node = node.parentNode;
}
}
function hideNode(node, hard = false) {
if (!node) return;
if (hard) {
node.style.display = 'none';
return;
}
node.style.opacity = '0.025';
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment