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