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-2016 Eyeo GmbH | 3 * Copyright (C) 2006-2016 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 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 try | 185 try |
186 { | 186 { |
187 return element.contentDocument; | 187 return element.contentDocument; |
188 } | 188 } |
189 catch (e) | 189 catch (e) |
190 { | 190 { |
191 return null; | 191 return null; |
192 } | 192 } |
193 } | 193 } |
194 | 194 |
195 function ElementHidingTracer(selectors) | 195 function ElementHidingTracer() |
196 { | 196 { |
197 this.selectors = selectors; | 197 this.filters = []; |
198 | 198 |
199 this.changedNodes = []; | 199 this.changedNodes = []; |
200 this.timeout = null; | 200 this.timeout = null; |
| 201 this.started = false; |
201 | 202 |
202 this.observer = new MutationObserver(this.observe.bind(this)); | 203 this.observer = new MutationObserver(this.observe.bind(this)); |
203 this.trace = this.trace.bind(this); | 204 this.trace = this.trace.bind(this); |
204 | |
205 if (document.readyState == "loading") | |
206 document.addEventListener("DOMContentLoaded", this.trace); | |
207 else | |
208 this.trace(); | |
209 } | 205 } |
210 ElementHidingTracer.prototype = { | 206 ElementHidingTracer.prototype = { |
211 checkNodes: function(nodes) | 207 start: function() |
212 { | 208 { |
213 var matchedSelectors = []; | 209 let _start = () => { |
| 210 this.trace(); |
| 211 this.started = true; |
| 212 }; |
214 | 213 |
215 // Find all selectors that match any hidden element inside the given nodes. | 214 if (document.readyState == "loading") |
216 for (var i = 0; i < this.selectors.length; i++) | 215 document.addEventListener("DOMContentLoaded", _start); |
| 216 else |
| 217 _start(); |
| 218 }, |
| 219 |
| 220 addFilters: function(filters) |
| 221 { |
| 222 if (this.started) |
| 223 window.setTimeout(() => { |
| 224 this.checkNodes([document], filters); |
| 225 }, 0); |
| 226 |
| 227 Array.prototype.push.apply(this.filters, filters); |
| 228 }, |
| 229 |
| 230 checkNodes: function(nodes, filters) |
| 231 { |
| 232 let matchedFilters = []; |
| 233 |
| 234 for (let [filter, selector] of filters) |
217 { | 235 { |
218 var selector = this.selectors[i]; | 236 nodes: for (let node of nodes) |
| 237 { |
| 238 let elements = node.querySelectorAll(selector); |
219 | 239 |
220 for (var j = 0; j < nodes.length; j++) | 240 for (let element of elements) |
221 { | |
222 var elements = nodes[j].querySelectorAll(selector); | |
223 var matched = false; | |
224 | |
225 for (var k = 0; k < elements.length; k++) | |
226 { | 241 { |
227 // Only consider selectors that actually have an effect on the | 242 // Only consider selectors that actually have an effect on the |
228 // computed styles, and aren't overridden by rules with higher | 243 // computed styles, and aren't overridden by rules with higher |
229 // priority, or haven't been circumvented in a different way. | 244 // priority, or haven't been circumvented in a different way. |
230 if (getComputedStyle(elements[k]).display == "none") | 245 if (getComputedStyle(element).display == "none") |
231 { | 246 { |
232 matchedSelectors.push(selector); | 247 matchedFilters.push(filter.replace(/^.*?##/, "")); |
233 matched = true; | 248 break nodes; |
234 break; | |
235 } | 249 } |
236 } | 250 } |
237 | |
238 if (matched) | |
239 break; | |
240 } | 251 } |
241 } | 252 } |
242 | 253 |
243 if (matchedSelectors.length > 0) | 254 if (matchedFilters.length > 0) |
244 ext.backgroundPage.sendMessage({ | 255 ext.backgroundPage.sendMessage({ |
245 type: "devtools.traceElemHide", | 256 type: "devtools.traceElemHide", |
246 selectors: matchedSelectors | 257 selectors: matchedFilters |
247 }); | 258 }); |
248 }, | 259 }, |
249 | 260 |
250 onTimeout: function() | 261 onTimeout: function() |
251 { | 262 { |
252 this.checkNodes(this.changedNodes); | 263 this.checkNodes(this.changedNodes, this.filters); |
253 this.changedNodes = []; | 264 this.changedNodes = []; |
254 this.timeout = null; | 265 this.timeout = null; |
255 }, | 266 }, |
256 | 267 |
257 observe: function(mutations) | 268 observe: function(mutations) |
258 { | 269 { |
259 // Forget previously changed nodes that are no longer in the DOM. | 270 // Forget previously changed nodes that are no longer in the DOM. |
260 for (var i = 0; i < this.changedNodes.length; i++) | 271 for (var i = 0; i < this.changedNodes.length; i++) |
261 { | 272 { |
262 if (!document.contains(this.changedNodes[i])) | 273 if (!document.contains(this.changedNodes[i])) |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
305 | 316 |
306 // Check only nodes whose descendants have changed, and not more often | 317 // Check only nodes whose descendants have changed, and not more often |
307 // than once a second. Otherwise large pages with a lot of DOM mutations | 318 // than once a second. Otherwise large pages with a lot of DOM mutations |
308 // (like YouTube) freeze when the devtools panel is active. | 319 // (like YouTube) freeze when the devtools panel is active. |
309 if (this.timeout == null) | 320 if (this.timeout == null) |
310 this.timeout = setTimeout(this.onTimeout.bind(this), 1000); | 321 this.timeout = setTimeout(this.onTimeout.bind(this), 1000); |
311 }, | 322 }, |
312 | 323 |
313 trace: function() | 324 trace: function() |
314 { | 325 { |
315 this.checkNodes([document]); | 326 this.checkNodes([document], this.filters); |
316 | 327 |
317 this.observer.observe( | 328 this.observer.observe( |
318 document, | 329 document, |
319 { | 330 { |
320 childList: true, | 331 childList: true, |
321 attributes: true, | 332 attributes: true, |
322 subtree: true | 333 subtree: true |
323 } | 334 } |
324 ); | 335 ); |
325 }, | 336 }, |
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
483 var shadow = shadowRoot(this); | 494 var shadow = shadowRoot(this); |
484 return shadow == ourShadowRoot ? null : shadow; | 495 return shadow == ourShadowRoot ? null : shadow; |
485 } | 496 } |
486 }); | 497 }); |
487 }, null); | 498 }, null); |
488 } | 499 } |
489 | 500 |
490 return shadow; | 501 return shadow; |
491 }, | 502 }, |
492 | 503 |
493 addSelectors: function(selectors) | 504 addSelectors: function(selectors, filters) |
494 { | 505 { |
495 if (selectors.length == 0) | 506 if (!selectors.length) |
496 return; | 507 return; |
497 | 508 |
| 509 if (this.tracer) |
| 510 { |
| 511 let tracedFilters = []; |
| 512 for (let i = 0; i < selectors.length; i++) |
| 513 tracedFilters.push([filters ? filters[i] : selectors[i], selectors[i]]); |
| 514 this.tracer.addFilters(tracedFilters); |
| 515 } |
| 516 |
498 if (!this.style) | 517 if (!this.style) |
499 { | 518 { |
500 // Create <style> element lazily, only if we add styles. Add it to | 519 // Create <style> element lazily, only if we add styles. Add it to |
501 // the shadow DOM if possible. Otherwise fallback to the <head> or | 520 // the shadow DOM if possible. Otherwise fallback to the <head> or |
502 // <html> element. If we have injected a style element before that | 521 // <html> element. If we have injected a style element before that |
503 // has been removed (the sheet property is null), create a new one. | 522 // has been removed (the sheet property is null), create a new one. |
504 this.style = document.createElement("style"); | 523 this.style = document.createElement("style"); |
505 (this.shadow || document.head | 524 (this.shadow || document.head |
506 || document.documentElement).appendChild(this.style); | 525 || document.documentElement).appendChild(this.style); |
507 | 526 |
(...skipping 29 matching lines...) Expand all Loading... |
537 { | 556 { |
538 var selector = selectors.slice(i, i + this.selectorGroupSize).join(", "); | 557 var selector = selectors.slice(i, i + this.selectorGroupSize).join(", "); |
539 this.style.sheet.insertRule(selector + "{display: none !important;}", | 558 this.style.sheet.insertRule(selector + "{display: none !important;}", |
540 this.style.sheet.cssRules.length); | 559 this.style.sheet.cssRules.length); |
541 } | 560 } |
542 }, | 561 }, |
543 | 562 |
544 apply: function() | 563 apply: function() |
545 { | 564 { |
546 var selectors = null; | 565 var selectors = null; |
547 var elemHideEmulationLoaded = false; | |
548 | 566 |
549 var checkLoaded = function() | 567 var checkLoaded = function() |
550 { | 568 { |
551 if (!selectors || !elemHideEmulationLoaded) | |
552 return; | |
553 | |
554 if (this.tracer) | 569 if (this.tracer) |
555 this.tracer.disconnect(); | 570 this.tracer.disconnect(); |
556 this.tracer = null; | 571 this.tracer = null; |
557 | 572 |
| 573 if (selectors.trace) |
| 574 this.tracer = new ElementHidingTracer(); |
| 575 |
558 if (this.style && this.style.parentElement) | 576 if (this.style && this.style.parentElement) |
559 this.style.parentElement.removeChild(this.style); | 577 this.style.parentElement.removeChild(this.style); |
560 this.style = null; | 578 this.style = null; |
561 | 579 |
562 this.addSelectors(selectors.selectors); | 580 if (selectors.selectors) |
| 581 this.addSelectors(selectors.selectors); |
| 582 |
563 this.elemHideEmulation.apply(); | 583 this.elemHideEmulation.apply(); |
564 | 584 |
565 if (selectors.trace) | 585 if (this.tracer) |
566 this.tracer = new ElementHidingTracer(selectors.selectors); | 586 this.tracer.start(); |
567 }.bind(this); | 587 }.bind(this); |
568 | 588 |
569 ext.backgroundPage.sendMessage({type: "get-selectors"}, function(response) | 589 ext.backgroundPage.sendMessage({type: "get-selectors"}, response => |
570 { | 590 { |
571 selectors = response; | 591 selectors = response; |
572 checkLoaded(); | 592 this.elemHideEmulation.load(checkLoaded); |
573 }); | |
574 | |
575 this.elemHideEmulation.load(function() | |
576 { | |
577 elemHideEmulationLoaded = true; | |
578 checkLoaded(); | |
579 }); | 593 }); |
580 } | 594 } |
581 }; | 595 }; |
582 | 596 |
583 if (document instanceof HTMLDocument) | 597 if (document instanceof HTMLDocument) |
584 { | 598 { |
585 checkSitekey(); | 599 checkSitekey(); |
586 wrapWebSocket(); | 600 wrapWebSocket(); |
587 | 601 |
588 var elemhide = new ElemHide(); | 602 var elemhide = new ElemHide(); |
589 elemhide.apply(); | 603 elemhide.apply(); |
590 | 604 |
591 document.addEventListener("error", function(event) | 605 document.addEventListener("error", function(event) |
592 { | 606 { |
593 checkCollapse(event.target); | 607 checkCollapse(event.target); |
594 }, true); | 608 }, true); |
595 | 609 |
596 document.addEventListener("load", function(event) | 610 document.addEventListener("load", function(event) |
597 { | 611 { |
598 var element = event.target; | 612 var 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 } |
OLD | NEW |