| 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 |
| (...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 const userStyleSheetsSupported = ("extensionTypes" in browser && | |
| 32 "CSSOrigin" in browser.extensionTypes) || | |
| 33 (info.platform == "chromium" && | |
| 34 parseInt(info.platformVersion, 10) >= 66); | |
| 35 | |
| 36 // 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 |
| 37 // 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 |
| 38 // at all, even if it's available. | 33 // at all, even if it's available. |
| 39 // See https://crbug.com/608854 | 34 // See https://crbug.com/608854 |
| 40 const styleSheetRemovalSupported = "removeCSS" in browser.tabs && | 35 const styleSheetRemovalSupported = info.platform == "gecko"; |
| 41 info.platform == "gecko"; | |
| 42 | 36 |
| 43 const selectorGroupSize = 1024; | 37 const selectorGroupSize = 1024; |
| 38 |
| 39 let userStyleSheetsSupported = true; |
| 44 | 40 |
| 45 function* splitSelectors(selectors) | 41 function* splitSelectors(selectors) |
| 46 { | 42 { |
| 47 // Chromium's Blink engine supports only up to 8,192 simple selectors, and | 43 // Chromium's Blink engine supports only up to 8,192 simple selectors, and |
| 48 // even fewer compound selectors, in a rule. The exact number of selectors | 44 // even fewer compound selectors, in a rule. The exact number of selectors |
| 49 // that would work depends on their sizes (e.g. "#foo .bar" has a size of 2). | 45 // that would work depends on their sizes (e.g. "#foo .bar" has a size of 2). |
| 50 // Since we don't know the sizes of the selectors here, we simply split them | 46 // Since we don't know the sizes of the selectors here, we simply split them |
| 51 // into groups of 1,024, based on the reasonable assumption that the average | 47 // into groups of 1,024, based on the reasonable assumption that the average |
| 52 // selector won't have a size greater than 8. The alternative would be to | 48 // selector won't have a size greater than 8. The alternative would be to |
| 53 // calculate the sizes of the selectors and divide them up accordingly, but | 49 // calculate the sizes of the selectors and divide them up accordingly, but |
| (...skipping 11 matching lines...) Expand all Loading... |
| 65 yield selectorGroup.join(", ") + " {display: none !important;}"; | 61 yield selectorGroup.join(", ") + " {display: none !important;}"; |
| 66 } | 62 } |
| 67 | 63 |
| 68 function createStyleSheet(selectors) | 64 function createStyleSheet(selectors) |
| 69 { | 65 { |
| 70 return Array.from(createRules(selectors)).join("\n"); | 66 return Array.from(createRules(selectors)).join("\n"); |
| 71 } | 67 } |
| 72 | 68 |
| 73 function addStyleSheet(tabId, frameId, styleSheet) | 69 function addStyleSheet(tabId, frameId, styleSheet) |
| 74 { | 70 { |
| 75 browser.tabs.insertCSS(tabId, { | 71 try |
| 76 code: styleSheet, | 72 { |
| 77 cssOrigin: "user", | 73 browser.tabs.insertCSS(tabId, { |
| 78 frameId, | 74 code: styleSheet, |
| 79 matchAboutBlank: true, | 75 cssOrigin: "user", |
| 80 runAt: "document_start" | 76 frameId, |
| 81 }); | 77 matchAboutBlank: true, |
| 78 runAt: "document_start" |
| 79 }); |
| 80 } |
| 81 catch (error) |
| 82 { |
| 83 userStyleSheetsSupported = false; |
| 84 } |
| 85 |
| 86 return userStyleSheetsSupported; |
| 82 } | 87 } |
| 83 | 88 |
| 84 function removeStyleSheet(tabId, frameId, styleSheet) | 89 function removeStyleSheet(tabId, frameId, styleSheet) |
| 85 { | 90 { |
| 86 if (!styleSheetRemovalSupported) | 91 if (!styleSheetRemovalSupported) |
| 87 return; | 92 return; |
| 88 | 93 |
| 89 browser.tabs.removeCSS(tabId, { | 94 browser.tabs.removeCSS(tabId, { |
| 90 code: styleSheet, | 95 code: styleSheet, |
| 91 cssOrigin: "user", | 96 cssOrigin: "user", |
| (...skipping 14 matching lines...) Expand all Loading... |
| 106 | 111 |
| 107 let oldStyleSheet = frame.injectedStyleSheets.get(groupName); | 112 let oldStyleSheet = frame.injectedStyleSheets.get(groupName); |
| 108 | 113 |
| 109 // Ideally we would compare the old and new style sheets and skip this code | 114 // Ideally we would compare the old and new style sheets and skip this code |
| 110 // if they're the same, but the old style sheet can be a leftover from a | 115 // if they're the same, but the old style sheet can be a leftover from a |
| 111 // previous instance of the frame. We must add the new style sheet | 116 // previous instance of the frame. We must add the new style sheet |
| 112 // regardless. | 117 // regardless. |
| 113 | 118 |
| 114 // Add the new style sheet first to keep previously hidden elements from | 119 // Add the new style sheet first to keep previously hidden elements from |
| 115 // reappearing momentarily. | 120 // reappearing momentarily. |
| 116 if (styleSheet) | 121 if (styleSheet && !addStyleSheet(tabId, frameId, styleSheet)) |
| 117 addStyleSheet(tabId, frameId, styleSheet); | 122 return false; |
| 118 | 123 |
| 119 // Sometimes the old and new style sheets can be exactly the same. In such a | 124 // Sometimes the old and new style sheets can be exactly the same. In such a |
| 120 // case, do not remove the "old" style sheet, because it is in fact the new | 125 // case, do not remove the "old" style sheet, because it is in fact the new |
| 121 // style sheet now. | 126 // style sheet now. |
| 122 if (oldStyleSheet && oldStyleSheet != styleSheet) | 127 if (oldStyleSheet && oldStyleSheet != styleSheet) |
| 123 removeStyleSheet(tabId, frameId, oldStyleSheet); | 128 removeStyleSheet(tabId, frameId, oldStyleSheet); |
| 124 | 129 |
| 125 frame.injectedStyleSheets.set(groupName, styleSheet); | 130 frame.injectedStyleSheets.set(groupName, styleSheet); |
| 131 return true; |
| 126 } | 132 } |
| 127 | 133 |
| 128 port.on("elemhide.getSelectors", (message, sender) => | 134 port.on("elemhide.getSelectors", (message, sender) => |
| 129 { | 135 { |
| 130 let selectors = []; | 136 let selectors = []; |
| 131 let emulatedPatterns = []; | 137 let emulatedPatterns = []; |
| 132 let trace = devtools && devtools.hasPanel(sender.page); | 138 let trace = devtools && devtools.hasPanel(sender.page); |
| 133 let inline = !userStyleSheetsSupported; | 139 let inline = !userStyleSheetsSupported; |
| 134 | 140 |
| 135 if (!checkWhitelisted(sender.page, sender.frame, | 141 if (!checkWhitelisted(sender.page, sender.frame, |
| 136 RegExpFilter.typeMap.DOCUMENT | | 142 RegExpFilter.typeMap.DOCUMENT | |
| 137 RegExpFilter.typeMap.ELEMHIDE)) | 143 RegExpFilter.typeMap.ELEMHIDE)) |
| 138 { | 144 { |
| 139 let hostname = extractHostFromFrame(sender.frame); | 145 let hostname = extractHostFromFrame(sender.frame); |
| 140 let specificOnly = checkWhitelisted(sender.page, sender.frame, | 146 let specificOnly = checkWhitelisted(sender.page, sender.frame, |
| 141 RegExpFilter.typeMap.GENERICHIDE); | 147 RegExpFilter.typeMap.GENERICHIDE); |
| 142 | 148 |
| 143 selectors = ElemHide.getSelectorsForDomain( | 149 selectors = ElemHide.getSelectorsForDomain( |
| 144 hostname, | 150 hostname, |
| 145 specificOnly ? ElemHide.SPECIFIC_ONLY : ElemHide.ALL_MATCHING | 151 specificOnly ? ElemHide.SPECIFIC_ONLY : ElemHide.ALL_MATCHING |
| 146 ); | 152 ); |
| 147 | 153 |
| 148 for (let filter of ElemHideEmulation.getRulesForDomain(hostname)) | 154 for (let filter of ElemHideEmulation.getRulesForDomain(hostname)) |
| 149 emulatedPatterns.push({selector: filter.selector, text: filter.text}); | 155 emulatedPatterns.push({selector: filter.selector, text: filter.text}); |
| 150 } | 156 } |
| 151 | 157 |
| 152 if (!inline) | 158 if (!inline && !updateFrameStyles(sender.page.id, sender.frame.id, |
| 153 updateFrameStyles(sender.page.id, sender.frame.id, selectors); | 159 selectors, "standard")) |
| 160 { |
| 161 inline = true; |
| 162 } |
| 154 | 163 |
| 155 let response = {trace, inline, emulatedPatterns}; | 164 let response = {trace, inline, emulatedPatterns}; |
| 156 if (trace || inline) | 165 if (trace || inline) |
| 157 response.selectors = selectors; | 166 response.selectors = selectors; |
| 158 | 167 |
| 159 // If we can't remove user style sheets using tabs.removeCSS, we'll only keep | 168 // If we can't remove user style sheets using tabs.removeCSS, we'll only keep |
| 160 // adding them, which could cause problems with emulated filters as described | 169 // adding them, which could cause problems with emulation filters as |
| 161 // in issue #5864. Instead, we can just ask the content script to add styles | 170 // described in issue #5864. Instead, we can just ask the content script to |
| 162 // for emulated filters inline. | 171 // add styles for emulation filters inline. |
| 163 if (!styleSheetRemovalSupported) | 172 if (!styleSheetRemovalSupported) |
| 164 response.inlineEmulated = true; | 173 response.inlineEmulated = true; |
| 165 | 174 |
| 166 return response; | 175 return response; |
| 167 }); | 176 }); |
| 168 | 177 |
| 169 port.on("elemhide.injectSelectors", (message, sender) => | 178 port.on("elemhide.injectSelectors", (message, sender) => |
| 170 { | 179 { |
| 171 updateFrameStyles(sender.page.id, sender.frame.id, message.selectors, | 180 updateFrameStyles(sender.page.id, sender.frame.id, message.selectors, |
| 172 message.groupName); | 181 message.groupName); |
| 173 }); | 182 }); |
| LEFT | RIGHT |