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

Side by Side Diff: chrome/content/elemHideEmulation.js

Issue 29494577: Issue 5438 - Observer DOM changes to reapply filters. (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Created July 21, 2017, 7:53 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« chrome/content/.eslintrc.json ('K') | « chrome/content/.eslintrc.json ('k') | no next file » | 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-2017 eyeo GmbH 3 * Copyright (C) 2006-2017 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 299 matching lines...) Expand 10 before | Expand all | Expand 10 after
310 } 310 }
311 }; 311 };
312 312
313 function ElemHideEmulation(window, getFiltersFunc, addSelectorsFunc, 313 function ElemHideEmulation(window, getFiltersFunc, addSelectorsFunc,
314 hideElemsFunc) 314 hideElemsFunc)
315 { 315 {
316 this.window = window; 316 this.window = window;
317 this.getFiltersFunc = getFiltersFunc; 317 this.getFiltersFunc = getFiltersFunc;
318 this.addSelectorsFunc = addSelectorsFunc; 318 this.addSelectorsFunc = addSelectorsFunc;
319 this.hideElemsFunc = hideElemsFunc; 319 this.hideElemsFunc = hideElemsFunc;
320 this.observer = new MutationObserver(this.observe.bind(this));
Wladimir Palant 2017/07/21 21:37:28 new window.MutationObserver?
hub 2017/07/22 01:31:24 https://developer.mozilla.org/en-US/docs/Web/API/M
hub 2017/07/23 15:36:34 My bad. will actually do that.
320 } 321 }
321 322
322 ElemHideEmulation.prototype = { 323 ElemHideEmulation.prototype = {
323 isSameOrigin(stylesheet) 324 isSameOrigin(stylesheet)
324 { 325 {
325 try 326 try
326 { 327 {
327 return new URL(stylesheet.href).origin == this.window.location.origin; 328 return new URL(stylesheet.href).origin == this.window.location.origin;
328 } 329 }
329 catch (e) 330 catch (e)
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after
480 } 481 }
481 } 482 }
482 } 483 }
483 484
484 this.addSelectorsFunc(selectors, selectorFilters); 485 this.addSelectorsFunc(selectors, selectorFilters);
485 this.hideElemsFunc(elements, elementFilters); 486 this.hideElemsFunc(elements, elementFilters);
486 }, 487 },
487 488
488 _stylesheetQueue: null, 489 _stylesheetQueue: null,
489 490
491 queueFiltering()
492 {
493 if (!this._stylesheetQueue &&
494 Date.now() - this._lastInvocation < MIN_INVOCATION_INTERVAL)
495 {
496 this._stylesheetQueue = [];
497 this.window.setTimeout(() =>
498 {
499 let stylesheets = this._stylesheetQueue;
500 this._stylesheetQueue = null;
501 this.addSelectors(stylesheets);
502 }, MIN_INVOCATION_INTERVAL - (Date.now() - this._lastInvocation));
503 }
504 },
505
490 onLoad(event) 506 onLoad(event)
491 { 507 {
492 let stylesheet = event.target.sheet; 508 let stylesheet = event.target.sheet;
493 if (stylesheet) 509 if (stylesheet)
494 { 510 {
495 if (!this._stylesheetQueue && 511 this.queueFiltering();
496 Date.now() - this._lastInvocation < MIN_INVOCATION_INTERVAL)
497 {
498 this._stylesheetQueue = [];
499 this.window.setTimeout(() =>
500 {
501 let stylesheets = this._stylesheetQueue;
502 this._stylesheetQueue = null;
503 this.addSelectors(stylesheets);
504 }, MIN_INVOCATION_INTERVAL - (Date.now() - this._lastInvocation));
505 }
506 512
507 if (this._stylesheetQueue) 513 if (this._stylesheetQueue)
508 this._stylesheetQueue.push(stylesheet); 514 this._stylesheetQueue.push(stylesheet);
509 else 515 else
510 this.addSelectors([stylesheet]); 516 this.addSelectors([stylesheet]);
511 } 517 }
512 }, 518 },
513 519
520 observe(mutations)
521 {
522 for (let mutation of mutations)
523 {
524 if (mutation.type == "childList")
525 {
526 this.queueFiltering();
Wladimir Palant 2017/07/21 21:37:28 a) Reapplying the rules on the entire document is
hub 2017/07/22 01:31:24 Acknowledged.
527 break;
528 }
529 }
530 },
531
514 apply() 532 apply()
515 { 533 {
516 this.getFiltersFunc(patterns => 534 this.getFiltersFunc(patterns =>
517 { 535 {
518 this.patterns = []; 536 this.patterns = [];
519 for (let pattern of patterns) 537 for (let pattern of patterns)
520 { 538 {
521 let selectors = this.parseSelector(pattern.selector); 539 let selectors = this.parseSelector(pattern.selector);
522 if (selectors != null && selectors.length > 0) 540 if (selectors != null && selectors.length > 0)
523 this.patterns.push({selectors, text: pattern.text}); 541 this.patterns.push({selectors, text: pattern.text});
524 } 542 }
525 543
526 if (this.patterns.length > 0) 544 if (this.patterns.length > 0)
527 { 545 {
528 let {document} = this.window; 546 let {document} = this.window;
529 this.addSelectors(); 547 this.addSelectors();
548 this.observer.observe(
549 document,
550 {
551 childList: true,
552 subtree: true
553 }
554 );
530 document.addEventListener("load", this.onLoad.bind(this), true); 555 document.addEventListener("load", this.onLoad.bind(this), true);
531 } 556 }
532 }); 557 });
533 } 558 }
534 }; 559 };
OLDNEW
« chrome/content/.eslintrc.json ('K') | « chrome/content/.eslintrc.json ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld