Skip to content

Instantly share code, notes, and snippets.

@africanmx
Created March 31, 2025 12:37
Show Gist options
  • Save africanmx/8584bffd7e01113db69ae2fdc9ceeb6a to your computer and use it in GitHub Desktop.
Save africanmx/8584bffd7e01113db69ae2fdc9ceeb6a to your computer and use it in GitHub Desktop.
IIFE Script for Removing and Monitoring Ads
(function() {
'use strict';
// Configuration - adjust these selectors based on the ads you're targeting
const adSelectors = [
// Common ad classes/IDs
'.ad', '.ads', '.advertisement',
'.ad-container', '.ad-wrapper',
'[id*="ad"]', '[class*="ad"]',
'[id*="Ad"]', '[class*="Ad"]',
'[id*="banner"]', '[class*="banner"]',
'iframe[src*="ads"]', 'iframe[src*="advertisement"]',
// Add any platform-specific ad selectors you encounter
];
// Function to remove ads
function removeAds() {
adSelectors.forEach(selector => {
try {
document.querySelectorAll(selector).forEach(element => {
// Remove the element
element.remove();
console.log('Removed ad element:', selector);
});
} catch (e) {
console.warn('Error removing ads with selector', selector, e);
}
});
}
// Function to check for mutations (new ads appearing)
function setupMutationObserver() {
const observer = new MutationObserver((mutations) => {
let needsCheck = false;
mutations.forEach(mutation => {
if (mutation.addedNodes.length > 0) {
needsCheck = true;
}
});
if (needsCheck) {
removeAds();
}
});
// Observe the entire document for changes
observer.observe(document.body, {
childList: true,
subtree: true
});
return observer;
}
// Initialize the ad removal system
function init() {
// Remove existing ads immediately
removeAds();
// Set up observer for new ads
const observer = setupMutationObserver();
// Also check periodically as a fallback
const interval = setInterval(removeAds, 3000);
// Cleanup function if needed
return function cleanup() {
observer.disconnect();
clearInterval(interval);
};
}
// Start the ad removal system
const cleanup = init();
// Optional: Expose cleanup to window if you need to stop it later
window.adCleanup = cleanup;
console.log('Ad removal script initialized');
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment