LEFT | RIGHT |
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("../adblockpluscore/lib/filterClasses"); |
23 const {ElemHide} = require("elemHide"); | 23 const {ElemHide} = require("../adblockpluscore/lib/elemHide"); |
24 const {ElemHideEmulation} = require("elemHideEmulation"); | 24 const {ElemHideEmulation} = require("../adblockpluscore/lib/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("../buildtools/info"); |
30 | 30 |
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; |
(...skipping 23 matching lines...) Expand all Loading... |
63 | 63 |
64 function createStyleSheet(selectors) | 64 function createStyleSheet(selectors) |
65 { | 65 { |
66 return Array.from(createRules(selectors)).join("\n"); | 66 return Array.from(createRules(selectors)).join("\n"); |
67 } | 67 } |
68 | 68 |
69 function addStyleSheet(tabId, frameId, styleSheet) | 69 function addStyleSheet(tabId, frameId, styleSheet) |
70 { | 70 { |
71 try | 71 try |
72 { | 72 { |
73 browser.tabs.insertCSS(tabId, { | 73 let promise = browser.tabs.insertCSS(tabId, { |
74 code: styleSheet, | 74 code: styleSheet, |
75 cssOrigin: "user", | 75 cssOrigin: "user", |
76 frameId, | 76 frameId, |
77 matchAboutBlank: true, | 77 matchAboutBlank: true, |
78 runAt: "document_start" | 78 runAt: "document_start" |
79 }); | 79 }); |
| 80 |
| 81 // See error handling notes in the catch block. |
| 82 promise.catch(() => {}); |
80 } | 83 } |
81 catch (error) | 84 catch (error) |
82 { | 85 { |
83 userStyleSheetsSupported = false; | 86 // If the error is about the "cssOrigin" option, this is an older version |
84 } | 87 // of Chromium (65 and below) or Firefox (52 and below) that does not |
85 | 88 // support user style sheets. |
86 return userStyleSheetsSupported; | 89 if (/\bcssOrigin\b/.test(error.message)) |
| 90 userStyleSheetsSupported = false; |
| 91 |
| 92 // For other errors, we simply return false to indicate failure. |
| 93 // |
| 94 // One common error that occurs frequently is when a frame is not found |
| 95 // (e.g. "Error: No frame with id 574 in tab 266"), which can happen when |
| 96 // the code in the parent document has removed the frame before the |
| 97 // background page has had a chance to respond to the content script's |
| 98 // "elemhide.getSelectors" message. We simply ignore such errors, because |
| 99 // otherwise they show up in the log too often and make debugging |
| 100 // difficult. |
| 101 // |
| 102 // Also note that the missing frame error is thrown synchronously on |
| 103 // Firefox, while on Chromium it is an asychronous promise rejection. In |
| 104 // the latter case, we cannot indicate failure to the caller, but we still |
| 105 // explicitly ignore the error. |
| 106 return false; |
| 107 } |
| 108 |
| 109 return true; |
87 } | 110 } |
88 | 111 |
89 function removeStyleSheet(tabId, frameId, styleSheet) | 112 function removeStyleSheet(tabId, frameId, styleSheet) |
90 { | 113 { |
91 if (!styleSheetRemovalSupported) | 114 if (!styleSheetRemovalSupported) |
92 return; | 115 return; |
93 | 116 |
94 browser.tabs.removeCSS(tabId, { | 117 browser.tabs.removeCSS(tabId, { |
95 code: styleSheet, | 118 code: styleSheet, |
96 cssOrigin: "user", | 119 cssOrigin: "user", |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 removeStyleSheet(tabId, frameId, oldStyleSheet); | 157 removeStyleSheet(tabId, frameId, oldStyleSheet); |
135 | 158 |
136 frame.injectedStyleSheets.set(groupName, styleSheet); | 159 frame.injectedStyleSheets.set(groupName, styleSheet); |
137 return true; | 160 return true; |
138 } | 161 } |
139 | 162 |
140 port.on("elemhide.getSelectors", (message, sender) => | 163 port.on("elemhide.getSelectors", (message, sender) => |
141 { | 164 { |
142 let selectors = []; | 165 let selectors = []; |
143 let emulatedPatterns = []; | 166 let emulatedPatterns = []; |
144 let trace = devtools && devtools.hasPanel(sender.page); | 167 let trace = devtools.hasPanel(sender.page.id); |
145 let inline = !userStyleSheetsSupported; | 168 let inline = !userStyleSheetsSupported; |
146 | 169 |
147 if (!checkWhitelisted(sender.page, sender.frame, | 170 if (!checkWhitelisted(sender.page, sender.frame, null, |
148 RegExpFilter.typeMap.DOCUMENT | | 171 RegExpFilter.typeMap.DOCUMENT | |
149 RegExpFilter.typeMap.ELEMHIDE)) | 172 RegExpFilter.typeMap.ELEMHIDE)) |
150 { | 173 { |
151 let hostname = extractHostFromFrame(sender.frame); | 174 let hostname = extractHostFromFrame(sender.frame); |
152 let specificOnly = checkWhitelisted(sender.page, sender.frame, | 175 let specificOnly = checkWhitelisted(sender.page, sender.frame, null, |
153 RegExpFilter.typeMap.GENERICHIDE); | 176 RegExpFilter.typeMap.GENERICHIDE); |
154 | 177 |
155 selectors = ElemHide.getSelectorsForDomain( | 178 selectors = ElemHide.getSelectorsForDomain( |
156 hostname, | 179 hostname, |
157 specificOnly ? ElemHide.SPECIFIC_ONLY : ElemHide.ALL_MATCHING | 180 specificOnly ? ElemHide.SPECIFIC_ONLY : ElemHide.ALL_MATCHING |
158 ); | 181 ); |
159 | 182 |
160 for (let filter of ElemHideEmulation.getRulesForDomain(hostname)) | 183 for (let filter of ElemHideEmulation.getRulesForDomain(hostname)) |
161 emulatedPatterns.push({selector: filter.selector, text: filter.text}); | 184 emulatedPatterns.push({selector: filter.selector, text: filter.text}); |
162 } | 185 } |
(...skipping 16 matching lines...) Expand all Loading... |
179 response.inlineEmulated = true; | 202 response.inlineEmulated = true; |
180 | 203 |
181 return response; | 204 return response; |
182 }); | 205 }); |
183 | 206 |
184 port.on("elemhide.injectSelectors", (message, sender) => | 207 port.on("elemhide.injectSelectors", (message, sender) => |
185 { | 208 { |
186 updateFrameStyles(sender.page.id, sender.frame.id, message.selectors, | 209 updateFrameStyles(sender.page.id, sender.frame.id, message.selectors, |
187 message.groupName, message.appendOnly); | 210 message.groupName, message.appendOnly); |
188 }); | 211 }); |
LEFT | RIGHT |