Left: | ||
Right: |
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 |
(...skipping 10 matching lines...) Expand all Loading... | |
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 const info = require("info"); |
30 | 30 |
31 // Chromium's support for tabs.removeCSS is still a work in progress and the | 31 const styleSheetRemovalSupported = "removeCSS" in browser.tabs; |
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 |
37 const selectorGroupSize = 1024; | 33 const selectorGroupSize = 1024; |
38 | 34 |
39 let userStyleSheetsSupported = true; | 35 let userStyleSheetsSupported = true; |
40 | 36 |
41 function* splitSelectors(selectors) | 37 function* splitSelectors(selectors) |
42 { | 38 { |
43 // Chromium's Blink engine supports only up to 8,192 simple selectors, and | 39 // Chromium's Blink engine supports only up to 8,192 simple selectors, and |
44 // even fewer compound selectors, in a rule. The exact number of selectors | 40 // even fewer compound selectors, in a rule. The exact number of selectors |
45 // that would work depends on their sizes (e.g. "#foo .bar" has a size of 2). | 41 // that would work depends on their sizes (e.g. "#foo .bar" has a size of 2). |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
110 | 106 |
111 let frame = ext.getFrame(tabId, frameId); | 107 let frame = ext.getFrame(tabId, frameId); |
112 if (!frame) | 108 if (!frame) |
113 return false; | 109 return false; |
114 | 110 |
115 if (!frame.injectedStyleSheets) | 111 if (!frame.injectedStyleSheets) |
116 frame.injectedStyleSheets = new Map(); | 112 frame.injectedStyleSheets = new Map(); |
117 | 113 |
118 let oldStyleSheet = frame.injectedStyleSheets.get(groupName); | 114 let oldStyleSheet = frame.injectedStyleSheets.get(groupName); |
119 | 115 |
116 // The behavior of tabs.insertCSS and tabs.removeCSS is different between | |
kzar
2018/03/19 18:42:49
Thanks for taking the time to write such a good co
| |
117 // Firefox and Chromium. On Firefox, it is an error to inject a duplicate | |
118 // style sheet, whereas Chromium will happily inject the duplicate as a copy. | |
119 // tabs.removeCSS on Chromium will remove only the last injected copy. | |
120 // | |
120 // Ideally we would compare the old and new style sheets and skip this code | 121 // Ideally we would compare the old and new style sheets and skip this code |
121 // if they're the same, but the old style sheet can be a leftover from a | 122 // if they're the same, but the old style sheet can be a leftover from a |
122 // previous instance of the frame. We must add the new style sheet | 123 // previous instance of the frame. We must add the new style sheet |
123 // regardless. | 124 // regardless. |
125 // | |
126 // We could simply remove the old style sheet first before we add the new | |
127 // one, but this may cause previously hidden elements to reappear | |
128 // momentarily. On the other hand, we cannot remove the old style sheet after | |
129 // we add the new one if they're exactly the same, because this would cause | |
130 // Firefox to remove its only copy; if we skip the removal, Chromium would | |
131 // end up with a duplicate copy! | |
132 // | |
133 // So: (1) we do not want to remove first to avoid flicker; (2) if the old | |
kzar
2018/03/19 18:42:49
I think this stanza can be removed.
| |
134 // and new style sheets are identical, we do not want to remove on Firefox, | |
135 // but we do want to remove on Chromium. | |
136 // | |
137 // In order to resolve this conundrum without resorting to browser-specific | |
138 // code, we treat the different types of style sheets differently. | |
139 if (groupName == "standard") | |
kzar
2018/03/19 18:42:49
Perhaps the logic would be clearer something like
| |
140 { | |
141 // Style sheets in the "standard" group are typically only added once and | |
kzar
2018/03/19 18:42:49
I think instead of saying 'the "standard" group' a
| |
142 // never removed. In this case, it's OK to remove the old style sheet first | |
143 // and accept the flicker. | |
144 if (oldStyleSheet) | |
145 { | |
146 removeStyleSheet(tabId, frameId, oldStyleSheet); | |
124 | 147 |
125 // Add the new style sheet first to keep previously hidden elements from | 148 // Delete the entry first, just in case style sheet injection below |
126 // reappearing momentarily. | 149 // fails. |
127 if (styleSheet && !addStyleSheet(tabId, frameId, styleSheet)) | 150 frame.injectedStyleSheets.delete(groupName); |
128 return false; | 151 } |
129 | 152 |
130 // Sometimes the old and new style sheets can be exactly the same. In such a | 153 if (styleSheet && !addStyleSheet(tabId, frameId, styleSheet)) |
131 // case, do not remove the "old" style sheet, because it is in fact the new | 154 return false; |
132 // style sheet now. | 155 } |
133 if (oldStyleSheet && oldStyleSheet != styleSheet) | 156 else if (styleSheet != oldStyleSheet) |
134 removeStyleSheet(tabId, frameId, oldStyleSheet); | 157 { |
158 // Note that it is possible that the old and new style sheets are the same | |
159 // but the old one is really a leftover from a previous instance of the | |
160 // frame (i.e. it doesn't exist in the current instance of the frame). In | |
161 // this case we'll skip the addition of the new style sheet and ads will | |
162 // not get blocked. This would not be acceptable for style sheets in the | |
163 // "standard" group, but it's OK for other style sheets. Given the problems | |
164 // described above, this is the best tradeoff. | |
165 | |
166 // Add the new style sheet first to keep previously hidden elements from | |
167 // reappearing momentarily. | |
168 if (styleSheet && !addStyleSheet(tabId, frameId, styleSheet)) | |
169 return false; | |
170 | |
171 if (oldStyleSheet) | |
172 removeStyleSheet(tabId, frameId, oldStyleSheet); | |
173 } | |
135 | 174 |
136 frame.injectedStyleSheets.set(groupName, styleSheet); | 175 frame.injectedStyleSheets.set(groupName, styleSheet); |
137 return true; | 176 return true; |
138 } | 177 } |
139 | 178 |
140 port.on("elemhide.getSelectors", (message, sender) => | 179 port.on("elemhide.getSelectors", (message, sender) => |
141 { | 180 { |
142 let selectors = []; | 181 let selectors = []; |
143 let emulatedPatterns = []; | 182 let emulatedPatterns = []; |
144 let trace = devtools && devtools.hasPanel(sender.page); | 183 let trace = devtools && devtools.hasPanel(sender.page); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
179 response.inlineEmulated = true; | 218 response.inlineEmulated = true; |
180 | 219 |
181 return response; | 220 return response; |
182 }); | 221 }); |
183 | 222 |
184 port.on("elemhide.injectSelectors", (message, sender) => | 223 port.on("elemhide.injectSelectors", (message, sender) => |
185 { | 224 { |
186 updateFrameStyles(sender.page.id, sender.frame.id, message.selectors, | 225 updateFrameStyles(sender.page.id, sender.frame.id, message.selectors, |
187 message.groupName); | 226 message.groupName); |
188 }); | 227 }); |
OLD | NEW |