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 20 matching lines...) Expand all Loading... | |
31 // Chromium's support for tabs.removeCSS is still a work in progress and the | 31 // Chromium's support for tabs.removeCSS is still a work in progress and the |
32 // API is likely to be different from Firefox's; for now we just don't use it | 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. | 33 // at all, even if it's available. |
34 // See https://crbug.com/608854 | 34 // See https://crbug.com/608854 |
35 const styleSheetRemovalSupported = info.platform == "gecko"; | 35 const styleSheetRemovalSupported = info.platform == "gecko"; |
36 | 36 |
37 const selectorGroupSize = 1024; | 37 const selectorGroupSize = 1024; |
38 | 38 |
39 let userStyleSheetsSupported = true; | 39 let userStyleSheetsSupported = true; |
40 | 40 |
41 function delay(time, func = () => {}, ...args) | |
Manish Jethani
2018/04/06 06:20:02
This simply wraps setTimeout into a promise, which
| |
42 { | |
43 return new Promise((resolve, reject) => | |
44 { | |
45 setTimeout(() => | |
46 { | |
47 try | |
48 { | |
49 resolve(func(...args)); | |
50 } | |
51 catch (error) | |
52 { | |
53 reject(error); | |
54 } | |
55 }, | |
56 time); | |
57 }); | |
58 } | |
59 | |
41 function* splitSelectors(selectors) | 60 function* splitSelectors(selectors) |
42 { | 61 { |
43 // Chromium's Blink engine supports only up to 8,192 simple selectors, and | 62 // 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 | 63 // 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). | 64 // that would work depends on their sizes (e.g. "#foo .bar" has a size of 2). |
46 // Since we don't know the sizes of the selectors here, we simply split them | 65 // Since we don't know the sizes of the selectors here, we simply split them |
47 // into groups of 1,024, based on the reasonable assumption that the average | 66 // into groups of 1,024, based on the reasonable assumption that the average |
48 // selector won't have a size greater than 8. The alternative would be to | 67 // selector won't have a size greater than 8. The alternative would be to |
49 // calculate the sizes of the selectors and divide them up accordingly, but | 68 // calculate the sizes of the selectors and divide them up accordingly, but |
50 // this approach is more efficient and has worked well in practice. In theory | 69 // this approach is more efficient and has worked well in practice. In theory |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
150 // Sometimes the old and new style sheets can be exactly the same. In such a | 169 // Sometimes the old and new style sheets can be exactly the same. In such a |
151 // case, do not remove the "old" style sheet, because it is in fact the new | 170 // case, do not remove the "old" style sheet, because it is in fact the new |
152 // style sheet now. | 171 // style sheet now. |
153 if (oldStyleSheet && oldStyleSheet != styleSheet) | 172 if (oldStyleSheet && oldStyleSheet != styleSheet) |
154 removeStyleSheet(tabId, frameId, oldStyleSheet); | 173 removeStyleSheet(tabId, frameId, oldStyleSheet); |
155 | 174 |
156 frame.injectedStyleSheets.set(groupName, styleSheet); | 175 frame.injectedStyleSheets.set(groupName, styleSheet); |
157 return true; | 176 return true; |
158 } | 177 } |
159 | 178 |
160 port.on("elemhide.getSelectors", (message, sender) => | 179 function doElementHiding(page, frame) |
161 { | 180 { |
162 let selectors = []; | 181 let selectors = []; |
163 let emulatedPatterns = []; | 182 let emulatedPatterns = []; |
164 let trace = devtools && devtools.hasPanel(sender.page); | 183 let trace = devtools && devtools.hasPanel(page); |
165 let inline = !userStyleSheetsSupported; | 184 let inline = !userStyleSheetsSupported; |
166 | 185 |
167 if (!checkWhitelisted(sender.page, sender.frame, | 186 if (!checkWhitelisted(page, frame, |
168 RegExpFilter.typeMap.DOCUMENT | | 187 RegExpFilter.typeMap.DOCUMENT | |
169 RegExpFilter.typeMap.ELEMHIDE)) | 188 RegExpFilter.typeMap.ELEMHIDE)) |
170 { | 189 { |
171 let hostname = extractHostFromFrame(sender.frame); | 190 let hostname = extractHostFromFrame(frame); |
172 let specificOnly = checkWhitelisted(sender.page, sender.frame, | 191 let specificOnly = checkWhitelisted(page, frame, |
173 RegExpFilter.typeMap.GENERICHIDE); | 192 RegExpFilter.typeMap.GENERICHIDE); |
174 | 193 |
175 selectors = ElemHide.getSelectorsForDomain( | 194 selectors = ElemHide.getSelectorsForDomain( |
176 hostname, | 195 hostname, |
177 specificOnly ? ElemHide.SPECIFIC_ONLY : ElemHide.ALL_MATCHING | 196 specificOnly ? ElemHide.SPECIFIC_ONLY : ElemHide.ALL_MATCHING |
178 ); | 197 ); |
179 | 198 |
180 for (let filter of ElemHideEmulation.getRulesForDomain(hostname)) | 199 for (let filter of ElemHideEmulation.getRulesForDomain(hostname)) |
181 emulatedPatterns.push({selector: filter.selector, text: filter.text}); | 200 emulatedPatterns.push({selector: filter.selector, text: filter.text}); |
182 } | 201 } |
183 | 202 |
184 if (!inline && !updateFrameStyles(sender.page.id, sender.frame.id, | 203 if (!inline && !updateFrameStyles(page.id, frame.id, selectors, "standard")) |
185 selectors, "standard")) | |
186 { | |
187 inline = true; | 204 inline = true; |
188 } | |
189 | 205 |
190 let response = {trace, inline, emulatedPatterns}; | 206 let message = {type: "elemhide.apply", trace, inline}; |
191 if (trace || inline) | 207 if (trace || inline) |
192 response.selectors = selectors; | 208 message.selectors = selectors; |
209 | |
210 if (emulatedPatterns.length > 0) | |
211 message.emulatedPatterns = emulatedPatterns; | |
193 | 212 |
194 // If we can't remove user style sheets using tabs.removeCSS, we'll only keep | 213 // If we can't remove user style sheets using tabs.removeCSS, we'll only keep |
195 // adding them, which could cause problems with emulation filters as | 214 // adding them, which could cause problems with emulation filters as |
196 // described in issue #5864. Instead, we can just ask the content script to | 215 // described in issue #5864. Instead, we can just ask the content script to |
197 // add styles for emulation filters inline. | 216 // add styles for emulation filters inline. |
198 if (!styleSheetRemovalSupported) | 217 if (!styleSheetRemovalSupported) |
199 response.inlineEmulated = true; | 218 message.inlineEmulated = true; |
200 | 219 |
201 return response; | 220 // In most cases on modern browsers there's nothing for the content script to |
221 // do. | |
222 if (!message.selectors && !message.emulatedPatterns) | |
223 return; | |
224 | |
225 // The content script is loaded at about the same time as we get the | |
226 // webNavigation.onCommitted event; sometimes we have to retry a couple of | |
227 // times before we get through. | |
228 browser.tabs.sendMessage(page.id, message, {frameId: frame.id}) | |
229 .catch(() => | |
230 delay(250, browser.tabs.sendMessage, page.id, message, {frameId: frame.id}) | |
Manish Jethani
2018/04/06 06:20:02
Unfortunately sometimes the content script hasn't
| |
231 ) | |
232 .catch(() => | |
233 delay(1000, browser.tabs.sendMessage, page.id, message, {frameId: frame.id}) | |
234 ); | |
235 } | |
236 | |
237 browser.webNavigation.onCommitted.addListener(({tabId, frameId, url}) => | |
238 { | |
239 // There's a bug in Chrome that causes webNavigation.onCommitted to get | |
240 // dispatched twice if there's a URL filter present, therefore we must listen | |
241 // for all URLs and do an explicit check here. | |
242 // https://crbug.com/827855 | |
243 if (!/^(https?:\/\/|about:blank\b|about:srcdoc\b)/.test(url)) | |
244 return; | |
245 | |
246 let page = new ext.Page({id: tabId, url: url}); | |
247 let frame = ext.getFrame(tabId, frameId); | |
248 | |
249 if (!frame) | |
250 return; | |
251 | |
252 doElementHiding(page, frame); | |
253 }); | |
254 | |
255 port.on("elemhide.needApply", (message, sender) => | |
256 { | |
257 doElementHiding(sender.page, sender.frame); | |
202 }); | 258 }); |
203 | 259 |
204 port.on("elemhide.injectSelectors", (message, sender) => | 260 port.on("elemhide.injectSelectors", (message, sender) => |
205 { | 261 { |
206 updateFrameStyles(sender.page.id, sender.frame.id, message.selectors, | 262 updateFrameStyles(sender.page.id, sender.frame.id, message.selectors, |
207 message.groupName); | 263 message.groupName); |
208 }); | 264 }); |
OLD | NEW |