| Left: | ||
| Right: |
| 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-2015 Eyeo GmbH | 3 * Copyright (C) 2006-2015 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 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 173 } | 173 } |
| 174 | 174 |
| 175 result.push(prefix + selector.substring(start)); | 175 result.push(prefix + selector.substring(start)); |
| 176 } | 176 } |
| 177 | 177 |
| 178 return result; | 178 return result; |
| 179 } | 179 } |
| 180 | 180 |
| 181 function init(document) | 181 function init(document) |
| 182 { | 182 { |
| 183 var shadow = null; | |
| 184 var style = null; | |
| 185 | |
| 183 // Use Shadow DOM if available to don't mess with web pages that rely on | 186 // Use Shadow DOM if available to don't mess with web pages that rely on |
| 184 // the order of their own <style> tags (#309). | 187 // the order of their own <style> tags (#309). |
| 185 // | 188 // |
| 186 // However, creating a shadow root breaks running CSS transitions. So we | 189 // However, creating a shadow root breaks running CSS transitions. So we |
| 187 // have to create the shadow root before transistions might start (#452). | 190 // have to create the shadow root before transistions might start (#452). |
| 188 // | 191 // |
| 189 // Also, we can't use shadow DOM on Google Docs, since it breaks printing | 192 // Also, we can't use shadow DOM on Google Docs, since it breaks printing |
| 190 // there (#1770). | 193 // there (#1770). |
| 191 var shadow = null; | |
| 192 if ("createShadowRoot" in document.documentElement && document.domain != "docs .google.com") | 194 if ("createShadowRoot" in document.documentElement && document.domain != "docs .google.com") |
| 193 { | 195 { |
| 194 shadow = document.documentElement.createShadowRoot(); | 196 shadow = document.documentElement.createShadowRoot(); |
| 195 shadow.appendChild(document.createElement("shadow")); | 197 shadow.appendChild(document.createElement("shadow")); |
| 196 } | 198 } |
| 197 | 199 |
| 198 // Sets the currently used CSS rules for elemhide filters | 200 var hideElements = function(selectors) |
| 199 var setElemhideCSSRules = function(selectors) | |
| 200 { | 201 { |
| 201 if (selectors.length == 0) | 202 // Create <style> element lazily, only if we add styles. Add it to |
| 202 return; | 203 // the shadow DOM if possible. Otherwise fallback to the <head> or |
| 203 | 204 // <html> element. If we have injected a style element before that |
| 204 var style = document.createElement("style"); | 205 // has been removed (the sheet property is null), create a new one. |
| 205 style.setAttribute("type", "text/css"); | 206 if (!style || !style.sheet) |
| 206 | |
| 207 if (shadow) | |
| 208 { | 207 { |
| 209 shadow.appendChild(style); | 208 style = document.createElement("style"); |
| 210 selectors = convertSelectorsForShadowDOM(selectors); | 209 (shadow || document.head || document.documentElement).appendChild(style); |
| 211 } | |
| 212 else | |
| 213 { | |
| 214 // Try to insert the style into the <head> tag, inserting directly under t he | |
| 215 // document root breaks dev tools functionality: | |
| 216 // http://code.google.com/p/chromium/issues/detail?id=178109 | |
| 217 (document.head || document.documentElement).appendChild(style); | |
| 218 } | 210 } |
| 219 | 211 |
| 220 var setRules = function() | 212 // If applying this code for a subframe, that navigated to another document |
| 213 // while we waited for the response from the background page, the sheet | |
| 214 // property stays null, after adding the <style> element to the shadow DOM. | |
|
Wladimir Palant
2015/03/04 19:06:15
Sorry about nit picking but let's reformulate that
Sebastian Noack
2015/03/04 19:34:19
I changed following:
* I prefer to talk about a di
| |
| 215 if (style.sheet) | |
| 221 { | 216 { |
| 222 // The sheet property might not exist yet if the | 217 // If using shadow DOM, we have to add the ::content pseudo-element |
| 223 // <style> element was created for a sub frame | 218 // before each selector, in order to match elements within the |
| 224 if (!style.sheet) | 219 // insertion point. |
| 225 { | 220 if (shadow) |
| 226 setTimeout(setRules, 0); | 221 selectors = convertSelectorsForShadowDOM(selectors); |
| 227 return; | |
| 228 } | |
| 229 | 222 |
| 230 // WebKit apparently chokes when the selector list in a CSS rule is huge. | 223 // WebKit (and Blink?) apparently chokes when the selector list in a |
| 231 // So we split the elemhide selectors into groups. | 224 // CSS rule is huge. So we split the elemhide selectors into groups. |
| 232 for (var i = 0; selectors.length > 0; i++) | 225 while (selectors.length > 0) |
| 233 { | 226 { |
| 234 var selector = selectors.splice(0, SELECTOR_GROUP_SIZE).join(", "); | 227 var selector = selectors.splice(0, SELECTOR_GROUP_SIZE).join(", "); |
| 235 style.sheet.insertRule(selector + " { display: none !important; }", i); | 228 |
| 229 style.sheet.insertRule( | |
| 230 selector + " { display: none !important; }", | |
| 231 style.sheet.cssRules.length | |
| 232 ); | |
| 236 } | 233 } |
| 237 }; | 234 } |
| 235 }; | |
| 238 | 236 |
| 239 setRules(); | 237 ext.backgroundPage.sendMessage({type: "get-selectors"}, function(selectors) |
| 240 reinjectRulesWhenRemoved(document, style); | 238 { |
| 241 }; | 239 if (selectors.length > 0) |
| 240 { | |
| 241 hideElements(selectors); | |
| 242 reinjectRulesWhenRemoved(document, style); | |
| 243 } | |
| 244 }); | |
| 242 | 245 |
| 243 document.addEventListener("error", function(event) | 246 document.addEventListener("error", function(event) |
| 244 { | 247 { |
| 245 checkCollapse(event.target); | 248 checkCollapse(event.target); |
| 246 }, true); | 249 }, true); |
| 247 | 250 |
| 248 document.addEventListener("load", function(event) | 251 document.addEventListener("load", function(event) |
| 249 { | 252 { |
| 250 var element = event.target; | 253 var element = event.target; |
| 251 | 254 |
| 252 if (/^i?frame$/.test(element.localName)) | 255 if (/^i?frame$/.test(element.localName)) |
| 253 checkCollapse(element); | 256 checkCollapse(element); |
| 254 | 257 |
| 255 // prior to Chrome 37, content scripts cannot run on about:blank, | 258 // prior to Chrome 37, content scripts cannot run on about:blank, |
| 256 // about:srcdoc and javascript: URLs. Moreover, as of Chrome 40 | 259 // about:srcdoc and javascript: URLs. Moreover, as of Chrome 40 |
| 257 // "load" and "error" events aren't dispatched there. So we have | 260 // "load" and "error" events aren't dispatched there. So we have |
| 258 // to apply element hiding and collapsing from the parent frame. | 261 // to apply element hiding and collapsing from the parent frame. |
| 259 if (/\bChrome\//.test(navigator.userAgent) && isInlineFrame(element)) | 262 if (/\bChrome\//.test(navigator.userAgent) && isInlineFrame(element)) |
| 260 { | 263 { |
| 261 init(element.contentDocument); | 264 init(element.contentDocument); |
| 262 | 265 |
| 263 for (var tagName in typeMap) | 266 for (var tagName in typeMap) |
| 264 Array.prototype.forEach.call(element.contentDocument.getElementsByTagNam e(tagName), checkCollapse); | 267 Array.prototype.forEach.call(element.contentDocument.getElementsByTagNam e(tagName), checkCollapse); |
| 265 } | 268 } |
| 266 }, true); | 269 }, true); |
| 267 | 270 |
| 268 ext.backgroundPage.sendMessage({type: "get-selectors"}, setElemhideCSSRules); | 271 return hideElements; |
| 269 } | 272 } |
| 270 | 273 |
| 271 if (document instanceof HTMLDocument) | 274 if (document instanceof HTMLDocument) |
| 272 { | 275 { |
| 273 checkSitekey(); | 276 checkSitekey(); |
| 274 init(document); | 277 window.hideElements = init(document); |
| 275 } | 278 } |
| OLD | NEW |