asdf98 commited on
Commit
c4b3d73
·
verified ·
1 Parent(s): 59d550a

fix: rename remaining Muse Shield globals/markers in injected adblock script

Browse files
src-tauri/resources/scripts/adblock_layer1.js CHANGED
@@ -1,141 +1,138 @@
1
- (function() {
2
- 'use strict';
3
-
4
- const blocked = new Set(window.__MUSE_BLOCKED_DOMAINS__ || []);
5
-
6
- function extractDomain(url) {
7
- try {
8
- const absolute = String(url || '').startsWith('http') ? String(url) : location.origin + String(url || '');
9
- return new URL(absolute).hostname.replace(/^www\./, '');
10
- } catch { return null; }
11
- }
12
-
13
- function isDomainBlocked(url) {
14
- if (!url || typeof url !== 'string') return false;
15
- const domain = extractDomain(url);
16
- if (!domain) return false;
17
- const parts = domain.split('.');
18
- for (let i = 0; i < parts.length - 1; i++) {
19
- if (blocked.has(parts.slice(i).join('.'))) return true;
20
- }
21
- return false;
22
- }
23
-
24
- function emptyResponseFor(url) {
25
- const type = String(url || '').toLowerCase();
26
- if (type.includes('.json') || type.includes('/api/') || type.includes('youtubei')) {
27
- return new Response('{}', { status: 200, headers: { 'Content-Type': 'application/json' } });
28
- }
29
- if (type.includes('.js')) {
30
- return new Response('/* blocked by Muse Shield */', { status: 200, headers: { 'Content-Type': 'application/javascript' } });
31
- }
32
- return new Response('', { status: 204 });
33
- }
34
-
35
- // Patch fetch
36
- const _fetch = window.fetch;
37
- window.fetch = function(input, init) {
38
- const url = typeof input === 'string' ? input : (input instanceof Request ? input.url : '');
39
- if (isDomainBlocked(url)) {
40
- return Promise.resolve(emptyResponseFor(url));
41
- }
42
- return _fetch.apply(this, arguments);
43
- };
44
-
45
- // Patch XMLHttpRequest
46
- const _xhrOpen = XMLHttpRequest.prototype.open;
47
- XMLHttpRequest.prototype.open = function(method, url) {
48
- this.__muse_blocked = isDomainBlocked(url);
49
- return _xhrOpen.apply(this, arguments);
50
- };
51
- const _xhrSend = XMLHttpRequest.prototype.send;
52
- XMLHttpRequest.prototype.send = function() {
53
- if (this.__muse_blocked) {
54
- Object.defineProperty(this, 'readyState', { get: () => 4 });
55
- Object.defineProperty(this, 'status', { get: () => 204 });
56
- Object.defineProperty(this, 'responseText', { get: () => '' });
57
- Object.defineProperty(this, 'response', { get: () => '' });
58
- if (typeof this.onload === 'function') setTimeout(() => this.onload(new Event('load')), 0);
59
- if (typeof this.onreadystatechange === 'function') setTimeout(() => this.onreadystatechange(new Event('readystatechange')), 0);
60
- return;
61
- }
62
- return _xhrSend.apply(this, arguments);
63
- };
64
-
65
- // Patch WebSocket
66
- const _WS = window.WebSocket;
67
- if (_WS) {
68
- window.WebSocket = function(url, protocols) {
69
- if (isDomainBlocked(url)) {
70
- return { readyState: 3, send() {}, close() {}, addEventListener() {}, removeEventListener() {} };
71
- }
72
- return new _WS(url, protocols);
73
- };
74
- Object.setPrototypeOf(window.WebSocket, _WS);
75
- }
76
-
77
- const placeholderSelectors = [
78
- '[id^="google_ads_iframe"]', '[id*="google_ads"]', '[id*="googleads"]',
79
- '[id*="doubleclick"]', '[id*="div-gpt-ad"]', '[id*="gpt_unit"]',
80
- '[class*="ad-slot"]', '[class*="adSlot"]', '[class*="ads-slot"]',
81
- '[class*="advert"]', '[class*="sponsor"]', '[class*="promoted"]',
82
- '[class*="ad-container"]', '[class*="ad_wrapper"]', '[class*="ad-wrapper"]',
83
- '[aria-label*="advertisement" i]', '[data-ad]', '[data-ad-unit]', '[data-ad-slot]',
84
- 'iframe[src*="doubleclick"]', 'iframe[src*="googlesyndication"]',
85
- 'iframe[src*="adnxs"]', 'iframe[src*="taboola"]', 'iframe[src*="outbrain"]',
86
- '.ytp-ad-module', '.ytp-ad-overlay-container', '#player-ads', '#masthead-ad',
87
- 'ytd-ad-slot-renderer', 'ytd-promoted-sparkles-web-renderer', 'ytd-display-ad-renderer',
88
- 'ytd-promoted-video-renderer', 'ytd-in-feed-ad-layout-renderer'
89
- ];
90
-
91
- function hidePlaceholders(root = document) {
92
- try {
93
- for (const sel of placeholderSelectors) {
94
- root.querySelectorAll(sel).forEach(el => {
95
- el.style.setProperty('display', 'none', 'important');
96
- el.style.setProperty('visibility', 'hidden', 'important');
97
- el.setAttribute('data-muse-hidden-ad', 'true');
98
- });
99
- }
100
- // Collapse empty ad-shaped containers
101
- root.querySelectorAll('div, section, aside').forEach(el => {
102
- const id = (el.id || '').toLowerCase();
103
- const cls = (el.className || '').toString().toLowerCase();
104
- if ((/\bads?\b|advert|sponsor|promoted|dfp|gpt|taboola|outbrain/.test(id + ' ' + cls)) && el.offsetHeight < 260) {
105
- el.style.setProperty('display', 'none', 'important');
106
- }
107
- });
108
- } catch (_) {}
109
- }
110
-
111
- // Initial cosmetic cleanup
112
- if (document.documentElement) {
113
- const style = document.createElement('style');
114
- style.id = '__muse_generic_cosmetic';
115
- style.textContent = placeholderSelectors.join(',') + '{display:none!important;visibility:hidden!important;min-height:0!important;height:0!important}';
116
- document.documentElement.appendChild(style);
117
- }
118
-
119
- // Block tracking images/scripts/iframes added dynamically + cleanup placeholders
120
- const observer = new MutationObserver((mutations) => {
121
- for (const mutation of mutations) {
122
- for (const node of mutation.addedNodes) {
123
- if (node.nodeType !== 1) continue;
124
- if (node.tagName === 'SCRIPT' && node.src && isDomainBlocked(node.src)) { node.remove(); continue; }
125
- if (node.tagName === 'IMG' && node.src && isDomainBlocked(node.src)) {
126
- node.src = 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7';
127
- node.style.display = 'none'; continue;
128
- }
129
- if (node.tagName === 'IFRAME' && node.src && isDomainBlocked(node.src)) { node.remove(); continue; }
130
- hidePlaceholders(node);
131
- }
132
- }
133
- });
134
-
135
- function start() {
136
- hidePlaceholders();
137
- observer.observe(document.documentElement, { childList: true, subtree: true });
138
- setInterval(hidePlaceholders, 2000);
139
- }
140
- if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', start, { once: true }); else start();
141
- })();
 
