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: Do not check error message Created Feb. 1, 2018, 10:37 a.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 // Chromium's support for tabs.removeCSS is still a work in progress and the
31 "CSSOrigin" in browser.extensionTypes; 32 // API is likely to be different from Firefox's; for now we just don't use it
33 // at all, even if it's available.
34 // See https://crbug.com/608854
35 const styleSheetRemovalSupported = info.platform == "gecko";
36
32 const selectorGroupSize = 1024; 37 const selectorGroupSize = 1024;
33 38
39 let userStyleSheetsSupported = true;
40
34 function* splitSelectors(selectors) 41 function* splitSelectors(selectors)
35 { 42 {
36 // Chromium's Blink engine supports only up to 8,192 simple selectors, and 43 // 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 44 // 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). 45 // 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 46 // 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 47 // 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 48 // selector won't have a size greater than 8. The alternative would be to
42 // calculate the sizes of the selectors and divide them up accordingly, but 49 // calculate the sizes of the selectors and divide them up accordingly, but
43 // this approach is more efficient and has worked well in practice. In theory 50 // this approach is more efficient and has worked well in practice. In theory
(...skipping 10 matching lines...) Expand all
54 yield selectorGroup.join(", ") + " {display: none !important;}"; 61 yield selectorGroup.join(", ") + " {display: none !important;}";
55 } 62 }
56 63
57 function createStyleSheet(selectors) 64 function createStyleSheet(selectors)
58 { 65 {
59 return Array.from(createRules(selectors)).join("\n"); 66 return Array.from(createRules(selectors)).join("\n");
60 } 67 }
61 68
62 function addStyleSheet(tabId, frameId, styleSheet) 69 function addStyleSheet(tabId, frameId, styleSheet)
63 { 70 {
64 browser.tabs.insertCSS(tabId, { 71 try
65 code: styleSheet, 72 {
66 cssOrigin: "user", 73 browser.tabs.insertCSS(tabId, {
67 frameId, 74 code: styleSheet,
68 matchAboutBlank: true, 75 cssOrigin: "user",
69 runAt: "document_start" 76 frameId,
70 }); 77 matchAboutBlank: true,
78 runAt: "document_start"
79 });
80 }
81 catch (error)
82 {
83 userStyleSheetsSupported = false;
84 }
85
86 return userStyleSheetsSupported;
71 } 87 }
72 88
73 function removeStyleSheet(tabId, frameId, styleSheet) 89 function removeStyleSheet(tabId, frameId, styleSheet)
74 { 90 {
91 if (!styleSheetRemovalSupported)
92 return;
93
75 browser.tabs.removeCSS(tabId, { 94 browser.tabs.removeCSS(tabId, {
76 code: styleSheet, 95 code: styleSheet,
77 cssOrigin: "user", 96 cssOrigin: "user",
78 frameId, 97 frameId,
79 matchAboutBlank: true 98 matchAboutBlank: true
80 }); 99 });
81 } 100 }
82 101
83 function updateFrameStyles(tabId, frameId, selectors, groupName) 102 function updateFrameStyles(tabId, frameId, selectors, groupName)
84 { 103 {
85 let styleSheet = null; 104 let styleSheet = null;
86 if (selectors.length > 0) 105 if (selectors.length > 0)
87 styleSheet = createStyleSheet(selectors); 106 styleSheet = createStyleSheet(selectors);
88 107
89 let frame = ext.getFrame(tabId, frameId); 108 let frame = ext.getFrame(tabId, frameId);
90 if (!frame.injectedStyleSheets) 109 if (!frame.injectedStyleSheets)
91 frame.injectedStyleSheets = new Map(); 110 frame.injectedStyleSheets = new Map();
92 111
93 let oldStyleSheet = frame.injectedStyleSheets.get(groupName); 112 let oldStyleSheet = frame.injectedStyleSheets.get(groupName);
94 113
95 // Ideally we would compare the old and new style sheets and skip this code 114 // Ideally we would compare the old and new style sheets and skip this code
96 // if they're the same, but the old style sheet can be a leftover from a 115 // if they're the same, but the old style sheet can be a leftover from a
97 // previous instance of the frame. We must add the new style sheet 116 // previous instance of the frame. We must add the new style sheet
98 // regardless. 117 // regardless.
99 118
100 // Add the new style sheet first to keep previously hidden elements from 119 // Add the new style sheet first to keep previously hidden elements from
101 // reappearing momentarily. 120 // reappearing momentarily.
102 if (styleSheet) 121 if (styleSheet && !addStyleSheet(tabId, frameId, styleSheet))
103 addStyleSheet(tabId, frameId, styleSheet); 122 return false;
104 123
105 // Sometimes the old and new style sheets can be exactly the same. In such a 124 // Sometimes the old and new style sheets can be exactly the same. In such a
106 // case, do not remove the "old" style sheet, because it is in fact the new 125 // case, do not remove the "old" style sheet, because it is in fact the new
107 // style sheet now. 126 // style sheet now.
108 if (oldStyleSheet && oldStyleSheet != styleSheet) 127 if (oldStyleSheet && oldStyleSheet != styleSheet)
109 removeStyleSheet(tabId, frameId, oldStyleSheet); 128 removeStyleSheet(tabId, frameId, oldStyleSheet);
110 129
111 frame.injectedStyleSheets.set(groupName, styleSheet); 130 frame.injectedStyleSheets.set(groupName, styleSheet);
131 return true;
112 } 132 }
113 133
114 port.on("elemhide.getSelectors", (message, sender) => 134 port.on("elemhide.getSelectors", (message, sender) =>
115 { 135 {
116 let selectors = []; 136 let selectors = [];
117 let emulatedPatterns = []; 137 let emulatedPatterns = [];
118 let trace = devtools && devtools.hasPanel(sender.page); 138 let trace = devtools && devtools.hasPanel(sender.page);
119 let inline = !userStyleSheetsSupported; 139 let inline = !userStyleSheetsSupported;
120 140
121 if (!checkWhitelisted(sender.page, sender.frame, 141 if (!checkWhitelisted(sender.page, sender.frame,
122 RegExpFilter.typeMap.DOCUMENT | 142 RegExpFilter.typeMap.DOCUMENT |
123 RegExpFilter.typeMap.ELEMHIDE)) 143 RegExpFilter.typeMap.ELEMHIDE))
124 { 144 {
125 let hostname = extractHostFromFrame(sender.frame); 145 let hostname = extractHostFromFrame(sender.frame);
126 let specificOnly = checkWhitelisted(sender.page, sender.frame, 146 let specificOnly = checkWhitelisted(sender.page, sender.frame,
127 RegExpFilter.typeMap.GENERICHIDE); 147 RegExpFilter.typeMap.GENERICHIDE);
128 148
129 selectors = ElemHide.getSelectorsForDomain( 149 selectors = ElemHide.getSelectorsForDomain(
130 hostname, 150 hostname,
131 specificOnly ? ElemHide.SPECIFIC_ONLY : ElemHide.ALL_MATCHING 151 specificOnly ? ElemHide.SPECIFIC_ONLY : ElemHide.ALL_MATCHING
132 ); 152 );
133 153
134 for (let filter of ElemHideEmulation.getRulesForDomain(hostname)) 154 for (let filter of ElemHideEmulation.getRulesForDomain(hostname))
135 emulatedPatterns.push({selector: filter.selector, text: filter.text}); 155 emulatedPatterns.push({selector: filter.selector, text: filter.text});
136 } 156 }
137 157
138 if (!inline) 158 if (!inline && !updateFrameStyles(sender.page.id, sender.frame.id,
139 updateFrameStyles(sender.page.id, sender.frame.id, selectors, "standard"); 159 selectors, "standard"))
160 {
161 inline = true;
162 }
140 163
141 let response = {trace, inline, emulatedPatterns}; 164 let response = {trace, inline, emulatedPatterns};
142 if (trace || inline) 165 if (trace || inline)
143 response.selectors = selectors; 166 response.selectors = selectors;
144 167
168 // If we can't remove user style sheets using tabs.removeCSS, we'll only keep
169 // adding them, which could cause problems with emulation filters as
170 // described in issue #5864. Instead, we can just ask the content script to
171 // add styles for emulation filters inline.
172 if (!styleSheetRemovalSupported)
173 response.inlineEmulated = true;
174
145 return response; 175 return response;
146 }); 176 });
147 177
148 port.on("elemhide.injectSelectors", (message, sender) => 178 port.on("elemhide.injectSelectors", (message, sender) =>
149 { 179 {
150 updateFrameStyles(sender.page.id, sender.frame.id, message.selectors, 180 updateFrameStyles(sender.page.id, sender.frame.id, message.selectors,
151 message.groupName); 181 message.groupName);
152 }); 182 });
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