Skip to content

Instantly share code, notes, and snippets.

@pilotpirxie
Last active November 11, 2024 11:23
Show Gist options
  • Save pilotpirxie/fc70813ea064d634afbc25b9fd2c7dd6 to your computer and use it in GitHub Desktop.
Save pilotpirxie/fc70813ea064d634afbc25b9fd2c7dd6 to your computer and use it in GitHub Desktop.
Tampermonkey Hide Facebook Feed
// ==UserScript==
// @name Tampermonkey Hide Facebook Feed
// @namespace http://tampermonkey.net/
// @version 2024-10-09
// @description Remove Facebook main feed while keeping messages and groups
// @author pilotpirxie
// @match https://*.facebook.com/*
// @grant none
// @run-at document-start
// ==/UserScript==
(function() {
'use strict';
const isMainFeedPage = () => {
const path = window.location.pathname;
return path === '/' ||
path === '/home.php' ||
path.startsWith('/?') ||
path === '/home' ||
path === '';
};
let showTimeout = null;
const createStyle = () => {
const style = document.createElement('style');
style.id = 'fb-feed-blocker';
style.textContent = `
div[role="main"] {
display: none !important;
}
`;
return style;
};
const updateStyle = () => {
if (showTimeout) {
clearTimeout(showTimeout);
showTimeout = null;
}
const existingStyle = document.getElementById('fb-feed-blocker');
if (isMainFeedPage()) {
if (existingStyle) {
existingStyle.textContent = createStyle().textContent;
} else {
document.head.appendChild(createStyle());
}
} else {
showTimeout = setTimeout(() => {
if (existingStyle) {
existingStyle.textContent = '';
}
}, 300);
}
};
const ensureStyle = () => {
if (!document.getElementById('fb-feed-blocker')) {
document.head.appendChild(createStyle());
}
updateStyle();
};
const injectInitialStyle = () => {
if (document.head) {
ensureStyle();
} else {
const observer = new MutationObserver((mutations, obs) => {
if (document.head) {
ensureStyle();
obs.disconnect();
}
});
observer.observe(document.documentElement, {
childList: true,
subtree: true
});
}
};
const watchNavigation = () => {
const originalPushState = history.pushState;
const originalReplaceState = history.replaceState;
history.pushState = function() {
originalPushState.apply(this, arguments);
updateStyle();
};
history.replaceState = function() {
originalReplaceState.apply(this, arguments);
updateStyle();
};
window.addEventListener('popstate', updateStyle);
};
const watchStyleChanges = () => {
const observer = new MutationObserver((mutations) => {
for (const mutation of mutations) {
if (mutation.type === 'childList' &&
(mutation.target === document.head || mutation.target === document.body)) {
ensureStyle();
}
}
});
observer.observe(document.documentElement, {
childList: true,
subtree: true,
attributes: true,
attributeFilter: ['style']
});
};
const init = () => {
injectInitialStyle();
watchNavigation();
watchStyleChanges();
};
init();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment