| OLD | NEW |
| 1 /* | 1 /* |
| 2 * This file is part of Adblock Plus <http://adblockplus.org/>, | 2 * This file is part of Adblock Plus <http://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2014 Eyeo GmbH | 3 * Copyright (C) 2006-2014 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 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 // because we don't know how many rules are already in there | 145 // because we don't know how many rules are already in there |
| 146 stylesheet.addRule(selector, "display: none !important;"); | 146 stylesheet.addRule(selector, "display: none !important;"); |
| 147 } | 147 } |
| 148 } | 148 } |
| 149 ); | 149 ); |
| 150 }); | 150 }); |
| 151 | 151 |
| 152 observer.observe(style.parentNode, {childList: true}); | 152 observer.observe(style.parentNode, {childList: true}); |
| 153 } | 153 } |
| 154 | 154 |
| 155 function traceHiddenElements(document, selectors) |
| 156 { |
| 157 function check(element) |
| 158 { |
| 159 var matchedSelectors = []; |
| 160 |
| 161 for (var i = 0; i < selectors.length; i++) |
| 162 { |
| 163 var selector = selectors[i]; |
| 164 var elements = document.querySelectorAll(selector); |
| 165 |
| 166 for (var j = 0; j < elements.length; j++) |
| 167 { |
| 168 if (getComputedStyle(elements[j]).display == "none") |
| 169 { |
| 170 matchedSelectors.push(selector); |
| 171 break; |
| 172 } |
| 173 } |
| 174 } |
| 175 |
| 176 if (matchedSelectors.length > 0) |
| 177 ext.backgroundPage.sendMessage({type: "trace-elemhide", selectors: matched
Selectors}); |
| 178 } |
| 179 |
| 180 function trace() |
| 181 { |
| 182 check(); |
| 183 |
| 184 var MutationObserver = window.MutationObserver || window.WebKitMutationObser
ver; |
| 185 if (MutationObserver) |
| 186 { |
| 187 new MutationObserver(check).observe(document, |
| 188 { |
| 189 childList: true, |
| 190 attributes: true, |
| 191 subtree: true |
| 192 }); |
| 193 } |
| 194 } |
| 195 |
| 196 if (document.readyState == "loading") |
| 197 document.addEventListener("DOMContentLoaded", trace); |
| 198 else |
| 199 trace(); |
| 200 } |
| 201 |
| 155 function init(document) | 202 function init(document) |
| 156 { | 203 { |
| 157 // use Shadow DOM if available to don't mess with web pages that | 204 // use Shadow DOM if available to don't mess with web pages that |
| 158 // rely on the order of their own <style> tags (#309). However we | 205 // rely on the order of their own <style> tags (#309). However we |
| 159 // must not create the shadow root in the response callback passed | 206 // must not create the shadow root in the response callback passed |
| 160 // to sendMessage(), otherwise Chrome breaks some websites (#450). | 207 // to sendMessage(), otherwise Chrome breaks some websites (#450). |
| 161 var shadow = null; | 208 var shadow = null; |
| 162 if ("createShadowRoot" in document.documentElement) | 209 if ("createShadowRoot" in document.documentElement) |
| 163 { | 210 { |
| 164 shadow = document.documentElement.createShadowRoot(); | 211 shadow = document.documentElement.createShadowRoot(); |
| 165 shadow.appendChild(document.createElement("shadow")); | 212 shadow.appendChild(document.createElement("shadow")); |
| 166 } | 213 } |
| 167 | 214 |
| 168 // Sets the currently used CSS rules for elemhide filters | 215 // Sets the currently used CSS rules for elemhide filters |
| 169 var setElemhideCSSRules = function(selectors) | 216 var setElemhideCSSRules = function(response) |
| 170 { | 217 { |
| 171 if (selectors.length == 0) | 218 if (response.selectors.length == 0) |
| 172 return; | 219 return; |
| 173 | 220 |
| 221 var selectors = response.selectors.slice(0); |
| 174 var style = document.createElement("style"); | 222 var style = document.createElement("style"); |
| 175 style.setAttribute("type", "text/css"); | 223 style.setAttribute("type", "text/css"); |
| 176 | 224 |
| 177 if (shadow) | 225 if (shadow) |
| 178 { | 226 { |
| 179 shadow.appendChild(style); | 227 shadow.appendChild(style); |
| 180 | 228 |
| 181 for (var i = 0; i < selectors.length; i++) | 229 for (var i = 0; i < selectors.length; i++) |
| 182 selectors[i] = "::content " + selectors[i]; | 230 selectors[i] = "::content " + selectors[i]; |
| 183 } | 231 } |
| (...skipping 15 matching lines...) Expand all Loading... |
| 199 return; | 247 return; |
| 200 } | 248 } |
| 201 | 249 |
| 202 // WebKit apparently chokes when the selector list in a CSS rule is huge. | 250 // WebKit apparently chokes when the selector list in a CSS rule is huge. |
| 203 // So we split the elemhide selectors into groups. | 251 // So we split the elemhide selectors into groups. |
| 204 for (var i = 0; selectors.length > 0; i++) | 252 for (var i = 0; selectors.length > 0; i++) |
| 205 { | 253 { |
| 206 var selector = selectors.splice(0, SELECTOR_GROUP_SIZE).join(", "); | 254 var selector = selectors.splice(0, SELECTOR_GROUP_SIZE).join(", "); |
| 207 style.sheet.insertRule(selector + " { display: none !important; }", i); | 255 style.sheet.insertRule(selector + " { display: none !important; }", i); |
| 208 } | 256 } |
| 257 |
| 258 if (response.trace) |
| 259 traceHiddenElements(document, response.selectors); |
| 209 }; | 260 }; |
| 210 | 261 |
| 211 setRules(); | 262 setRules(); |
| 212 reinjectRulesWhenRemoved(document, style); | 263 reinjectRulesWhenRemoved(document, style); |
| 213 }; | 264 }; |
| 214 | 265 |
| 215 document.addEventListener("error", function(event) | 266 document.addEventListener("error", function(event) |
| 216 { | 267 { |
| 217 checkCollapse(event.target); | 268 checkCollapse(event.target); |
| 218 }, true); | 269 }, true); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 238 }, true); | 289 }, true); |
| 239 | 290 |
| 240 ext.backgroundPage.sendMessage({type: "get-selectors"}, setElemhideCSSRules); | 291 ext.backgroundPage.sendMessage({type: "get-selectors"}, setElemhideCSSRules); |
| 241 } | 292 } |
| 242 | 293 |
| 243 if (document instanceof HTMLDocument) | 294 if (document instanceof HTMLDocument) |
| 244 { | 295 { |
| 245 checkSitekey(); | 296 checkSitekey(); |
| 246 init(document); | 297 init(document); |
| 247 } | 298 } |
| OLD | NEW |