OLD | NEW |
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 |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
13 * | 13 * |
14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
16 */ | 16 */ |
17 | 17 |
18 /** @module contentFiltering */ | 18 /** @module contentFiltering */ |
19 | 19 |
20 "use strict"; | 20 "use strict"; |
21 | 21 |
22 const {RegExpFilter} = require("../adblockpluscore/lib/filterClasses"); | 22 const {RegExpFilter} = require("../adblockpluscore/lib/filterClasses"); |
23 const {ElemHide} = require("../adblockpluscore/lib/elemHide"); | 23 const {ElemHide, createStyleSheet} = require("../adblockpluscore/lib/elemHide"); |
24 const {ElemHideEmulation} = require("../adblockpluscore/lib/elemHideEmulation"); | 24 const {ElemHideEmulation} = require("../adblockpluscore/lib/elemHideEmulation"); |
25 const {filterNotifier} = require("../adblockpluscore/lib/filterNotifier"); | 25 const {filterNotifier} = require("../adblockpluscore/lib/filterNotifier"); |
26 const {Snippets, compileScript} = require("../adblockpluscore/lib/snippets"); | 26 const {Snippets, compileScript} = require("../adblockpluscore/lib/snippets"); |
27 const {checkWhitelisted} = require("./whitelisting"); | 27 const {checkWhitelisted} = require("./whitelisting"); |
28 const {extractHostFromFrame} = require("./url"); | 28 const {extractHostFromFrame} = require("./url"); |
29 const {port} = require("./messaging"); | 29 const {port} = require("./messaging"); |
30 const {HitLogger, logRequest} = require("./hitLogger"); | 30 const {HitLogger, logRequest} = require("./hitLogger"); |
31 const info = require("info"); | 31 const info = require("info"); |
32 | 32 |
33 // Chromium's support for tabs.removeCSS is still a work in progress and the | 33 // Chromium's support for tabs.removeCSS is still a work in progress and the |
34 // API is likely to be different from Firefox's; for now we just don't use it | 34 // API is likely to be different from Firefox's; for now we just don't use it |
35 // at all, even if it's available. | 35 // at all, even if it's available. |
36 // See https://crbug.com/608854 | 36 // See https://crbug.com/608854 |
37 const styleSheetRemovalSupported = info.platform == "gecko"; | 37 const styleSheetRemovalSupported = info.platform == "gecko"; |
38 | 38 |
39 const selectorGroupSize = 1024; | 39 const selectorGroupSize = 1024; |
40 | 40 |
41 let userStyleSheetsSupported = true; | 41 let userStyleSheetsSupported = true; |
42 | 42 |
43 let snippetsLibrarySource = ""; | 43 let snippetsLibrarySource = ""; |
44 let executableCode = new Map(); | 44 let executableCode = new Map(); |
45 | 45 |
46 function* splitSelectors(selectors) | |
47 { | |
48 // Chromium's Blink engine supports only up to 8,192 simple selectors, and | |
49 // even fewer compound selectors, in a rule. The exact number of selectors | |
50 // that would work depends on their sizes (e.g. "#foo .bar" has a size of 2). | |
51 // Since we don't know the sizes of the selectors here, we simply split them | |
52 // into groups of 1,024, based on the reasonable assumption that the average | |
53 // selector won't have a size greater than 8. The alternative would be to | |
54 // calculate the sizes of the selectors and divide them up accordingly, but | |
55 // this approach is more efficient and has worked well in practice. In theory | |
56 // this could still lead to some selectors not working on Chromium, but it is | |
57 // highly unlikely. | |
58 // See issue #6298 and https://crbug.com/804179 | |
59 for (let i = 0; i < selectors.length; i += selectorGroupSize) | |
60 yield selectors.slice(i, i + selectorGroupSize); | |
61 } | |
62 | |
63 function* createRules(selectors) | |
64 { | |
65 for (let selectorGroup of splitSelectors(selectors)) | |
66 yield selectorGroup.join(", ") + " {display: none !important;}"; | |
67 } | |
68 | |
69 function createStyleSheet(selectors) | |
70 { | |
71 return Array.from(createRules(selectors)).join("\n"); | |
72 } | |
73 | |
74 function addStyleSheet(tabId, frameId, styleSheet) | 46 function addStyleSheet(tabId, frameId, styleSheet) |
75 { | 47 { |
76 try | 48 try |
77 { | 49 { |
78 let promise = browser.tabs.insertCSS(tabId, { | 50 let promise = browser.tabs.insertCSS(tabId, { |
79 code: styleSheet, | 51 code: styleSheet, |
80 cssOrigin: "user", | 52 cssOrigin: "user", |
81 frameId, | 53 frameId, |
82 matchAboutBlank: true, | 54 matchAboutBlank: true, |
83 runAt: "document_start" | 55 runAt: "document_start" |
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
293 updateFrameStyles(sender.page.id, sender.frame.id, message.selectors, | 265 updateFrameStyles(sender.page.id, sender.frame.id, message.selectors, |
294 message.groupName, message.appendOnly); | 266 message.groupName, message.appendOnly); |
295 }); | 267 }); |
296 | 268 |
297 fetch(browser.extension.getURL("/snippets.js"), {cache: "no-cache"}) | 269 fetch(browser.extension.getURL("/snippets.js"), {cache: "no-cache"}) |
298 .then(response => response.ok ? response.text() : "") | 270 .then(response => response.ok ? response.text() : "") |
299 .then(text => | 271 .then(text => |
300 { | 272 { |
301 snippetsLibrarySource = text; | 273 snippetsLibrarySource = text; |
302 }); | 274 }); |
OLD | NEW |