1
+ (function() {
2
+ 'use strict';
3
+
4
+ const blocked = new Set(window.__LUMAREF_BLOCKED_DOMAINS__ || []);
5
+
6
+ function extractDomain(url) {
7
+ try {
8
+ const absolute = String(url || '').startsWith('http') ? String(url) : location.origin + String(url || '');
9
+ return new URL(absolute).hostname.replace(/^www\./, '');
10
+ } catch { return null; }
11
+ }
12
+
13
+ function isDomainBlocked(url) {
14
+ if (!url || typeof url !== 'string') return false;
15
+ const domain = extractDomain(url);
16
+ if (!domain) return false;
17
+ const parts = domain.split('.');
18
+ for (let i = 0; i < parts.length - 1; i++) {
19
+ if (blocked.has(parts.slice(i).join('.'))) return true;
20
+ }
21
+ return false;
22
+ }
23
+
24
+ function emptyResponseFor(url) {
25
+ const type = String(url || '').toLowerCase();
26
+ if (type.includes('.json') || type.includes('/api/') || type.includes('youtubei')) {
27
+ return new Response('{}', { status: 200, headers: { 'Content-Type': 'application/json' } });
28
+ }
29
+ if (type.includes('.js')) {
30
+ return new Response('/* blocked by LumaRef Shield */', { status: 200, headers: { 'Content-Type': 'application/javascript' } });
31
+ }
32
+ return new Response('', { status: 204 });
33
+ }
34
+
35
+ // Patch fetch
36
+ const _fetch = window.fetch;
37
+ window.fetch = function(input, init) {
38
+ const url = typeof input === 'string' ? input : (input instanceof Request ? input.url : '');
39
+ if (isDomainBlocked(url)) {
40
+ return Promise.resolve(emptyResponseFor(url));
41
+ }
42
+ return _fetch.apply(this, arguments);
43
+ };
44
+
45
+ // Patch XMLHttpRequest
46
+ const _xhrOpen = XMLHttpRequest.prototype.open;
47
+ XMLHttpRequest.prototype.open = function(method, url) {
48
+ this.__lumaref_blocked = isDomainBlocked(url);
49
+ return _xhrOpen.apply(this, arguments);
50
+ };
51
+ const _xhrSend = XMLHttpRequest.prototype.send;
52
+ XMLHttpRequest.prototype.send = function() {
53
+ if (this.__lumaref_blocked) {
54
+ Object.defineProperty(this, 'readyState', { get: () => 4 });
55
+ Object.defineProperty(this, 'status', { get: () => 204 });
56
+ Object.defineProperty(this, 'responseText', { get: () => '' });
57
+ Object.defineProperty(this, 'response', { get: () => '' });
58
+ if (typeof this.onload === 'function') setTimeout(() => this.onload(new Event('load')), 0);
59
+ if (typeof this.onreadystatechange === 'function') setTimeout(() => this.onreadystatechange(new Event('readystatechange')), 0);
60
+ return;
61
+ }
62
+ return _xhrSend.apply(this, arguments);
63
+ };
64
+
65
+ // Patch WebSocket
66
+ const _WS = window.WebSocket;
67
+ if (_WS) {
68
+ window.WebSocket = function(url, protocols) {
69
+ if (isDomainBlocked(url)) {
70
+ return { readyState: 3, send() {}, close() {}, addEventListener() {}, removeEventListener() {} };
71
+ }
72
+ return new _WS(url, protocols);
73
+ };
74
+ Object.setPrototypeOf(window.WebSocket, _WS);
75
+ }
76
+
77
+ const placeholderSelectors = [
78
+ '[id^="google_ads_iframe"]', '[id*="google_ads"]', '[id*="googleads"]',
79
+ '[id*="doubleclick"]', '[id*="div-gpt-ad"]', '[id*="gpt_unit"]',
80
+ '[class*="ad-slot"]', '[class*="adSlot"]', '[class*="ads-slot"]',
81
+ '[class*="advert"]', '[class*="sponsor"]', '[class*="promoted"]',
82
+ '[class*="ad-container"]', '[class*="ad_wrapper"]', '[class*="ad-wrapper"]',
83
+ '[aria-label*="advertisement" i]', '[data-ad]', '[data-ad-unit]', '[data-ad-slot]',
84
+ 'iframe[src*="doubleclick"]', 'iframe[src*="googlesyndication"]',
85
+ 'iframe[src*="adnxs"]', 'iframe[src*="taboola"]', 'iframe[src*="outbrain"]',
86
+ '.ytp-ad-module', '.ytp-ad-overlay-container', '#player-ads', '#masthead-ad',
87
+ 'ytd-ad-slot-renderer', 'ytd-promoted-sparkles-web-renderer', 'ytd-display-ad-renderer',
88
+ 'ytd-promoted-video-renderer', 'ytd-in-feed-ad-layout-renderer'
89
+ ];
90
+
91
+ function hidePlaceholders(root = document) {
92
+ try {
93
+ for (const sel of placeholderSelectors) {
94
+ root.querySelectorAll(sel).forEach(el => {
95
+ el.style.setProperty('display', 'none', 'important');
96
+ el.style.setProperty('visibility', 'hidden', 'important');
97
+ el.setAttribute('data-lumaref-hidden-ad', 'true');
98
+ });
99
+ }
100
+ root.querySelectorAll('div, section, aside').forEach(el => {
101
+ const id = (el.id || '').toLowerCase();
102
+ const cls = (el.className || '').toString().toLowerCase();
103
+ if ((/\bads?\b|advert|sponsor|promoted|dfp|gpt|taboola|outbrain/.test(id + ' ' + cls)) && el.offsetHeight < 260) {
104
+ el.style.setProperty('display', 'none', 'important');
105
+ }
106
+ });
107
+ } catch (_) {}
108
+ }
109
+
110
+ if (document.documentElement) {
111
+ const style = document.createElement('style');
112
+ style.id = '__lumaref_generic_cosmetic';
113
+ style.textContent = placeholderSelectors.join(',') + '{display:none!important;visibility:hidden!important;min-height:0!important;height:0!important}';
114
+ document.documentElement.appendChild(style);
115
+ }
116
+
117
+ const observer = new MutationObserver((mutations) => {
118
+ for (const mutation of mutations) {
119
+ for (const node of mutation.addedNodes) {
120
+ if (node.nodeType !== 1) continue;
121
+ if (node.tagName === 'SCRIPT' && node.src && isDomainBlocked(node.src)) { node.remove(); continue; }
122
+ if (node.tagName === 'IMG' && node.src && isDomainBlocked(node.src)) {
123
+ node.src = 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7';
124
+ node.style.display = 'none'; continue;
125
+ }
126
+ if (node.tagName === 'IFRAME' && node.src && isDomainBlocked(node.src)) { node.remove(); continue; }
127
+ hidePlaceholders(node);
128
+ }
129
+ }
130
+ });
131
+
132
+ function start() {
133
+ hidePlaceholders();
134
+ observer.observe(document.documentElement, { childList: true, subtree: true });
135
+ setInterval(hidePlaceholders, 2000);
136
+ }
137
+ if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', start, { once: true }); else start();
138
+ })();