Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: include.preload.js

Issue 29370970: [adblockpluschrome] Issue 3596 - Added support for CSS property filters to devtools panel (Closed)
Patch Set: Use two Arrays insteaf of Array of Arrays in ElemHidingTracer Created Feb. 13, 2017, 10:31 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | lib/devtools.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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.selectors = [];
198 this.filters = [];
198 199
199 this.changedNodes = []; 200 this.changedNodes = [];
200 this.timeout = null; 201 this.timeout = null;
202 this.started = false;
201 203
202 this.observer = new MutationObserver(this.observe.bind(this)); 204 this.observer = new MutationObserver(this.observe.bind(this));
203 this.trace = this.trace.bind(this); 205 this.trace = this.trace.bind(this);
204
205 if (document.readyState == "loading")
206 document.addEventListener("DOMContentLoaded", this.trace);
207 else
208 this.trace();
209 } 206 }
210 ElementHidingTracer.prototype = { 207 ElementHidingTracer.prototype = {
211 checkNodes: function(nodes) 208 start: function()
212 { 209 {
213 var matchedSelectors = []; 210 let _start = () => {
211 this.trace();
Sebastian Noack 2017/02/13 15:09:17 Please correct me if I'm wrong, but it seems this
wspee 2017/02/14 09:28:40 Done.
212 this.started = true;
213 };
214 214
215 // Find all selectors that match any hidden element inside the given nodes. 215 if (document.readyState == "loading")
216 for (var i = 0; i < this.selectors.length; i++) 216 document.addEventListener("DOMContentLoaded", _start);
217 else
218 _start();
219 },
220
221 addFilters: function(selectors, filters)
222 {
223 if (this.started)
224 window.setTimeout(() => {
225 this.checkNodes([document], selectors, filters);
226 }, 0);
227
228 Array.prototype.push.apply(this.selectors, selectors);
Sebastian Noack 2017/02/13 15:09:17 As mentioned before, please use the rest operator
wspee 2017/02/14 09:28:40 Done.
229 Array.prototype.push.apply(this.filters, filters);
230 },
231
232 checkNodes: function(nodes, selectors, filters)
233 {
234 let matchedFilters = [];
235
236 for (let i = 0; i < selectors.length; i++)
217 { 237 {
218 var selector = this.selectors[i]; 238 let selector = selectors[i];
Sebastian Noack 2017/02/13 15:09:17 It seems "selector" and "filter" are only used onc
wspee 2017/02/14 09:28:40 Done.
239 let filter = filters[i];
219 240
220 for (var j = 0; j < nodes.length; j++) 241 nodes: for (let node of nodes)
221 { 242 {
222 var elements = nodes[j].querySelectorAll(selector); 243 let elements = node.querySelectorAll(selector);
223 var matched = false;
224 244
225 for (var k = 0; k < elements.length; k++) 245 for (let element of elements)
226 { 246 {
227 // Only consider selectors that actually have an effect on the 247 // Only consider selectors that actually have an effect on the
228 // computed styles, and aren't overridden by rules with higher 248 // computed styles, and aren't overridden by rules with higher
229 // priority, or haven't been circumvented in a different way. 249 // priority, or haven't been circumvented in a different way.
230 if (getComputedStyle(elements[k]).display == "none") 250 if (getComputedStyle(element).display == "none")
231 { 251 {
232 matchedSelectors.push(selector); 252 matchedFilters.push(filter.replace(/^.*?##/, ""));
233 matched = true; 253 break nodes;
234 break;
235 } 254 }
236 } 255 }
237
238 if (matched)
239 break;
240 } 256 }
241 } 257 }
242 258
243 if (matchedSelectors.length > 0) 259 if (matchedFilters.length > 0)
244 ext.backgroundPage.sendMessage({ 260 ext.backgroundPage.sendMessage({
245 type: "devtools.traceElemHide", 261 type: "devtools.traceElemHide",
246 selectors: matchedSelectors 262 selectors: matchedFilters
247 }); 263 });
248 }, 264 },
249 265
250 onTimeout: function() 266 onTimeout: function()
251 { 267 {
252 this.checkNodes(this.changedNodes); 268 this.checkNodes(this.changedNodes, this.selectors, this.filters);
253 this.changedNodes = []; 269 this.changedNodes = [];
254 this.timeout = null; 270 this.timeout = null;
255 }, 271 },
256 272
257 observe: function(mutations) 273 observe: function(mutations)
258 { 274 {
259 // Forget previously changed nodes that are no longer in the DOM. 275 // Forget previously changed nodes that are no longer in the DOM.
260 for (var i = 0; i < this.changedNodes.length; i++) 276 for (var i = 0; i < this.changedNodes.length; i++)
261 { 277 {
262 if (!document.contains(this.changedNodes[i])) 278 if (!document.contains(this.changedNodes[i]))
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
305 321
306 // Check only nodes whose descendants have changed, and not more often 322 // 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 323 // than once a second. Otherwise large pages with a lot of DOM mutations
308 // (like YouTube) freeze when the devtools panel is active. 324 // (like YouTube) freeze when the devtools panel is active.
309 if (this.timeout == null) 325 if (this.timeout == null)
310 this.timeout = setTimeout(this.onTimeout.bind(this), 1000); 326 this.timeout = setTimeout(this.onTimeout.bind(this), 1000);
311 }, 327 },
312 328
313 trace: function() 329 trace: function()
314 { 330 {
315 this.checkNodes([document]); 331 this.checkNodes([document], this.selectors, this.filters);
316 332
317 this.observer.observe( 333 this.observer.observe(
318 document, 334 document,
319 { 335 {
320 childList: true, 336 childList: true,
321 attributes: true, 337 attributes: true,
322 subtree: true 338 subtree: true
323 } 339 }
324 ); 340 );
325 }, 341 },
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after
483 var shadow = shadowRoot(this); 499 var shadow = shadowRoot(this);
484 return shadow == ourShadowRoot ? null : shadow; 500 return shadow == ourShadowRoot ? null : shadow;
485 } 501 }
486 }); 502 });
487 }, null); 503 }, null);
488 } 504 }
489 505
490 return shadow; 506 return shadow;
491 }, 507 },
492 508
493 addSelectors: function(selectors) 509 addSelectors: function(selectors, filters)
494 { 510 {
495 if (selectors.length == 0) 511 if (!selectors.length)
496 return; 512 return;
497 513
514 if (this.tracer)
515 {
516 if (!filters)
517 {
518 filters = [];
519 Array.prototype.push.apply(filters, selectors);
Sebastian Noack 2017/02/13 15:09:17 As indicated before, I'm not sure if it is a good
wspee 2017/02/14 09:28:40 Done.
520 }
521 this.tracer.addFilters(selectors, filters);
522 }
523
498 if (!this.style) 524 if (!this.style)
499 { 525 {
500 // Create <style> element lazily, only if we add styles. Add it to 526 // Create <style> element lazily, only if we add styles. Add it to
501 // the shadow DOM if possible. Otherwise fallback to the <head> or 527 // the shadow DOM if possible. Otherwise fallback to the <head> or
502 // <html> element. If we have injected a style element before that 528 // <html> element. If we have injected a style element before that
503 // has been removed (the sheet property is null), create a new one. 529 // has been removed (the sheet property is null), create a new one.
504 this.style = document.createElement("style"); 530 this.style = document.createElement("style");
505 (this.shadow || document.head 531 (this.shadow || document.head
506 || document.documentElement).appendChild(this.style); 532 || document.documentElement).appendChild(this.style);
507 533
(...skipping 29 matching lines...) Expand all
537 { 563 {
538 var selector = selectors.slice(i, i + this.selectorGroupSize).join(", "); 564 var selector = selectors.slice(i, i + this.selectorGroupSize).join(", ");
539 this.style.sheet.insertRule(selector + "{display: none !important;}", 565 this.style.sheet.insertRule(selector + "{display: none !important;}",
540 this.style.sheet.cssRules.length); 566 this.style.sheet.cssRules.length);
541 } 567 }
542 }, 568 },
543 569
544 apply: function() 570 apply: function()
545 { 571 {
546 var selectors = null; 572 var selectors = null;
547 var elemHideEmulationLoaded = false;
548 573
549 var checkLoaded = function() 574 var checkLoaded = function()
550 { 575 {
551 if (!selectors || !elemHideEmulationLoaded)
552 return;
553
554 if (this.tracer) 576 if (this.tracer)
555 this.tracer.disconnect(); 577 this.tracer.disconnect();
556 this.tracer = null; 578 this.tracer = null;
557 579
580 if (selectors.trace)
581 this.tracer = new ElementHidingTracer();
582
558 if (this.style && this.style.parentElement) 583 if (this.style && this.style.parentElement)
559 this.style.parentElement.removeChild(this.style); 584 this.style.parentElement.removeChild(this.style);
560 this.style = null; 585 this.style = null;
561 586
562 this.addSelectors(selectors.selectors); 587 if (selectors.selectors)
588 this.addSelectors(selectors.selectors);
589
563 this.elemHideEmulation.apply(); 590 this.elemHideEmulation.apply();
564 591
565 if (selectors.trace) 592 if (this.tracer)
566 this.tracer = new ElementHidingTracer(selectors.selectors); 593 this.tracer.start();
567 }.bind(this); 594 }.bind(this);
568 595
569 ext.backgroundPage.sendMessage({type: "get-selectors"}, function(response) 596 ext.backgroundPage.sendMessage({type: "get-selectors"}, response =>
570 { 597 {
571 selectors = response; 598 selectors = response;
572 checkLoaded(); 599 this.elemHideEmulation.load(checkLoaded);
573 });
574
575 this.elemHideEmulation.load(function()
576 {
577 elemHideEmulationLoaded = true;
578 checkLoaded();
579 }); 600 });
580 } 601 }
581 }; 602 };
582 603
583 if (document instanceof HTMLDocument) 604 if (document instanceof HTMLDocument)
584 { 605 {
585 checkSitekey(); 606 checkSitekey();
586 wrapWebSocket(); 607 wrapWebSocket();
587 608
588 var elemhide = new ElemHide(); 609 var elemhide = new ElemHide();
589 elemhide.apply(); 610 elemhide.apply();
590 611
591 document.addEventListener("error", function(event) 612 document.addEventListener("error", function(event)
592 { 613 {
593 checkCollapse(event.target); 614 checkCollapse(event.target);
594 }, true); 615 }, true);
595 616
596 document.addEventListener("load", function(event) 617 document.addEventListener("load", function(event)
597 { 618 {
598 var element = event.target; 619 var element = event.target;
599 if (/^i?frame$/.test(element.localName)) 620 if (/^i?frame$/.test(element.localName))
600 checkCollapse(element); 621 checkCollapse(element);
601 }, true); 622 }, true);
602 } 623 }
OLDNEW
« no previous file with comments | « no previous file | lib/devtools.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld