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 139 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 | 150 // 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 | 151 // case, do not remove the "old" style sheet, because it is in fact the new |
152 // style sheet now. | 152 // style sheet now. |
153 if (oldStyleSheet && oldStyleSheet != styleSheet) | 153 if (oldStyleSheet && oldStyleSheet != styleSheet) |
154 removeStyleSheet(tabId, frameId, oldStyleSheet); | 154 removeStyleSheet(tabId, frameId, oldStyleSheet); |
155 | 155 |
156 frame.injectedStyleSheets.set(groupName, styleSheet); | 156 frame.injectedStyleSheets.set(groupName, styleSheet); |
157 return true; | 157 return true; |
158 } | 158 } |
159 | 159 |
160 port.on("elemhide.getSelectors", (message, sender) => | 160 function doElementHiding(page, frame) |
161 { | 161 { |
162 let selectors = []; | 162 let selectors = []; |
163 let emulatedPatterns = []; | 163 let emulatedPatterns = []; |
164 let trace = devtools && devtools.hasPanel(sender.page); | 164 let trace = devtools && devtools.hasPanel(page); |
165 let inline = !userStyleSheetsSupported; | 165 let inline = !userStyleSheetsSupported; |
166 | 166 |
167 if (!checkWhitelisted(sender.page, sender.frame, | 167 if (!checkWhitelisted(page, frame, |
168 RegExpFilter.typeMap.DOCUMENT | | 168 RegExpFilter.typeMap.DOCUMENT | |
169 RegExpFilter.typeMap.ELEMHIDE)) | 169 RegExpFilter.typeMap.ELEMHIDE)) |
170 { | 170 { |
171 let hostname = extractHostFromFrame(sender.frame); | 171 let hostname = extractHostFromFrame(frame); |
172 let specificOnly = checkWhitelisted(sender.page, sender.frame, | 172 let specificOnly = checkWhitelisted(page, frame, |
173 RegExpFilter.typeMap.GENERICHIDE); | 173 RegExpFilter.typeMap.GENERICHIDE); |
174 | 174 |
175 selectors = ElemHide.getSelectorsForDomain( | 175 selectors = ElemHide.getSelectorsForDomain( |
176 hostname, | 176 hostname, |
177 specificOnly ? ElemHide.SPECIFIC_ONLY : ElemHide.ALL_MATCHING | 177 specificOnly ? ElemHide.SPECIFIC_ONLY : ElemHide.ALL_MATCHING |
178 ); | 178 ); |
179 | 179 |
180 for (let filter of ElemHideEmulation.getRulesForDomain(hostname)) | 180 for (let filter of ElemHideEmulation.getRulesForDomain(hostname)) |
181 emulatedPatterns.push({selector: filter.selector, text: filter.text}); | 181 emulatedPatterns.push({selector: filter.selector, text: filter.text}); |
182 } | 182 } |
183 | 183 |
184 if (!inline && !updateFrameStyles(sender.page.id, sender.frame.id, | 184 if (!inline && !updateFrameStyles(page.id, frame.id, |
185 selectors, "standard")) | 185 selectors, "standard")) |
186 { | 186 { |
187 inline = true; | 187 inline = true; |
188 } | 188 } |
189 | 189 |
190 let response = {trace, inline, emulatedPatterns}; | 190 let message = {type: "elemhide.apply", trace, inline, emulatedPatterns}; |
191 if (trace || inline) | 191 if (trace || inline) |
192 response.selectors = selectors; | 192 message.selectors = selectors; |
193 | 193 |
194 // If we can't remove user style sheets using tabs.removeCSS, we'll only keep | 194 // 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 | 195 // adding them, which could cause problems with emulation filters as |
196 // described in issue #5864. Instead, we can just ask the content script to | 196 // described in issue #5864. Instead, we can just ask the content script to |
197 // add styles for emulation filters inline. | 197 // add styles for emulation filters inline. |
198 if (!styleSheetRemovalSupported) | 198 if (!styleSheetRemovalSupported) |
199 response.inlineEmulated = true; | 199 message.inlineEmulated = true; |
200 | 200 |
201 return response; | 201 if (!message.selectors && message.emulatedPatterns.length == 0) |
| 202 return; |
| 203 |
| 204 // The content script is loaded at about the same time as we get the |
| 205 // webNavigation.onCommitted event; sometimes we have to retry a couple of |
| 206 // times before we get through. |
| 207 browser.tabs.sendMessage(page.id, message, {frameId: frame.id}) |
| 208 .catch(() => |
| 209 browser.tabs.sendMessage(page.id, message, {frameId: frame.id}) |
| 210 ) |
| 211 .catch(() => |
| 212 browser.tabs.sendMessage(page.id, message, {frameId: frame.id}) |
| 213 ); |
| 214 } |
| 215 |
| 216 browser.webNavigation.onCommitted.addListener(details => |
| 217 { |
| 218 if (!/^(https?:\/\/|about:blank\b|about:srcdoc\b)/.test(details.url)) |
| 219 return; |
| 220 |
| 221 let page = new ext.Page({id: details.tabId, url: details.url}); |
| 222 let frame = ext.getFrame(details.tabId, details.frameId); |
| 223 |
| 224 if (!frame) |
| 225 return; |
| 226 |
| 227 doElementHiding(page, frame); |
| 228 }); |
| 229 |
| 230 port.on("elemhide.needApply", (message, sender) => |
| 231 { |
| 232 doElementHiding(sender.page, sender.frame); |
202 }); | 233 }); |
203 | 234 |
204 port.on("elemhide.injectSelectors", (message, sender) => | 235 port.on("elemhide.injectSelectors", (message, sender) => |
205 { | 236 { |
206 updateFrameStyles(sender.page.id, sender.frame.id, message.selectors, | 237 updateFrameStyles(sender.page.id, sender.frame.id, message.selectors, |
207 message.groupName); | 238 message.groupName); |
208 }); | 239 }); |
OLD | NEW |