Skip to content

Instantly share code, notes, and snippets.

@4msar
Last active May 14, 2026 08:51
Show Gist options
  • Select an option

  • Save 4msar/b7bde6b98e85b28c5d86065ac8d1b223 to your computer and use it in GitHub Desktop.

Select an option

Save 4msar/b7bde6b98e85b28c5d86065ac8d1b223 to your computer and use it in GitHub Desktop.
Remove Facebook Sponsored Post
// ==UserScript==
// @name Filter Facebook
// @version 1.0
// @description Filter all sponsored post and reels from facebook while browsing.
// @downloadURL https://gist.githubusercontent.com/4msar/b7bde6b98e85b28c5d86065ac8d1b223/raw/030f4c34d9692702622cd41d2c0a5bc469f2cc73/filter.js
// @updateURL https://gist.githubusercontent.com/4msar/b7bde6b98e85b28c5d86065ac8d1b223/raw/030f4c34d9692702622cd41d2c0a5bc469f2cc73/filter.js
// @homepageURL https://gist.github.com/4msar/b7bde6b98e85b28c5d86065ac8d1b223
// @author Saiful Alam (msar.me)
// @include *facebook.com*
// @include *fb.com*
// ==/UserScript==
function main() {
const removeSponsored = () => {
const words = ["Sponsored", "Patrocinado", "Gesponsert"];
document.querySelectorAll('div[data-focus="feed_story"]').forEach((el) => {
const anchorTags = [...el.querySelectorAll("a")].filter((a) => a.href);
if (
// Start filtering
el.querySelector('a[href*="/ads/"]') ||
words.some((w) => el.innerText.includes(w)) ||
anchorTags.some(
(link) =>
link.href.includes("/ads/") ||
(link.innerText === "" && a.querySelectorAll("canvas").length > 9),
)
// End filtering
) {
el.remove();
}
});
};
const hideReels = () => {
[...document.querySelectorAll('div[data-focus="feed_story"]')]
.filter((el) => {
const match = [...el.querySelectorAll("h1,h2,h3,h4,span,div")].filter(
(el) => el.textContent.trim() === "Reels",
);
return match.length > 0;
})
.forEach((item) => item.remove());
};
const init = () => {
const timer = setTimeout(() => {
clearTimeout(timer);
removeSponsored();
hideReels();
}, 100);
};
new MutationObserver(init).observe(document.body, {
childList: true,
subtree: true,
});
window.addEventListener("scroll", init, { passive: true });
document.addEventListener("scroll", init, {
passive: true,
capture: true,
});
if ("onscrollend" in window) window.addEventListener("scrollend", init, { passive: true });
}
main();
@4msar

4msar commented May 14, 2026

Copy link
Copy Markdown
Author

Open your AdGuard Settings and go to the Extensions tab.

Click the + icon and select Create User Script (or choose Install from File/URL).

Paste the code there, save it, and you’re done — Facebook ads should now be gone while browsing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment