-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent_blocker.js
More file actions
322 lines (278 loc) · 8.72 KB
/
content_blocker.js
File metadata and controls
322 lines (278 loc) · 8.72 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
// content_blocker.js
const PAYWALL_SELECTORS = [
'[id*="paywall"]',
'[class*="paywall"]',
'[id*="piano"]',
'[class*="piano"]',
'[id*="meter"]',
'[class*="meter"]',
'[id*="subscribe"]',
'[class*="subscribe"]',
'[id*="tinypass"]',
'[class*="tinypass"]',
'[data-testid*="paywall"]',
'[aria-label*="subscribe" i]'
].join(", ");
const BOOTSTRAP_SCRIPT_PATTERNS = [
"cdn.tinypass.com/api/tinypass.min.js",
"tinypass",
"tp.piano",
"/piano/",
"experience.piano"
];
const PAYWALL_REQUEST_PATTERNS = [
"paywallbypassmemberdetailsquery",
"memberandsubscriptionsdetailsquery",
"paywallbypass(",
"paywallbypassinput",
"graphql?query=query%20paywall",
"graphql?query=query%20memberandsubscriptions"
];
function ensureBypassStyle() {
if (document.getElementById("popup-blocker-bypass-style")) {
return;
}
const style = document.createElement("style");
style.id = "popup-blocker-bypass-style";
style.textContent = `
html.popup-blocker-unlock,
body.popup-blocker-unlock {
overflow: auto !important;
overflow-y: auto !important;
position: static !important;
height: auto !important;
}
${PAYWALL_SELECTORS} {
display: none !important;
visibility: hidden !important;
opacity: 0 !important;
pointer-events: none !important;
max-height: 0 !important;
}
`;
(document.head || document.documentElement).appendChild(style);
}
function unlockPageScroll() {
if (document.documentElement) {
document.documentElement.classList.add("popup-blocker-unlock");
document.documentElement.style.setProperty("overflow", "auto", "important");
document.documentElement.style.setProperty("overflow-y", "auto", "important");
}
if (document.body) {
document.body.classList.add("popup-blocker-unlock");
document.body.style.setProperty("overflow", "auto", "important");
document.body.style.setProperty("overflow-y", "auto", "important");
document.body.style.setProperty("position", "static", "important");
}
}
function neutralizePaywallElements() {
const candidates = document.querySelectorAll(PAYWALL_SELECTORS);
candidates.forEach((el) => {
el.style.setProperty("display", "none", "important");
el.style.setProperty("visibility", "hidden", "important");
el.style.setProperty("opacity", "0", "important");
el.style.setProperty("pointer-events", "none", "important");
el.style.setProperty("max-height", "0", "important");
});
}
function hasContentShell() {
return Boolean(document.body && (document.querySelector("article") || document.querySelector("main")));
}
function hasBasicShell() {
return Boolean(document.body && document.documentElement);
}
function hasBootstrapScriptInDom() {
const scripts = document.querySelectorAll("script[src]");
for (const script of scripts) {
const src = (script.getAttribute("src") || "").toLowerCase();
if (BOOTSTRAP_SCRIPT_PATTERNS.some((pattern) => src.includes(pattern))) {
return true;
}
}
return false;
}
function hasPaywallSignal() {
if (hasBootstrapScriptInDom()) {
return true;
}
return Boolean(document.querySelector(PAYWALL_SELECTORS));
}
function isBootstrapScriptNode(node) {
if (!node || node.nodeType !== Node.ELEMENT_NODE || node.tagName !== "SCRIPT") {
return false;
}
const src = (node.getAttribute("src") || "").toLowerCase();
if (!src) {
return false;
}
return BOOTSTRAP_SCRIPT_PATTERNS.some((pattern) => src.includes(pattern));
}
function normalizeUrlForMatch(url) {
try {
return decodeURIComponent(String(url || "").toLowerCase());
} catch {
return String(url || "").toLowerCase();
}
}
function shouldBlockPaywallRequest(url) {
const normalized = normalizeUrlForMatch(url);
if (!normalized) {
return false;
}
return PAYWALL_REQUEST_PATTERNS.some((pattern) => normalized.includes(pattern));
}
function installRequestBlockers(currentOrigin) {
if (window.__popupBlockerRequestHooksInstalled) {
return;
}
window.__popupBlockerRequestHooksInstalled = true;
const originalFetch = window.fetch;
if (typeof originalFetch === "function") {
window.fetch = function (...args) {
const request = args[0];
const url = typeof request === "string" ? request : request?.url;
if (shouldBlockPaywallRequest(url)) {
console.log(`[Popup Blocker] Blocked paywall fetch for ${currentOrigin}: ${url}`);
// Keep the caller waiting to avoid triggering fallback paywall routes.
return new Promise(() => {});
}
return originalFetch.apply(this, args);
};
}
const originalOpen = XMLHttpRequest.prototype.open;
const originalSend = XMLHttpRequest.prototype.send;
XMLHttpRequest.prototype.open = function (method, url, ...rest) {
this.__popupBlockerUrl = String(url || "");
this.__popupBlockerShouldBlock = shouldBlockPaywallRequest(this.__popupBlockerUrl);
return originalOpen.call(this, method, url, ...rest);
};
XMLHttpRequest.prototype.send = function (...args) {
if (this.__popupBlockerShouldBlock) {
console.log(`[Popup Blocker] Blocked paywall XHR for ${currentOrigin}: ${this.__popupBlockerUrl}`);
// Intentionally do not send this request.
return;
}
return originalSend.apply(this, args);
};
}
function stopNearPaywallBootstrap(currentOrigin) {
const maxWaitMs = 5000;
const startedAt = Date.now();
let didStop = false;
let sawBootstrapScript = false;
let observerTimer = null;
const stopNow = (reason) => {
if (didStop) {
return;
}
didStop = true;
console.log(`[Popup Blocker] Paywall stop triggered for ${currentOrigin} (${reason})`);
window.stop();
};
const maybeStop = (reason) => {
// Prefer stopping close to paywall bootstrap to avoid both white pages and late 404 overlays.
if (sawBootstrapScript && hasBasicShell()) {
stopNow(reason);
return true;
}
if (Date.now() - startedAt >= maxWaitMs && hasPaywallSignal() && hasContentShell()) {
stopNow("paywall-signal-timeout-fallback");
return true;
}
return false;
};
if (hasBootstrapScriptInDom()) {
sawBootstrapScript = true;
if (maybeStop("bootstrap-script-present-initial")) {
return;
}
}
const signalObserver = new MutationObserver((mutations) => {
if (didStop) {
return;
}
if (observerTimer) {
return;
}
observerTimer = setTimeout(() => {
observerTimer = null;
if (!didStop && hasPaywallSignal()) {
maybeStop("paywall-signal-observed");
}
}, 80);
for (const mutation of mutations) {
for (const node of mutation.addedNodes) {
if (isBootstrapScriptNode(node)) {
sawBootstrapScript = true;
maybeStop("bootstrap-script-added");
return;
}
}
}
});
const observeTarget = document.documentElement || document;
signalObserver.observe(observeTarget, { childList: true, subtree: true });
const tick = () => {
if (didStop) {
signalObserver.disconnect();
return;
}
if (maybeStop("poll")) {
signalObserver.disconnect();
return;
}
if (Date.now() - startedAt >= maxWaitMs + 500) {
signalObserver.disconnect();
return;
}
setTimeout(tick, 60);
};
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", () => {
if (!didStop && hasPaywallSignal()) {
maybeStop("dom-content-loaded");
}
}, { once: true });
}
tick();
}
function startBlockedSiteHandling(currentOrigin) {
const initialStopDeadlineMs = Date.now() + 1200;
const initialStop = () => {
if (document.body || Date.now() >= initialStopDeadlineMs) {
console.log(`[Popup Blocker] Initial early stop for ${currentOrigin}`);
window.stop();
return;
}
setTimeout(initialStop, 40);
};
initialStop();
installRequestBlockers(currentOrigin);
ensureBypassStyle();
unlockPageScroll();
neutralizePaywallElements();
// Keep neutralizing late-injected overlays without triggering mutation feedback loops.
let timer = null;
const stopObservingAfterMs = 15000;
const observer = new MutationObserver(() => {
if (timer) {
return;
}
timer = setTimeout(() => {
timer = null;
unlockPageScroll();
neutralizePaywallElements();
}, 80);
});
const observerTarget = document.documentElement || document;
observer.observe(observerTarget, { childList: true, subtree: true });
setTimeout(() => observer.disconnect(), stopObservingAfterMs);
stopNearPaywallBootstrap(currentOrigin);
}
chrome.storage.sync.get({ blockedSites: [] }, (data) => {
const blockedSites = data.blockedSites;
const currentOrigin = window.location.origin;
if (blockedSites.includes(currentOrigin)) {
startBlockedSiteHandling(currentOrigin);
}
});