musealpha / src-tauri /resources /scripts /adblock_layer1.js
asdf98's picture
Upload 112 files
3d7d9b5 verified
(function() {
'use strict';
const blocked = new Set(window.__MUSE_BLOCKED_DOMAINS__ || []);
function extractDomain(url) {
try {
const absolute = String(url || '').startsWith('http') ? String(url) : location.origin + String(url || '');
return new URL(absolute).hostname.replace(/^www\./, '');
} catch { return null; }
}
function isDomainBlocked(url) {
if (!url || typeof url !== 'string') return false;
const domain = extractDomain(url);
if (!domain) return false;
const parts = domain.split('.');
for (let i = 0; i < parts.length - 1; i++) {
if (blocked.has(parts.slice(i).join('.'))) return true;
}
return false;
}
function emptyResponseFor(url) {
const type = String(url || '').toLowerCase();
if (type.includes('.json') || type.includes('/api/') || type.includes('youtubei')) {
return new Response('{}', { status: 200, headers: { 'Content-Type': 'application/json' } });
}
if (type.includes('.js')) {
return new Response('/* blocked by Muse Shield */', { status: 200, headers: { 'Content-Type': 'application/javascript' } });
}
return new Response('', { status: 204 });
}
// Patch fetch
const _fetch = window.fetch;
window.fetch = function(input, init) {
const url = typeof input === 'string' ? input : (input instanceof Request ? input.url : '');
if (isDomainBlocked(url)) {
return Promise.resolve(emptyResponseFor(url));
}
return _fetch.apply(this, arguments);
};
// Patch XMLHttpRequest
const _xhrOpen = XMLHttpRequest.prototype.open;
XMLHttpRequest.prototype.open = function(method, url) {
this.__muse_blocked = isDomainBlocked(url);
return _xhrOpen.apply(this, arguments);
};
const _xhrSend = XMLHttpRequest.prototype.send;
XMLHttpRequest.prototype.send = function() {
if (this.__muse_blocked) {
Object.defineProperty(this, 'readyState', { get: () => 4 });
Object.defineProperty(this, 'status', { get: () => 204 });
Object.defineProperty(this, 'responseText', { get: () => '' });
Object.defineProperty(this, 'response', { get: () => '' });
if (typeof this.onload === 'function') setTimeout(() => this.onload(new Event('load')), 0);
if (typeof this.onreadystatechange === 'function') setTimeout(() => this.onreadystatechange(new Event('readystatechange')), 0);
return;
}
return _xhrSend.apply(this, arguments);
};
// Patch WebSocket
const _WS = window.WebSocket;
if (_WS) {
window.WebSocket = function(url, protocols) {
if (isDomainBlocked(url)) {
return { readyState: 3, send() {}, close() {}, addEventListener() {}, removeEventListener() {} };
}
return new _WS(url, protocols);
};
Object.setPrototypeOf(window.WebSocket, _WS);
}
const placeholderSelectors = [
'[id^="google_ads_iframe"]', '[id*="google_ads"]', '[id*="googleads"]',
'[id*="doubleclick"]', '[id*="div-gpt-ad"]', '[id*="gpt_unit"]',
'[class*="ad-slot"]', '[class*="adSlot"]', '[class*="ads-slot"]',
'[class*="advert"]', '[class*="sponsor"]', '[class*="promoted"]',
'[class*="ad-container"]', '[class*="ad_wrapper"]', '[class*="ad-wrapper"]',
'[aria-label*="advertisement" i]', '[data-ad]', '[data-ad-unit]', '[data-ad-slot]',
'iframe[src*="doubleclick"]', 'iframe[src*="googlesyndication"]',
'iframe[src*="adnxs"]', 'iframe[src*="taboola"]', 'iframe[src*="outbrain"]',
'.ytp-ad-module', '.ytp-ad-overlay-container', '#player-ads', '#masthead-ad',
'ytd-ad-slot-renderer', 'ytd-promoted-sparkles-web-renderer', 'ytd-display-ad-renderer',
'ytd-promoted-video-renderer', 'ytd-in-feed-ad-layout-renderer'
];
function hidePlaceholders(root = document) {
try {
for (const sel of placeholderSelectors) {
root.querySelectorAll(sel).forEach(el => {
el.style.setProperty('display', 'none', 'important');
el.style.setProperty('visibility', 'hidden', 'important');
el.setAttribute('data-muse-hidden-ad', 'true');
});
}
// Collapse empty ad-shaped containers
root.querySelectorAll('div, section, aside').forEach(el => {
const id = (el.id || '').toLowerCase();
const cls = (el.className || '').toString().toLowerCase();
if ((/\bads?\b|advert|sponsor|promoted|dfp|gpt|taboola|outbrain/.test(id + ' ' + cls)) && el.offsetHeight < 260) {
el.style.setProperty('display', 'none', 'important');
}
});
} catch (_) {}
}
// Initial cosmetic cleanup
if (document.documentElement) {
const style = document.createElement('style');
style.id = '__muse_generic_cosmetic';
style.textContent = placeholderSelectors.join(',') + '{display:none!important;visibility:hidden!important;min-height:0!important;height:0!important}';
document.documentElement.appendChild(style);
}
// Block tracking images/scripts/iframes added dynamically + cleanup placeholders
const observer = new MutationObserver((mutations) => {
for (const mutation of mutations) {
for (const node of mutation.addedNodes) {
if (node.nodeType !== 1) continue;
if (node.tagName === 'SCRIPT' && node.src && isDomainBlocked(node.src)) { node.remove(); continue; }
if (node.tagName === 'IMG' && node.src && isDomainBlocked(node.src)) {
node.src = 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7';
node.style.display = 'none'; continue;
}
if (node.tagName === 'IFRAME' && node.src && isDomainBlocked(node.src)) { node.remove(); continue; }
hidePlaceholders(node);
}
}
});
function start() {
hidePlaceholders();
observer.observe(document.documentElement, { childList: true, subtree: true });
setInterval(hidePlaceholders, 2000);
}
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', start, { once: true }); else start();
})();