| 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 |
| 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 scriptInjection */ |
| 19 | 19 |
| 20 "use strict"; | 20 "use strict"; |
| 21 | 21 |
| 22 const {RegExpFilter} = require("../adblockpluscore/lib/filterClasses"); | 22 const {RegExpFilter} = require("../adblockpluscore/lib/filterClasses"); |
| 23 const {ElemHide} = require("../adblockpluscore/lib/elemHide"); | 23 const {ElemHide} = require("../adblockpluscore/lib/elemHide"); |
| 24 const {ElemHideEmulation} = require("../adblockpluscore/lib/elemHideEmulation"); | 24 const {ElemHideEmulation} = require("../adblockpluscore/lib/elemHideEmulation"); |
| 25 const {Snippets, compileScript} = require("../adblockpluscore/lib/snippets"); |
| 25 const {checkWhitelisted} = require("./whitelisting"); | 26 const {checkWhitelisted} = require("./whitelisting"); |
| 26 const {extractHostFromFrame} = require("./url"); | 27 const {extractHostFromFrame} = require("./url"); |
| 27 const {port} = require("./messaging"); | 28 const {port} = require("./messaging"); |
| 28 const {HitLogger} = require("./hitLogger"); | 29 const {HitLogger} = require("./hitLogger"); |
| 29 const info = require("info"); | 30 const info = require("info"); |
| 30 | 31 |
| 31 // Chromium's support for tabs.removeCSS is still a work in progress and the | 32 // 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 | 33 // 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 // at all, even if it's available. |
| 34 // See https://crbug.com/608854 | 35 // See https://crbug.com/608854 |
| 35 const styleSheetRemovalSupported = info.platform == "gecko"; | 36 const styleSheetRemovalSupported = info.platform == "gecko"; |
| 36 | 37 |
| 37 const selectorGroupSize = 1024; | 38 const selectorGroupSize = 1024; |
| 38 | 39 |
| 39 let userStyleSheetsSupported = true; | 40 let userStyleSheetsSupported = true; |
| 40 | 41 |
| 42 let snippetsLibrarySource = ""; |
| 43 let executableCode = new Map(); |
| 44 |
| 41 function* splitSelectors(selectors) | 45 function* splitSelectors(selectors) |
| 42 { | 46 { |
| 43 // Chromium's Blink engine supports only up to 8,192 simple selectors, and | 47 // 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 | 48 // 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). | 49 // 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 | 50 // 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 | 51 // 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 | 52 // 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 | 53 // 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 | 54 // this approach is more efficient and has worked well in practice. In theory |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 88 // support user style sheets. | 92 // support user style sheets. |
| 89 if (/\bcssOrigin\b/.test(error.message)) | 93 if (/\bcssOrigin\b/.test(error.message)) |
| 90 userStyleSheetsSupported = false; | 94 userStyleSheetsSupported = false; |
| 91 | 95 |
| 92 // For other errors, we simply return false to indicate failure. | 96 // For other errors, we simply return false to indicate failure. |
| 93 // | 97 // |
| 94 // One common error that occurs frequently is when a frame is not found | 98 // 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 | 99 // (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 | 100 // 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 | 101 // background page has had a chance to respond to the content script's |
| 98 // "elemhide.getSelectors" message. We simply ignore such errors, because | 102 // "content.applyFilters" message. We simply ignore such errors, because |
| 99 // otherwise they show up in the log too often and make debugging | 103 // otherwise they show up in the log too often and make debugging |
| 100 // difficult. | 104 // difficult. |
| 101 // | 105 // |
| 102 // Also note that the missing frame error is thrown synchronously on | 106 // Also note that the missing frame error is thrown synchronously on |
| 103 // Firefox, while on Chromium it is an asychronous promise rejection. In | 107 // 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 | 108 // the latter case, we cannot indicate failure to the caller, but we still |
| 105 // explicitly ignore the error. | 109 // explicitly ignore the error. |
| 106 return false; | 110 return false; |
| 107 } | 111 } |
| 108 | 112 |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 153 // Sometimes the old and new style sheets can be exactly the same. In such a | 157 // Sometimes the old and new style sheets can be exactly the same. In such a |
| 154 // case, do not remove the "old" style sheet, because it is in fact the new | 158 // case, do not remove the "old" style sheet, because it is in fact the new |
| 155 // style sheet now. | 159 // style sheet now. |
| 156 if (oldStyleSheet && oldStyleSheet != styleSheet) | 160 if (oldStyleSheet && oldStyleSheet != styleSheet) |
| 157 removeStyleSheet(tabId, frameId, oldStyleSheet); | 161 removeStyleSheet(tabId, frameId, oldStyleSheet); |
| 158 | 162 |
| 159 frame.injectedStyleSheets.set(groupName, styleSheet); | 163 frame.injectedStyleSheets.set(groupName, styleSheet); |
| 160 return true; | 164 return true; |
| 161 } | 165 } |
| 162 | 166 |
| 163 port.on("elemhide.getSelectors", (message, sender) => | 167 function getExecutableCode(script) |
| 168 { |
| 169 let code = executableCode.get(script); |
| 170 if (code) |
| 171 return code; |
| 172 |
| 173 code = compileScript(script, [snippetsLibrarySource]); |
| 174 |
| 175 executableCode.set(script, code); |
| 176 return code; |
| 177 } |
| 178 |
| 179 function executeScript(script, tabId, frameId) |
| 180 { |
| 181 try |
| 182 { |
| 183 browser.tabs.executeScript(tabId, { |
| 184 code: getExecutableCode(script), |
| 185 frameId, |
| 186 matchAboutBlank: true, |
| 187 runAt: "document_start" |
| 188 }) |
| 189 .catch(error => |
| 190 { |
| 191 // Sometimes a frame is added and removed very quickly, in such cases we |
| 192 // simply ignore the error. |
| 193 if (error.message == "The frame was removed.") |
| 194 return; |
| 195 |
| 196 throw error; |
| 197 }); |
| 198 } |
| 199 catch (error) |
| 200 { |
| 201 } |
| 202 } |
| 203 |
| 204 port.on("content.applyFilters", (message, sender) => |
| 164 { | 205 { |
| 165 let selectors = []; | 206 let selectors = []; |
| 166 let emulatedPatterns = []; | 207 let emulatedPatterns = []; |
| 167 let trace = HitLogger.hasListener(sender.page.id); | 208 let trace = HitLogger.hasListener(sender.page.id); |
| 168 let inline = !userStyleSheetsSupported; | 209 let inline = !userStyleSheetsSupported; |
| 169 | 210 |
| 211 let {elemhide, snippets} = message.filterTypes || |
| 212 {elemhide: true, snippets: true}; |
| 213 |
| 170 if (!checkWhitelisted(sender.page, sender.frame, null, | 214 if (!checkWhitelisted(sender.page, sender.frame, null, |
| 171 RegExpFilter.typeMap.DOCUMENT | | 215 RegExpFilter.typeMap.DOCUMENT)) |
| 172 RegExpFilter.typeMap.ELEMHIDE)) | |
| 173 { | 216 { |
| 174 let hostname = extractHostFromFrame(sender.frame); | 217 let hostname = extractHostFromFrame(sender.frame); |
| 175 let specificOnly = checkWhitelisted(sender.page, sender.frame, null, | |
| 176 RegExpFilter.typeMap.GENERICHIDE); | |
| 177 | 218 |
| 178 selectors = ElemHide.getSelectorsForDomain(hostname, specificOnly); | 219 if (snippets) |
| 220 { |
| 221 for (let script of Snippets.getScriptsForDomain(hostname)) |
| 222 executeScript(script, sender.page.id, sender.frame.id); |
| 223 } |
| 179 | 224 |
| 180 for (let filter of ElemHideEmulation.getRulesForDomain(hostname)) | 225 if (elemhide && !checkWhitelisted(sender.page, sender.frame, null, |
| 181 emulatedPatterns.push({selector: filter.selector, text: filter.text}); | 226 RegExpFilter.typeMap.ELEMHIDE)) |
| 227 { |
| 228 let specificOnly = checkWhitelisted(sender.page, sender.frame, null, |
| 229 RegExpFilter.typeMap.GENERICHIDE); |
| 230 selectors = ElemHide.getSelectorsForDomain(hostname, specificOnly); |
| 231 |
| 232 for (let filter of ElemHideEmulation.getRulesForDomain(hostname)) |
| 233 emulatedPatterns.push({selector: filter.selector, text: filter.text}); |
| 234 } |
| 182 } | 235 } |
| 183 | 236 |
| 184 if (!inline && !updateFrameStyles(sender.page.id, sender.frame.id, | 237 if (!inline && !updateFrameStyles(sender.page.id, sender.frame.id, |
| 185 selectors, "standard")) | 238 selectors, "standard")) |
| 186 { | 239 { |
| 187 inline = true; | 240 inline = true; |
| 188 } | 241 } |
| 189 | 242 |
| 190 let response = {trace, inline, emulatedPatterns}; | 243 let response = {trace, inline, emulatedPatterns}; |
| 191 if (trace || inline) | 244 if (trace || inline) |
| 192 response.selectors = selectors; | 245 response.selectors = selectors; |
| 193 | 246 |
| 194 // If we can't remove user style sheets using tabs.removeCSS, we'll only keep | 247 // 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 | 248 // adding them, which could cause problems with emulation filters as |
| 196 // described in issue #5864. Instead, we can just ask the content script to | 249 // described in issue #5864. Instead, we can just ask the content script to |
| 197 // add styles for emulation filters inline. | 250 // add styles for emulation filters inline. |
| 198 if (!styleSheetRemovalSupported) | 251 if (!styleSheetRemovalSupported) |
| 199 response.inlineEmulated = true; | 252 response.inlineEmulated = true; |
| 200 | 253 |
| 201 return response; | 254 return response; |
| 202 }); | 255 }); |
| 203 | 256 |
| 204 port.on("elemhide.injectSelectors", (message, sender) => | 257 port.on("elemhide.injectSelectors", (message, sender) => |
| 205 { | 258 { |
| 206 updateFrameStyles(sender.page.id, sender.frame.id, message.selectors, | 259 updateFrameStyles(sender.page.id, sender.frame.id, message.selectors, |
| 207 message.groupName, message.appendOnly); | 260 message.groupName, message.appendOnly); |
| 208 }); | 261 }); |
| 262 |
| 263 fetch(browser.extension.getURL("/snippets.js"), {cache: "no-cache"}) |
| 264 .then(response => response.ok ? response.text() : "") |
| 265 .then(text => |
| 266 { |
| 267 snippetsLibrarySource = text; |
| 268 }); |
| OLD | NEW |