Skip to content

Instantly share code, notes, and snippets.

@jcguu95
Last active March 1, 2024 14:26
Show Gist options
  • Save jcguu95/dbb61efe9f66543b1203f6bab2434b21 to your computer and use it in GitHub Desktop.
Save jcguu95/dbb61efe9f66543b1203f6bab2434b21 to your computer and use it in GitHub Desktop.
Remove Facebook Feeds
// ==UserScript==
// @name Remove Facebook Feeds
// @version 2024-03-02
// @author Jin-Cheng Guu
// @match https://www.facebook.com/*
// @description Remove feeds from the Facebook main page.
// ==/UserScript==
(function() {
function MainDivs (element) {
// Recursively collects all div elements under the
// specified node element with the role "main".
let mainDivs = [];
if (element.tagName === 'DIV' && element.getAttribute('role') === 'main') {
mainDivs.push(element);
}
for (let i = 0; i < element.children.length; i++) {
mainDivs = mainDivs.concat(MainDivs(element.children[i]));
}
return mainDivs
}
function removeFeeds () {
let mainDivs = MainDivs(document.body);
mainDivs.forEach(div => div.remove());
}
removeFeeds();
// Tampermonkey does not re-run the code sometimes even when
// the user navigates to another page in facebook. However,
// mutation observer or the popstate method does not address
// the issue of detecting page changes either. We implemented
// an alternative solution. This approach, while not the
// best, attempts to remove whenever the user clicks.
let blacklist = ["www.facebook.com/", "www.facebook.com"]
let waitLength = 500 // millisecond
document.onclick = function (event) {
setTimeout(() => {
if (blacklist.includes(window.location.href.split("://")[1])) {
console.log("Remove main body in facebook main page.");
removeFeeds();
};
}, waitLength);
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment