-
Notifications
You must be signed in to change notification settings - Fork 139
/
adblocker.js
405 lines (350 loc) · 10.7 KB
/
adblocker.js
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/**
* Ghostery Browser Extension
* https://www.ghostery.com/
*
* Copyright 2017-present Ghostery GmbH. All rights reserved.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0
*/
import {
filterRequestHTML,
updateResponseHeadersWithCSP,
} from '@cliqz/adblocker-webextension';
import { parse } from 'tldts-experimental';
import { observe, ENGINES } from '/store/options.js';
import * as engines from '/utils/engines.js';
import Request from './utils/request.js';
import asyncSetup from './utils/setup.js';
import { updateTabStats } from './stats.js';
let enabledEngines = [];
let pausedDomains = [];
const setup = asyncSetup([
observe(null, (options) => {
enabledEngines = [
// Add custom engine
engines.CUSTOM_ENGINE,
// Set enabled engines
...ENGINES.filter(({ key }) => options.terms && options[key]).map(
({ name }) => name,
),
];
// Set paused domains
pausedDomains = options.paused ? options.paused.map(String) : [];
}),
engines.init(engines.CUSTOM_ENGINE),
ENGINES.map(({ name }) => engines.init(name)),
]);
function adblockerInjectStylesWebExtension(
styles,
{ tabId, frameId, allFrames = false },
) {
// Abort if stylesheet is empty.
if (styles.length === 0) {
return;
}
if (chrome.scripting && chrome.scripting.insertCSS) {
const target = {
tabId,
};
if (frameId) {
target.frameIds = [frameId];
} else {
target.allFrames = allFrames;
}
chrome.scripting
.insertCSS({
css: styles,
origin: 'USER',
target,
})
.catch((e) => console.warn('Failed to inject CSS', e));
} else {
const details = {
allFrames,
code: styles,
cssOrigin: 'user',
matchAboutBlank: true,
runAt: 'document_start',
};
if (frameId) {
details.frameId = frameId;
}
chrome.tabs
.insertCSS(tabId, details)
.catch((e) => console.warn('Failed to inject CSS', e));
}
}
// copied from https://github.com/cliqz-oss/adblocker/blob/0bdff8559f1c19effe278b8982fb8b6c33c9c0ab/packages/adblocker-webextension/adblocker.ts#L297
async function adblockerOnMessage(msg, sender) {
// Extract hostname from sender's URL
const { url = '', frameId } = sender;
const parsed = parse(url);
const hostname = parsed.hostname || '';
const domain = parsed.domain || '';
if (!sender.tab) {
return;
}
try {
setup.pending && (await setup.pending);
} catch (e) {
console.error(`Error while setup adblocker filters: ${e}`);
return;
}
if (pausedDomains.includes(domain) || pausedDomains.includes(hostname)) {
return;
}
const genericStyles = [];
const specificStyles = [];
let specificFrameId = null;
enabledEngines.forEach((name) => {
const engine = engines.get(name);
if (!engine) return;
// Once per tab/page load we inject base stylesheets. These are always
// the same for all frames of a given page because they do not depend on
// a particular domain and cannot be cancelled using unhide rules.
// Because of this, we specify `allFrames: true` when injecting them so
// that we do not need to perform this operation for sub-frames.
if (frameId === 0 && msg.lifecycle === 'start') {
const { active, styles } = engine.getCosmeticsFilters({
domain,
hostname,
url,
classes: msg.classes,
hrefs: msg.hrefs,
ids: msg.ids,
// This needs to be done only once per tab
getBaseRules: true,
getInjectionRules: false,
getExtendedRules: false,
getRulesFromDOM: false,
getRulesFromHostname: false,
});
if (active === false) {
return;
}
genericStyles.push(styles);
}
// Separately, requests cosmetics which depend on the page it self
// (either because of the hostname or content of the DOM). Content script
// logic is responsible for returning information about lists of classes,
// ids and hrefs observed in the DOM. MutationObserver is also used to
// make sure we can react to changes.
{
const { active, styles } = engine.getCosmeticsFilters({
domain,
hostname,
url,
classes: msg.classes,
hrefs: msg.hrefs,
ids: msg.ids,
// This needs to be done only once per frame
getBaseRules: false,
getInjectionRules: msg.lifecycle === 'start',
getExtendedRules: msg.lifecycle === 'start',
getRulesFromHostname: msg.lifecycle === 'start',
// This will be done every time we get information about DOM mutation
getRulesFromDOM: msg.lifecycle === 'dom-update',
});
if (active === false) {
return;
}
specificStyles.push(styles);
specificFrameId = frameId;
}
});
if (genericStyles.length > 0) {
adblockerInjectStylesWebExtension(genericStyles.join('\n'), {
tabId: sender.tab.id,
allFrames: true,
});
}
if (specificStyles.length > 0) {
adblockerInjectStylesWebExtension(specificStyles.join('\n'), {
tabId: sender.tab.id,
frameId: specificFrameId,
});
}
}
chrome.runtime.onMessage.addListener((msg, sender) => {
if (msg.action === 'getCosmeticsFilters') {
adblockerOnMessage(msg, sender).catch((e) =>
console.error(`Error while processing cosmetics filters: ${e}`),
);
}
return false;
});
const DEBUG_SCRIPLETS = false;
async function executeScriptlets(tabId, scripts) {
// Dynamically injected scripts can be difficult to find later in
// the debugger. Console logs simplifies setting up breakpoints if needed.
let debugMarker;
if (DEBUG_SCRIPLETS) {
debugMarker = (text) =>
`console.log('[ADBLOCKER-DEBUG]:', ${JSON.stringify(text)});`;
} else {
debugMarker = () => '';
}
// the scriptlet code that contains patches for the website
const codeRunningInPage = `(function(){
${debugMarker('run scriptlets (executing in "page world")')}
${scripts.join('\n\n')}}
)()`;
// wrapper to break the "isolated world" so that the patching operates
// on the website, not on the content script's isolated environment.
function codeRunningInContentScript(code) {
var script;
try {
script = document.createElement('script');
script.appendChild(document.createTextNode(decodeURIComponent(code)));
(document.head || document.documentElement).appendChild(script);
} catch (ex) {
console.error('Failed to run script', ex);
}
if (script) {
if (script.parentNode) {
script.parentNode.removeChild(script);
}
script.textContent = '';
}
}
chrome.scripting.executeScript(
{
injectImmediately: true,
world: __PLATFORM__ === 'firefox' ? undefined : 'MAIN',
target: {
tabId,
allFrames: true,
},
func: codeRunningInContentScript,
args: [encodeURIComponent(codeRunningInPage)],
},
() => {
if (chrome.runtime.lastError) {
console.warn(chrome.runtime.lastError);
}
},
);
}
async function injectScriptlets(tabId, url) {
const { hostname, domain } = parse(url);
if (!hostname) {
return;
}
try {
setup.pending && (await setup.pending);
} catch (e) {
console.error(`Error while setup adblocker filters: ${e}`);
return;
}
if (pausedDomains.includes(domain) || pausedDomains.includes(hostname)) {
return;
}
enabledEngines.forEach((name) => {
const engine = engines.get(name);
if (!engine) return;
const { active, scripts } = engine.getCosmeticsFilters({
url: url,
hostname,
domain: domain || '',
getBaseRules: false,
getInjectionRules: true,
getExtendedRules: false,
getRulesFromDOM: false,
getRulesFromHostname: true,
});
if (active === false) {
return;
}
if (scripts.length > 0) {
executeScriptlets(tabId, scripts);
}
});
}
if (__PLATFORM__ === 'safari') {
chrome.runtime.onMessage.addListener((msg, sender) => {
if (sender.url && msg.action === 'injectScriptlets') {
injectScriptlets(sender.tab.id, sender.url);
}
return false;
});
} else {
chrome.webNavigation.onCommitted.addListener(async (details) => {
injectScriptlets(details.tabId, details.url);
});
}
function isPaused(details, request) {
if (details.frameAncestors?.length > 0) {
for (const { url } of details.frameAncestors) {
const { domain, hostname } = parse(url);
if (pausedDomains.includes(domain || hostname)) {
return true;
}
}
}
if (pausedDomains.includes(request.sourceDomain || request.sourceHostname)) {
return true;
}
return false;
}
if (__PLATFORM__ === 'firefox') {
chrome.webRequest.onBeforeRequest.addListener(
(details) => {
if (details.tabId < 0) return;
const request = Request.fromRequestDetails(details);
// INFO: request.source... is only available in Firefox
if (request.sourceDomain || request.sourceHostname) {
if (details.type !== 'main_frame') {
updateTabStats(details.tabId, [request]);
}
if (isPaused(details, request)) return;
for (const name of enabledEngines) {
const engine = engines.get(name);
if (!engine) continue;
if (details.type === 'main_frame') {
const htmlFilters = engine.getHtmlFilters(request);
if (htmlFilters.length !== 0) {
filterRequestHTML(
chrome.webRequest.filterResponseData,
request,
htmlFilters,
);
return;
}
} else {
const { redirect, match } = engine.match(request);
if (redirect !== undefined) {
request.blocked = true;
return { redirectUrl: redirect.dataUrl };
} else if (match === true) {
request.blocked = true;
return { cancel: true };
}
}
}
}
},
{ urls: ['<all_urls>'] },
['blocking'],
);
chrome.webRequest.onHeadersReceived.addListener(
(details) => {
const request = Request.fromRequestDetails(details);
if (isPaused(details, request)) return;
let policies;
for (const name of enabledEngines) {
const engine = engines.get(name);
if (engine) {
policies = engine.getCSPDirectives(request);
if (policies !== undefined) {
break;
}
}
}
return updateResponseHeadersWithCSP(details, policies);
},
{ urls: ['<all_urls>'], types: ['main_frame'] },
['blocking', 'responseHeaders'],
);
}