Skip to content

Instantly share code, notes, and snippets.

@africanmx
Created March 31, 2025 12:40
Show Gist options
  • Save africanmx/cacfc1aa113cecdd20c1a3c7d137b348 to your computer and use it in GitHub Desktop.
Save africanmx/cacfc1aa113cecdd20c1a3c7d137b348 to your computer and use it in GitHub Desktop.
IIFE Script for Dark Mode and Removing and Monitoring Ads
(function() {
'use strict';
// ===== Configuration =====
const config = {
// Ad removal selectors
adSelectors: [
'.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"]',
],
// Dark mode enforcement
darkMode: {
enabled: true,
defaultBackground: '#121212',
defaultText: '#e0e0e0',
forceDarkClasses: [], // Add classes that should trigger dark mode
excludeSelectors: [
'img', 'video', 'iframe',
'.no-dark-mode', '.keep-light'
],
invertSelectors: [
'[style*="background-image"]',
'.invert-for-dark'
]
},
// Monitoring intervals
checkInterval: 3000,
mutationObserver: true
};
// ===== Core Functions =====
// Remove ads function
function removeAds() {
config.adSelectors.forEach(selector => {
try {
document.querySelectorAll(selector).forEach(element => {
element.remove();
console.debug('Removed ad element:', selector);
});
} catch (e) {
console.warn('Error removing ads with selector', selector, e);
}
});
}
// Apply dark mode styles
function applyDarkMode() {
if (!config.darkMode.enabled) return;
// Create or update our dark mode style element
let styleElement = document.getElementById('forced-dark-mode');
if (!styleElement) {
styleElement = document.createElement('style');
styleElement.id = 'forced-dark-mode';
document.head.appendChild(styleElement);
}
// Base dark mode CSS
let css = `
html, body {
background: ${config.darkMode.defaultBackground} !important;
color: ${config.darkMode.defaultText} !important;
scrollbar-color: #333 #111 !important;
}
* {
background: ${config.darkMode.defaultBackground} !important;
color: ${config.darkMode.defaultText} !important;
border-color: #444 !important;
}
a {
color: #4dabf7 !important;
}
input, textarea, select {
background: #333 !important;
border-color: #555 !important;
}
`;
// Add inverted elements
config.darkMode.invertSelectors.forEach(selector => {
css += `
${selector} {
filter: invert(1) hue-rotate(180deg) !important;
}
`;
});
// Add excluded elements
config.darkMode.excludeSelectors.forEach(selector => {
css += `
${selector} {
filter: none !important;
}
`;
});
styleElement.textContent = css;
}
// Check and enforce dark mode on new elements
function checkDarkMode() {
// Apply to the whole document
applyDarkMode();
// Special handling for elements that might resist styling
document.querySelectorAll('iframe').forEach(iframe => {
try {
if (iframe.contentDocument) {
const iframeStyle = iframe.contentDocument.createElement('style');
iframeStyle.textContent = `
html, body, * {
background: ${config.darkMode.defaultBackground} !important;
color: ${config.darkMode.defaultText} !important;
}
`;
iframe.contentDocument.head.appendChild(iframeStyle);
}
} catch (e) {
// Cross-origin iframe protection
}
});
}
// ===== Monitoring System =====
function setupMutationObserver() {
const observer = new MutationObserver((mutations) => {
let needsCheck = false;
mutations.forEach(mutation => {
if (mutation.addedNodes.length > 0) {
needsCheck = true;
}
});
if (needsCheck) {
removeAds();
checkDarkMode();
}
});
observer.observe(document.body, {
childList: true,
subtree: true,
attributes: true,
attributeFilter: ['style', 'class']
});
return observer;
}
// ===== Initialization =====
function init() {
// Initial cleanup
removeAds();
checkDarkMode();
// Set up monitoring
let observer;
if (config.mutationObserver) {
observer = setupMutationObserver();
}
const interval = setInterval(() => {
removeAds();
checkDarkMode();
}, config.checkInterval);
// Cleanup function
return function cleanup() {
if (observer) observer.disconnect();
clearInterval(interval);
// Remove dark mode styles
const styleElement = document.getElementById('forced-dark-mode');
if (styleElement) styleElement.remove();
console.log('Cleanup complete - ads and dark mode monitoring stopped');
};
}
// Start the system
const cleanup = init();
// Expose cleanup to window
window.darkAdCleanup = cleanup;
console.log('Dark Mode + Ad Removal script initialized');
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment