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-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 = new Map(); |
Sebastian Noack
2017/02/09 11:07:39
Why to use a a Map object? It seems the logic woul
wspee
2017/02/10 10:46:46
If you have information that are are dependent fro
Sebastian Noack
2017/02/10 14:52:55
I'd generally agree, that data that have a mutual
| |
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 for (let [key, value] of filters) | |
217 { | 228 { |
218 var selector = this.selectors[i]; | 229 if (!this.filters.has(key)) |
230 this.filters.set(key, []); | |
231 Array.prototype.push.apply(this.filters.get(key), value); | |
232 } | |
233 }, | |
219 | 234 |
220 for (var j = 0; j < nodes.length; j++) | 235 checkNodes: function(nodes, filters) |
236 { | |
237 let matchedFilters = []; | |
238 | |
239 for (let [filter, selectors] of filters) | |
240 { | |
241 for (let selector of selectors) | |
221 { | 242 { |
222 var elements = nodes[j].querySelectorAll(selector); | 243 for (let node of nodes) |
223 var matched = false; | 244 { |
245 let elements = node.querySelectorAll(selector); | |
246 let matched = false; | |
224 | 247 |
225 for (var k = 0; k < elements.length; k++) | 248 for (let element of elements) |
226 { | |
227 // Only consider selectors that actually have an effect on the | |
228 // computed styles, and aren't overridden by rules with higher | |
229 // priority, or haven't been circumvented in a different way. | |
230 if (getComputedStyle(elements[k]).display == "none") | |
231 { | 249 { |
232 matchedSelectors.push(selector); | 250 // Only consider selectors that actually have an effect on the |
233 matched = true; | 251 // computed styles, and aren't overridden by rules with higher |
252 // priority, or haven't been circumvented in a different way. | |
253 if (getComputedStyle(element).display == "none") | |
254 { | |
255 matchedFilters.push(filter.replace(/^.*?##/, "")); | |
256 matched = true; | |
Sebastian Noack
2017/02/09 11:07:39
It seems, the "match" variable would be unnecessar
wspee
2017/02/10 10:46:46
Done.
| |
257 break; | |
258 } | |
259 } | |
260 | |
261 if (matched) | |
234 break; | 262 break; |
235 } | |
236 } | 263 } |
237 | |
238 if (matched) | |
239 break; | |
240 } | 264 } |
241 } | 265 } |
242 | 266 |
243 if (matchedSelectors.length > 0) | 267 if (matchedFilters.length > 0) |
244 ext.backgroundPage.sendMessage({ | 268 ext.backgroundPage.sendMessage({ |
245 type: "devtools.traceElemHide", | 269 type: "devtools.traceElemHide", |
246 selectors: matchedSelectors | 270 selectors: matchedFilters |
247 }); | 271 }); |
248 }, | 272 }, |
249 | 273 |
250 onTimeout: function() | 274 onTimeout: function() |
251 { | 275 { |
252 this.checkNodes(this.changedNodes); | 276 this.checkNodes(this.changedNodes, this.filters); |
253 this.changedNodes = []; | 277 this.changedNodes = []; |
254 this.timeout = null; | 278 this.timeout = null; |
255 }, | 279 }, |
256 | 280 |
257 observe: function(mutations) | 281 observe: function(mutations) |
258 { | 282 { |
259 // Forget previously changed nodes that are no longer in the DOM. | 283 // Forget previously changed nodes that are no longer in the DOM. |
260 for (var i = 0; i < this.changedNodes.length; i++) | 284 for (var i = 0; i < this.changedNodes.length; i++) |
261 { | 285 { |
262 if (!document.contains(this.changedNodes[i])) | 286 if (!document.contains(this.changedNodes[i])) |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
305 | 329 |
306 // Check only nodes whose descendants have changed, and not more often | 330 // 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 | 331 // than once a second. Otherwise large pages with a lot of DOM mutations |
308 // (like YouTube) freeze when the devtools panel is active. | 332 // (like YouTube) freeze when the devtools panel is active. |
309 if (this.timeout == null) | 333 if (this.timeout == null) |
310 this.timeout = setTimeout(this.onTimeout.bind(this), 1000); | 334 this.timeout = setTimeout(this.onTimeout.bind(this), 1000); |
311 }, | 335 }, |
312 | 336 |
313 trace: function() | 337 trace: function() |
314 { | 338 { |
315 this.checkNodes([document]); | 339 this.checkNodes([document], this.filters); |
316 | 340 |
317 this.observer.observe( | 341 this.observer.observe( |
318 document, | 342 document, |
319 { | 343 { |
320 childList: true, | 344 childList: true, |
321 attributes: true, | 345 attributes: true, |
322 subtree: true | 346 subtree: true |
323 } | 347 } |
324 ); | 348 ); |
325 }, | 349 }, |
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
483 var shadow = shadowRoot(this); | 507 var shadow = shadowRoot(this); |
484 return shadow == ourShadowRoot ? null : shadow; | 508 return shadow == ourShadowRoot ? null : shadow; |
485 } | 509 } |
486 }); | 510 }); |
487 }, null); | 511 }, null); |
488 } | 512 } |
489 | 513 |
490 return shadow; | 514 return shadow; |
491 }, | 515 }, |
492 | 516 |
493 addSelectors: function(selectors) | 517 addSelectors: function(selectors, filters) |
494 { | 518 { |
495 if (selectors.length == 0) | 519 if (!selectors) |
Sebastian Noack
2017/02/09 11:07:39
Why did you change this line? Note that an empty a
wspee
2017/02/10 10:46:46
Done.
| |
496 return; | 520 return; |
497 | 521 |
522 if (this.tracer) | |
523 { | |
524 let tracedFilters = new Map(); | |
525 for (let i = 0; i < selectors.length; i++) | |
526 { | |
527 let filterKey = filters ? filters[i] : selectors[i]; | |
528 let tracedFilter = tracedFilters.get(filterKey); | |
529 if (!tracedFilter) | |
530 { | |
531 tracedFilter = []; | |
532 tracedFilters.set(filterKey, tracedFilter); | |
533 } | |
534 tracedFilter.push(selectors[i]); | |
535 } | |
536 this.tracer.addFilters(tracedFilters); | |
537 } | |
538 | |
498 if (!this.style) | 539 if (!this.style) |
499 { | 540 { |
500 // Create <style> element lazily, only if we add styles. Add it to | 541 // Create <style> element lazily, only if we add styles. Add it to |
501 // the shadow DOM if possible. Otherwise fallback to the <head> or | 542 // the shadow DOM if possible. Otherwise fallback to the <head> or |
502 // <html> element. If we have injected a style element before that | 543 // <html> element. If we have injected a style element before that |
503 // has been removed (the sheet property is null), create a new one. | 544 // has been removed (the sheet property is null), create a new one. |
504 this.style = document.createElement("style"); | 545 this.style = document.createElement("style"); |
505 (this.shadow || document.head | 546 (this.shadow || document.head |
506 || document.documentElement).appendChild(this.style); | 547 || document.documentElement).appendChild(this.style); |
507 | 548 |
(...skipping 29 matching lines...) Expand all Loading... | |
537 { | 578 { |
538 var selector = selectors.slice(i, i + this.selectorGroupSize).join(", "); | 579 var selector = selectors.slice(i, i + this.selectorGroupSize).join(", "); |
539 this.style.sheet.insertRule(selector + "{display: none !important;}", | 580 this.style.sheet.insertRule(selector + "{display: none !important;}", |
540 this.style.sheet.cssRules.length); | 581 this.style.sheet.cssRules.length); |
541 } | 582 } |
542 }, | 583 }, |
543 | 584 |
544 apply: function() | 585 apply: function() |
545 { | 586 { |
546 var selectors = null; | 587 var selectors = null; |
547 var elemHideEmulationLoaded = false; | |
548 | 588 |
549 var checkLoaded = function() | 589 var checkLoaded = function() |
550 { | 590 { |
551 if (!selectors || !elemHideEmulationLoaded) | |
552 return; | |
553 | |
554 if (this.tracer) | 591 if (this.tracer) |
555 this.tracer.disconnect(); | 592 this.tracer.disconnect(); |
556 this.tracer = null; | 593 this.tracer = null; |
557 | 594 |
595 if (selectors.trace) | |
596 this.tracer = new ElementHidingTracer(); | |
597 | |
558 if (this.style && this.style.parentElement) | 598 if (this.style && this.style.parentElement) |
559 this.style.parentElement.removeChild(this.style); | 599 this.style.parentElement.removeChild(this.style); |
560 this.style = null; | 600 this.style = null; |
561 | 601 |
562 this.addSelectors(selectors.selectors); | 602 if (selectors.selectors) |
603 this.addSelectors(selectors.selectors); | |
604 | |
563 this.elemHideEmulation.apply(); | 605 this.elemHideEmulation.apply(); |
564 | 606 |
565 if (selectors.trace) | 607 if (this.tracer) |
566 this.tracer = new ElementHidingTracer(selectors.selectors); | 608 this.tracer.start(); |
567 }.bind(this); | 609 }.bind(this); |
568 | 610 |
569 ext.backgroundPage.sendMessage({type: "get-selectors"}, function(response) | 611 ext.backgroundPage.sendMessage({type: "get-selectors"}, response => |
570 { | 612 { |
571 selectors = response; | 613 selectors = response; |
572 checkLoaded(); | 614 this.elemHideEmulation.load(checkLoaded); |
573 }); | |
574 | |
575 this.elemHideEmulation.load(function() | |
576 { | |
577 elemHideEmulationLoaded = true; | |
578 checkLoaded(); | |
579 }); | 615 }); |
580 } | 616 } |
581 }; | 617 }; |
582 | 618 |
583 if (document instanceof HTMLDocument) | 619 if (document instanceof HTMLDocument) |
584 { | 620 { |
585 checkSitekey(); | 621 checkSitekey(); |
586 wrapWebSocket(); | 622 wrapWebSocket(); |
587 | 623 |
588 var elemhide = new ElemHide(); | 624 var elemhide = new ElemHide(); |
589 elemhide.apply(); | 625 elemhide.apply(); |
590 | 626 |
591 document.addEventListener("error", function(event) | 627 document.addEventListener("error", function(event) |
592 { | 628 { |
593 checkCollapse(event.target); | 629 checkCollapse(event.target); |
594 }, true); | 630 }, true); |
595 | 631 |
596 document.addEventListener("load", function(event) | 632 document.addEventListener("load", function(event) |
597 { | 633 { |
598 var element = event.target; | 634 var element = event.target; |
599 if (/^i?frame$/.test(element.localName)) | 635 if (/^i?frame$/.test(element.localName)) |
600 checkCollapse(element); | 636 checkCollapse(element); |
601 }, true); | 637 }, true); |
602 } | 638 } |
OLD | NEW |