| Index: include.preload.js |
| diff --git a/include.preload.js b/include.preload.js |
| index 86e52c32afceb894f6591a84b0eca3b1842383ce..f94dcb0941e3546dbaac177aec5b07503bb24d17 100644 |
| --- a/include.preload.js |
| +++ b/include.preload.js |
| @@ -192,64 +192,76 @@ function getContentDocument(element) |
| } |
| } |
| -function ElementHidingTracer(selectors) |
| +function ElementHidingTracer() |
| { |
| - this.selectors = selectors; |
| + this.selectors = []; |
| + this.filters = []; |
| this.changedNodes = []; |
| this.timeout = null; |
| + this.started = false; |
| this.observer = new MutationObserver(this.observe.bind(this)); |
| this.trace = this.trace.bind(this); |
| - |
| - if (document.readyState == "loading") |
| - document.addEventListener("DOMContentLoaded", this.trace); |
| - else |
| - this.trace(); |
| } |
| ElementHidingTracer.prototype = { |
| - checkNodes: function(nodes) |
| + start: function() |
| { |
| - var matchedSelectors = []; |
| + if (document.readyState == "loading") |
| + document.addEventListener("DOMContentLoaded", this.trace); |
| + else |
| + this.trace(); |
| + }, |
| - // Find all selectors that match any hidden element inside the given nodes. |
| - for (var i = 0; i < this.selectors.length; i++) |
| - { |
| - var selector = this.selectors[i]; |
| + addSelectors: function(selectors, filters) |
| + { |
| + if (!filters) |
| + filters = new Array(selectors.length); |
| + |
| + if (this.started) |
| + window.setTimeout(() => { |
| + this.checkNodes([document], selectors, filters); |
| + }, 0); |
| + |
| + this.selectors.push(...selectors); |
| + this.filters.push(...filters); |
| + }, |
| + |
| + checkNodes: function(nodes, selectors, filters) |
| + { |
| + let matchedFilters = []; |
| - for (var j = 0; j < nodes.length; j++) |
| + for (let i = 0; i < selectors.length; i++) |
| + { |
| + nodes: for (let node of nodes) |
| { |
| - var elements = nodes[j].querySelectorAll(selector); |
| - var matched = false; |
| + let elements = node.querySelectorAll(selectors[i]); |
| - for (var k = 0; k < elements.length; k++) |
| + for (let element of elements) |
| { |
| // Only consider selectors that actually have an effect on the |
| // computed styles, and aren't overridden by rules with higher |
| // priority, or haven't been circumvented in a different way. |
| - if (getComputedStyle(elements[k]).display == "none") |
| + if (getComputedStyle(element).display == "none") |
| { |
| - matchedSelectors.push(selector); |
| - matched = true; |
| - break; |
| + let filter = filters[i] || selectors[i]; |
| + matchedFilters.push(filter.replace(/^.*?##/, "")); |
| + break nodes; |
| } |
| } |
| - |
| - if (matched) |
| - break; |
| } |
| } |
| - if (matchedSelectors.length > 0) |
| + if (matchedFilters.length > 0) |
| ext.backgroundPage.sendMessage({ |
| type: "devtools.traceElemHide", |
| - selectors: matchedSelectors |
| + selectors: matchedFilters |
| }); |
| }, |
| onTimeout: function() |
| { |
| - this.checkNodes(this.changedNodes); |
| + this.checkNodes(this.changedNodes, this.selectors, this.filters); |
| this.changedNodes = []; |
| this.timeout = null; |
| }, |
| @@ -312,7 +324,8 @@ ElementHidingTracer.prototype = { |
| trace: function() |
| { |
| - this.checkNodes([document]); |
| + this.started = true; |
| + this.checkNodes([document], this.selectors, this.filters); |
| this.observer.observe( |
| document, |
| @@ -490,11 +503,14 @@ ElemHide.prototype = { |
| return shadow; |
| }, |
| - addSelectors: function(selectors) |
| + addSelectors: function(selectors, filters) |
| { |
| - if (selectors.length == 0) |
| + if (!selectors.length) |
| return; |
| + if (this.tracer) |
| + this.tracer.addSelectors(selectors, filters); |
| + |
| if (!this.style) |
| { |
| // Create <style> element lazily, only if we add styles. Add it to |
| @@ -544,38 +560,33 @@ ElemHide.prototype = { |
| apply: function() |
| { |
| var selectors = null; |
| - var elemHideEmulationLoaded = false; |
| var checkLoaded = function() |
| { |
| - if (!selectors || !elemHideEmulationLoaded) |
| - return; |
| - |
| if (this.tracer) |
| this.tracer.disconnect(); |
| this.tracer = null; |
| + if (selectors.trace) |
| + this.tracer = new ElementHidingTracer(); |
| + |
| if (this.style && this.style.parentElement) |
| this.style.parentElement.removeChild(this.style); |
| this.style = null; |
| - this.addSelectors(selectors.selectors); |
| + if (selectors.selectors) |
| + this.addSelectors(selectors.selectors); |
| + |
| this.elemHideEmulation.apply(); |
| - if (selectors.trace) |
| - this.tracer = new ElementHidingTracer(selectors.selectors); |
| + if (this.tracer) |
| + this.tracer.start(); |
| }.bind(this); |
| - ext.backgroundPage.sendMessage({type: "get-selectors"}, function(response) |
| + ext.backgroundPage.sendMessage({type: "get-selectors"}, response => |
| { |
| selectors = response; |
| - checkLoaded(); |
| - }); |
| - |
| - this.elemHideEmulation.load(function() |
| - { |
| - elemHideEmulationLoaded = true; |
| - checkLoaded(); |
| + this.elemHideEmulation.load(checkLoaded); |
| }); |
| } |
| }; |