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

Unified 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.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« chrome/content/.eslintrc.json ('K') | « chrome/content/.eslintrc.json ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/content/elemHideEmulation.js
===================================================================
--- a/chrome/content/elemHideEmulation.js
+++ b/chrome/content/elemHideEmulation.js
@@ -312,16 +312,17 @@
function ElemHideEmulation(window, getFiltersFunc, addSelectorsFunc,
hideElemsFunc)
{
this.window = window;
this.getFiltersFunc = getFiltersFunc;
this.addSelectorsFunc = addSelectorsFunc;
this.hideElemsFunc = hideElemsFunc;
+ 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.
}
ElemHideEmulation.prototype = {
isSameOrigin(stylesheet)
{
try
{
return new URL(stylesheet.href).origin == this.window.location.origin;
@@ -482,53 +483,77 @@
}
this.addSelectorsFunc(selectors, selectorFilters);
this.hideElemsFunc(elements, elementFilters);
},
_stylesheetQueue: null,
+ queueFiltering()
+ {
+ if (!this._stylesheetQueue &&
+ Date.now() - this._lastInvocation < MIN_INVOCATION_INTERVAL)
+ {
+ this._stylesheetQueue = [];
+ this.window.setTimeout(() =>
+ {
+ let stylesheets = this._stylesheetQueue;
+ this._stylesheetQueue = null;
+ this.addSelectors(stylesheets);
+ }, MIN_INVOCATION_INTERVAL - (Date.now() - this._lastInvocation));
+ }
+ },
+
onLoad(event)
{
let stylesheet = event.target.sheet;
if (stylesheet)
{
- if (!this._stylesheetQueue &&
- Date.now() - this._lastInvocation < MIN_INVOCATION_INTERVAL)
- {
- this._stylesheetQueue = [];
- this.window.setTimeout(() =>
- {
- let stylesheets = this._stylesheetQueue;
- this._stylesheetQueue = null;
- this.addSelectors(stylesheets);
- }, MIN_INVOCATION_INTERVAL - (Date.now() - this._lastInvocation));
- }
+ this.queueFiltering();
if (this._stylesheetQueue)
this._stylesheetQueue.push(stylesheet);
else
this.addSelectors([stylesheet]);
}
},
+ observe(mutations)
+ {
+ for (let mutation of mutations)
+ {
+ if (mutation.type == "childList")
+ {
+ 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.
+ break;
+ }
+ }
+ },
+
apply()
{
this.getFiltersFunc(patterns =>
{
this.patterns = [];
for (let pattern of patterns)
{
let selectors = this.parseSelector(pattern.selector);
if (selectors != null && selectors.length > 0)
this.patterns.push({selectors, text: pattern.text});
}
if (this.patterns.length > 0)
{
let {document} = this.window;
this.addSelectors();
+ this.observer.observe(
+ document,
+ {
+ childList: true,
+ subtree: true
+ }
+ );
document.addEventListener("load", this.onLoad.bind(this), true);
}
});
}
};
« 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