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

Side by Side Diff: lib/cssInjection.js

Issue 29564767: Issue 242 - Use user style sheets on Chromium (Closed) Base URL: https://hg.adblockplus.org/adblockpluschrome/
Patch Set: Check for Gecko Created Jan. 31, 2018, 12:35 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 | « include.preload.js ('k') | 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
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 cssInjection */ 18 /** @module cssInjection */
19 19
20 "use strict"; 20 "use strict";
21 21
22 const {RegExpFilter} = require("filterClasses"); 22 const {RegExpFilter} = require("filterClasses");
23 const {ElemHide} = require("elemHide"); 23 const {ElemHide} = require("elemHide");
24 const {ElemHideEmulation} = require("elemHideEmulation"); 24 const {ElemHideEmulation} = require("elemHideEmulation");
25 const {checkWhitelisted} = require("whitelisting"); 25 const {checkWhitelisted} = require("whitelisting");
26 const {extractHostFromFrame} = require("url"); 26 const {extractHostFromFrame} = require("url");
27 const {port} = require("messaging"); 27 const {port} = require("messaging");
28 const devtools = require("devtools"); 28 const devtools = require("devtools");
29 const info = require("info");
29 30
30 const userStyleSheetsSupported = "extensionTypes" in browser && 31 const userStyleSheetsSupported = ("extensionTypes" in browser &&
31 "CSSOrigin" in browser.extensionTypes; 32 "CSSOrigin" in browser.extensionTypes) ||
33 (info.platform == "chromium" &&
34 parseInt(info.platformVersion, 10) >= 66);
35
36 // Chromium's support for tabs.removeCSS is still a work in progress and the
37 // API is likely to be different from Firefox's; for now we just don't use it
38 // at all, even if it's available.
39 // See https://crbug.com/608854
40 const styleSheetRemovalSupported = "removeCSS" in browser.tabs &&
41 info.platform == "gecko";
42
32 const selectorGroupSize = 1024; 43 const selectorGroupSize = 1024;
33 44
34 function* splitSelectors(selectors) 45 function* splitSelectors(selectors)
35 { 46 {
36 // Chromium's Blink engine supports only up to 8,192 simple selectors, and 47 // Chromium's Blink engine supports only up to 8,192 simple selectors, and
37 // even fewer compound selectors, in a rule. The exact number of selectors 48 // even fewer compound selectors, in a rule. The exact number of selectors
38 // that would work depends on their sizes (e.g. "#foo .bar" has a size of 2). 49 // that would work depends on their sizes (e.g. "#foo .bar" has a size of 2).
39 // Since we don't know the sizes of the selectors here, we simply split them 50 // Since we don't know the sizes of the selectors here, we simply split them
40 // into groups of 1,024, based on the reasonable assumption that the average 51 // into groups of 1,024, based on the reasonable assumption that the average
41 // selector won't have a size greater than 8. The alternative would be to 52 // selector won't have a size greater than 8. The alternative would be to
(...skipping 23 matching lines...) Expand all
65 code: styleSheet, 76 code: styleSheet,
66 cssOrigin: "user", 77 cssOrigin: "user",
67 frameId, 78 frameId,
68 matchAboutBlank: true, 79 matchAboutBlank: true,
69 runAt: "document_start" 80 runAt: "document_start"
70 }); 81 });
71 } 82 }
72 83
73 function removeStyleSheet(tabId, frameId, styleSheet) 84 function removeStyleSheet(tabId, frameId, styleSheet)
74 { 85 {
86 if (!styleSheetRemovalSupported)
87 return;
88
75 browser.tabs.removeCSS(tabId, { 89 browser.tabs.removeCSS(tabId, {
76 code: styleSheet, 90 code: styleSheet,
77 cssOrigin: "user", 91 cssOrigin: "user",
78 frameId, 92 frameId,
79 matchAboutBlank: true 93 matchAboutBlank: true
80 }); 94 });
81 } 95 }
82 96
83 function updateFrameStyles(tabId, frameId, selectors, groupName) 97 function updateFrameStyles(tabId, frameId, selectors, groupName)
84 { 98 {
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
135 emulatedPatterns.push({selector: filter.selector, text: filter.text}); 149 emulatedPatterns.push({selector: filter.selector, text: filter.text});
136 } 150 }
137 151
138 if (!inline) 152 if (!inline)
139 updateFrameStyles(sender.page.id, sender.frame.id, selectors); 153 updateFrameStyles(sender.page.id, sender.frame.id, selectors);
140 154
141 let response = {trace, inline, emulatedPatterns}; 155 let response = {trace, inline, emulatedPatterns};
142 if (trace || inline) 156 if (trace || inline)
143 response.selectors = selectors; 157 response.selectors = selectors;
144 158
159 // If we can't remove user style sheets using tabs.removeCSS, we'll only keep
160 // adding them, which could cause problems with emulated filters as described
161 // in issue #5864. Instead, we can just ask the content script to add styles
162 // for emulated filters inline.
163 if (!styleSheetRemovalSupported)
164 response.inlineEmulated = true;
165
145 return response; 166 return response;
146 }); 167 });
147 168
148 port.on("elemhide.injectSelectors", (message, sender) => 169 port.on("elemhide.injectSelectors", (message, sender) =>
149 { 170 {
150 updateFrameStyles(sender.page.id, sender.frame.id, message.selectors, 171 updateFrameStyles(sender.page.id, sender.frame.id, message.selectors,
151 message.groupName); 172 message.groupName);
152 }); 173 });
OLDNEW
« no previous file with comments | « include.preload.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld