Skip to content

Instantly share code, notes, and snippets.

@PixelPaw-Coding
Last active October 24, 2025 13:11
Show Gist options
  • Select an option

  • Save PixelPaw-Coding/21b3ecbc35dc101e2bd82bce9bce4a9a to your computer and use it in GitHub Desktop.

Select an option

Save PixelPaw-Coding/21b3ecbc35dc101e2bd82bce9bce4a9a to your computer and use it in GitHub Desktop.
Lightweight ad blocker for Pale Moon using CSS injection via Greasemonkey.
// ==UserScript==
// @name Go Away Ads! (Pale Moon)
// @namespace https://github.com/PixelPaw-Coding
// @version 1.2
// @description Lightweight ad blocker for Pale Moon using CSS injection and dynamic removal
// @match *://*/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
const STYLE_ID = 'go-away-ads-style';
const cssText = `
iframe[id^="ad"], iframe[class*="ad"], .ad, .ads, .adsbygoogle,
[class*="advert"], [id*="advert"], [data-ad], [data-ad-client],
div[id^="google_ads"], div[class*="sponsor"], [class*="banner"],
section[class*="ad"], [aria-label*="advertisement"] {
display: none !important;
visibility: hidden !important;
height: 0 !important;
width: 0 !important;
overflow: hidden !important;
margin: 0 !important;
padding: 0 !important;
}
`;
function injectStyle() {
if (!document.getElementById(STYLE_ID)) {
const style = document.createElement('style');
style.id = STYLE_ID;
style.textContent = cssText;
document.head.appendChild(style);
}
}
function removeAds() {
const ads = document.querySelectorAll(
'iframe[id^="ad"], iframe[class*="ad"], .ad, .ads, .adsbygoogle, ' +
'[class*="advert"], [id*="advert"], [data-ad], [data-ad-client], ' +
'div[id^="google_ads"], div[class*="sponsor"], [class*="banner"], ' +
'section[class*="ad"], [aria-label*="advertisement"]'
);
ads.forEach(ad => ad.remove());
}
function init() {
injectStyle();
removeAds();
// Watch for new elements dynamically added to the page
const observer = new MutationObserver(() => {
injectStyle();
removeAds();
});
observer.observe(document.body, { childList: true, subtree: true });
}
// Wait for <head> to exist
if (document.head) {
init();
} else {
const observer = new MutationObserver(() => {
if (document.head) {
init();
observer.disconnect();
}
});
observer.observe(document.documentElement, { childList: true, subtree: true });
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment