File size: 6,173 Bytes
3d7d9b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
(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();
})();