Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: lib/contentFiltering.js

Issue 29856567: Issue 6826 - Use contentScripts API for snippets if available Base URL: https://hg.adblockplus.org/adblockpluschrome/
Patch Set: Created Aug. 15, 2018, 2:30 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-present eyeo GmbH 3 * Copyright (C) 2006-present eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
(...skipping 24 matching lines...) Expand all
35 // See https://crbug.com/608854 35 // See https://crbug.com/608854
36 const styleSheetRemovalSupported = info.platform == "gecko"; 36 const styleSheetRemovalSupported = info.platform == "gecko";
37 37
38 const selectorGroupSize = 1024; 38 const selectorGroupSize = 1024;
39 39
40 let userStyleSheetsSupported = true; 40 let userStyleSheetsSupported = true;
41 41
42 let snippetsLibrarySource = ""; 42 let snippetsLibrarySource = "";
43 let executableCode = new Map(); 43 let executableCode = new Map();
44 44
45 let registeredContentScripts = new Map();
46
45 function* splitSelectors(selectors) 47 function* splitSelectors(selectors)
46 { 48 {
47 // Chromium's Blink engine supports only up to 8,192 simple selectors, and 49 // Chromium's Blink engine supports only up to 8,192 simple selectors, and
48 // even fewer compound selectors, in a rule. The exact number of selectors 50 // even fewer compound selectors, in a rule. The exact number of selectors
49 // that would work depends on their sizes (e.g. "#foo .bar" has a size of 2). 51 // that would work depends on their sizes (e.g. "#foo .bar" has a size of 2).
50 // Since we don't know the sizes of the selectors here, we simply split them 52 // Since we don't know the sizes of the selectors here, we simply split them
51 // into groups of 1,024, based on the reasonable assumption that the average 53 // into groups of 1,024, based on the reasonable assumption that the average
52 // selector won't have a size greater than 8. The alternative would be to 54 // selector won't have a size greater than 8. The alternative would be to
53 // calculate the sizes of the selectors and divide them up accordingly, but 55 // calculate the sizes of the selectors and divide them up accordingly, but
54 // this approach is more efficient and has worked well in practice. In theory 56 // this approach is more efficient and has worked well in practice. In theory
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after
222 let inline = !userStyleSheetsSupported; 224 let inline = !userStyleSheetsSupported;
223 225
224 let {elemhide, snippets} = message.filterTypes || 226 let {elemhide, snippets} = message.filterTypes ||
225 {elemhide: true, snippets: true}; 227 {elemhide: true, snippets: true};
226 228
227 if (!checkWhitelisted(sender.page, sender.frame, null, 229 if (!checkWhitelisted(sender.page, sender.frame, null,
228 RegExpFilter.typeMap.DOCUMENT)) 230 RegExpFilter.typeMap.DOCUMENT))
229 { 231 {
230 let hostname = extractHostFromFrame(sender.frame); 232 let hostname = extractHostFromFrame(sender.frame);
231 233
232 if (snippets) 234 if (snippets && !browser.contentScripts)
233 { 235 {
234 for (let script of Snippets.getScriptsForDomain(hostname)) 236 for (let script of Snippets.getScriptsForDomain(hostname))
235 executeScript(script, sender.page.id, sender.frame.id); 237 executeScript(script, sender.page.id, sender.frame.id);
236 } 238 }
237 239
238 if (elemhide && !checkWhitelisted(sender.page, sender.frame, null, 240 if (elemhide && !checkWhitelisted(sender.page, sender.frame, null,
239 RegExpFilter.typeMap.ELEMHIDE)) 241 RegExpFilter.typeMap.ELEMHIDE))
240 { 242 {
241 let specificOnly = checkWhitelisted(sender.page, sender.frame, null, 243 let specificOnly = checkWhitelisted(sender.page, sender.frame, null,
242 RegExpFilter.typeMap.GENERICHIDE); 244 RegExpFilter.typeMap.GENERICHIDE);
(...skipping 29 matching lines...) Expand all
272 updateFrameStyles(sender.page.id, sender.frame.id, message.selectors, 274 updateFrameStyles(sender.page.id, sender.frame.id, message.selectors,
273 message.groupName, message.appendOnly); 275 message.groupName, message.appendOnly);
274 }); 276 });
275 277
276 fetch(browser.extension.getURL("/snippets.js"), {cache: "no-cache"}) 278 fetch(browser.extension.getURL("/snippets.js"), {cache: "no-cache"})
277 .then(response => response.ok ? response.text() : "") 279 .then(response => response.ok ? response.text() : "")
278 .then(text => 280 .then(text =>
279 { 281 {
280 snippetsLibrarySource = text; 282 snippetsLibrarySource = text;
281 }); 283 });
284
285 if (browser.contentScripts)
286 {
287 Snippets.on("snippets.filterAdded", ({script, domains, text}) =>
288 {
289 let details = {
290 js: [{code: getExecutableCode(script)}],
291 allFrames: true,
292 matchAboutBlank: true,
293 runAt: "document_start",
294 matches: []
295 };
296
297 for (let [domain, include] of domains)
298 {
299 if (domain == "")
300 continue;
301
302 if (!include && !details.excludeMatches)
303 details.excludeMatches = [];
304
305 let matches = include ? details.matches : details.excludeMatches;
306
307 matches.push(`http://*.${domain}/*`);
308 matches.push(`https://*.${domain}/*`);
309 }
310
311 browser.contentScripts.register(details).then(contentScript =>
312 {
313 registeredContentScripts.set(text, contentScript);
314 });
315 });
316
317 Snippets.on("snippets.filterRemoved", ({text}) =>
318 {
319 let contentScript = registeredContentScripts.get(text);
320 if (contentScript)
321 {
322 contentScript.unregister();
323 registeredContentScripts.delete(text);
324 }
325 });
326
327 Snippets.on("snippets.filtersCleared", () =>
328 {
329 for (let contentScript of registeredContentScripts.values())
330 contentScript.unregister();
331
332 registeredContentScripts.clear();
333 });
334 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld