| 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 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 | 124 |
| 125 for (let i = 0; i < urls.length; i++) | 125 for (let i = 0; i < urls.length; i++) |
| 126 { | 126 { |
| 127 if (/^(?!https?:)[\w-]+:/i.test(urls[i])) | 127 if (/^(?!https?:)[\w-]+:/i.test(urls[i])) |
| 128 urls.splice(i--, 1); | 128 urls.splice(i--, 1); |
| 129 } | 129 } |
| 130 | 130 |
| 131 return urls; | 131 return urls; |
| 132 } | 132 } |
| 133 | 133 |
| 134 function isCollapsibleMediaElement(element, mediatype) | 134 function getSelectorForUserStyleSheet(element) |
| 135 { | 135 { |
| 136 if (mediatype != "MEDIA") | 136 // Setting the "display" CSS property to "none" doesn't have any effect on |
| 137 return false; | 137 // <frame> elements (in framesets). So we have to hide it inline through |
| 138 // the "visisiblity" CSS property. |
| 139 if (element.localName == "frame") |
| 140 return null; |
| 138 | 141 |
| 139 if (!element.getAttribute("src")) | 142 // If the <video> or <audio> element contains any <source> or <track> |
| 140 return false; | 143 // children, we cannot address it in CSS by the source URL; in that case we |
| 141 | 144 // don't "collapse" it using a CSS selector but rather hide it directly by |
| 142 for (let child of element.children) | 145 // setting the style="..." attribute. |
| 146 if (element.localName == "video" || element.localName == "audio") |
| 143 { | 147 { |
| 144 // If the <video> or <audio> element contains any <source> or <track> | 148 for (let child of element.children) |
| 145 // children, we cannot address it in CSS by the source URL; in that case we | 149 { |
| 146 // don't "collapse" it using a CSS selector but rather hide it directly by | 150 if (child.localName == "source" || child.localName == "track") |
| 147 // setting the style="..." attribute. | 151 return null; |
| 148 if (child.localName == "source" || child.localName == "track") | 152 } |
| 149 return false; | |
| 150 } | 153 } |
| 151 | 154 |
| 152 return true; | 155 // Microsoft Edge does not support CSS.escape(). However, it doesn't |
| 153 } | 156 // support user stylesheets either. So the selector would be added as |
| 157 // an author style sheet anyway. |
| 158 if (!("escape" in CSS)) |
| 159 return null; |
| 154 | 160 |
| 155 function collapseMediaElement(element, srcValue) | 161 let selector = element.localName; |
| 156 { | 162 let hasSrc = false; |
| 157 if (!srcValue) | 163 for (let attr of ["src", "srcset"]) |
| 158 return; | 164 { |
| 165 if (attr in element) |
| 166 { |
| 167 let value = element.getAttribute(attr); |
| 168 if (value) |
| 169 { |
| 170 selector += "[" + attr + "=" + CSS.escape(value) + "]"; |
| 171 hasSrc = true; |
| 172 } |
| 173 } |
| 174 } |
| 159 | 175 |
| 160 let selector = element.localName + "[src=" + CSS.escape(srcValue) + "]"; | 176 return hasSrc ? selector : null; |
| 161 | |
| 162 // Adding selectors is expensive so do it only if we really have a new | |
| 163 // selector. | |
| 164 if (!collapsingSelectors.has(selector)) | |
| 165 { | |
| 166 collapsingSelectors.add(selector); | |
| 167 elemhide.addSelectors([selector], null, "collapsing", true); | |
| 168 } | |
| 169 } | 177 } |
| 170 | 178 |
| 171 function hideElement(element) | 179 function hideElement(element) |
| 172 { | 180 { |
| 173 function doHide() | 181 function doHide() |
| 174 { | 182 { |
| 175 let propertyName = "display"; | 183 let propertyName = "display"; |
| 176 let propertyValue = "none"; | 184 let propertyValue = "none"; |
| 177 if (element.localName == "frame") | 185 if (element.localName == "frame") |
| 178 { | 186 { |
| (...skipping 19 matching lines...) Expand all Loading... |
| 198 function checkCollapse(element) | 206 function checkCollapse(element) |
| 199 { | 207 { |
| 200 let mediatype = typeMap.get(element.localName); | 208 let mediatype = typeMap.get(element.localName); |
| 201 if (!mediatype) | 209 if (!mediatype) |
| 202 return; | 210 return; |
| 203 | 211 |
| 204 let urls = getURLsFromElement(element); | 212 let urls = getURLsFromElement(element); |
| 205 if (urls.length == 0) | 213 if (urls.length == 0) |
| 206 return; | 214 return; |
| 207 | 215 |
| 208 let collapsibleMediaElement = isCollapsibleMediaElement(element, mediatype); | 216 // Construct the selector here, because the attributes it relies on can change |
| 209 | 217 // between now and when we get the response from the background page. |
| 210 // Save the value of the src attribute because it can change between now and | 218 let selector = getSelectorForUserStyleSheet(element); |
| 211 // when we get the response from the background page. | |
| 212 let srcValue = collapsibleMediaElement ? element.getAttribute("src") : null; | |
| 213 | 219 |
| 214 browser.runtime.sendMessage( | 220 browser.runtime.sendMessage( |
| 215 { | 221 { |
| 216 type: "filters.collapse", | 222 type: "filters.collapse", |
| 217 urls, | 223 urls, |
| 218 mediatype, | 224 mediatype, |
| 219 baseURL: document.location.href | 225 baseURL: document.location.href |
| 220 }, | 226 }, |
| 221 collapse => | 227 collapse => |
| 222 { | 228 { |
| 223 if (collapse) | 229 if (collapse) |
| 224 { | 230 { |
| 225 if (collapsibleMediaElement) | 231 if (selector) |
| 226 collapseMediaElement(element, srcValue); | 232 { |
| 233 if (!collapsingSelectors.has(selector)) |
| 234 { |
| 235 collapsingSelectors.add(selector); |
| 236 elemhide.addSelectors([selector], null, "collapsing", true); |
| 237 } |
| 238 } |
| 227 else | 239 else |
| 240 { |
| 228 hideElement(element); | 241 hideElement(element); |
| 242 } |
| 229 } | 243 } |
| 230 } | 244 } |
| 231 ); | 245 ); |
| 232 } | 246 } |
| 233 | 247 |
| 234 function checkSitekey() | 248 function checkSitekey() |
| 235 { | 249 { |
| 236 let attr = document.documentElement.getAttribute("data-adblockkey"); | 250 let attr = document.documentElement.getAttribute("data-adblockkey"); |
| 237 if (attr) | 251 if (attr) |
| 238 browser.runtime.sendMessage({type: "filters.addKey", token: attr}); | 252 browser.runtime.sendMessage({type: "filters.addKey", token: attr}); |
| (...skipping 359 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 598 let element = event.target; | 612 let element = event.target; |
| 599 if (/^i?frame$/.test(element.localName)) | 613 if (/^i?frame$/.test(element.localName)) |
| 600 checkCollapse(element); | 614 checkCollapse(element); |
| 601 }, true); | 615 }, true); |
| 602 } | 616 } |
| 603 | 617 |
| 604 window.checkCollapse = checkCollapse; | 618 window.checkCollapse = checkCollapse; |
| 605 window.elemhide = elemhide; | 619 window.elemhide = elemhide; |
| 606 window.typeMap = typeMap; | 620 window.typeMap = typeMap; |
| 607 window.getURLsFromElement = getURLsFromElement; | 621 window.getURLsFromElement = getURLsFromElement; |
| OLD | NEW |