Index: include.preload.js |
=================================================================== |
--- a/include.preload.js |
+++ b/include.preload.js |
@@ -339,18 +339,19 @@ |
} |
}; |
function ElemHide() |
{ |
this.shadow = this.createShadowTree(); |
this.style = null; |
this.tracer = null; |
- this.inject = true; |
+ this.inline = true; |
this.emulatedPatterns = null; |
+ this.injectedSelectors = []; |
this.elemHideEmulation = new ElemHideEmulation( |
this.addSelectors.bind(this), |
this.hideElements.bind(this) |
); |
} |
ElemHide.prototype = { |
selectorGroupSize: 200, |
@@ -374,18 +375,39 @@ |
// avoid creating the shadowRoot twice. |
let shadow = document.documentElement.shadowRoot || |
document.documentElement.createShadowRoot(); |
shadow.appendChild(document.createElement("shadow")); |
return shadow; |
}, |
- injectSelectors(selectors, filters) |
+ injectSelectors(selectors) |
{ |
+ if (selectors.length == 0 && this.injectedSelectors.length == 0) |
+ return; |
+ |
+ let message = {type: "elemhide.injectSelectors"}; |
+ |
+ if (selectors.length > 0) |
+ message.selectors = selectors; |
+ |
+ if (this.injectedSelectors.length > 0) |
+ message.previousSelectors = this.injectedSelectors; |
+ |
+ chrome.runtime.sendMessage(message); |
+ |
+ this.injectedSelectors = selectors; |
+ }, |
+ |
+ addSelectorsInline(selectors) |
+ { |
+ if (selectors.length == 0) |
+ return; |
+ |
if (!this.style) |
{ |
// Create <style> element lazily, only if we add styles. Add it to |
// the shadow DOM if possible. Otherwise fallback to the <head> or |
// <html> element. If we have injected a style element before that |
// has been removed (the sheet property is null), create a new one. |
this.style = document.createElement("style"); |
(this.shadow || document.head || |
@@ -429,40 +451,37 @@ |
).join(", "); |
this.style.sheet.insertRule(selector + "{display: none !important;}", |
this.style.sheet.cssRules.length); |
} |
}, |
addSelectors(selectors, filters) |
{ |
- if (selectors.length == 0) |
- return; |
- |
- if (this.inject) |
+ if (this.inline) |
{ |
// Insert the style rules inline if we have been instructed by the |
// background page to do so. This is usually the case, except on platforms |
// that do support user stylesheets via the chrome.tabs.insertCSS API |
// (Firefox 53 onwards for now and possibly Chrome in the near future). |
// Once all supported platforms have implemented this API, we can remove |
// the code below. See issue #5090. |
// Related Chrome and Firefox issues: |
// https://bugs.chromium.org/p/chromium/issues/detail?id=632009 |
// https://bugzilla.mozilla.org/show_bug.cgi?id=1310026 |
- this.injectSelectors(selectors, filters); |
+ this.addSelectorsInline(selectors); |
} |
else |
{ |
- chrome.runtime.sendMessage({ |
- type: "elemhide.injectSelectors", |
- selectors |
- }); |
+ this.injectSelectors(selectors); |
} |
+ if (selectors.length == 0) |
+ return; |
+ |
if (this.tracer) |
this.tracer.addSelectors(selectors, filters); |
}, |
hideElements(elements, filters) |
{ |
for (let element of elements) |
hideElement(element); |
@@ -487,19 +506,19 @@ |
if (this.style && this.style.parentElement) |
this.style.parentElement.removeChild(this.style); |
this.style = null; |
if (response.trace) |
this.tracer = new ElementHidingTracer(); |
- this.inject = response.inject; |
+ this.inline = response.inline; |
- if (this.inject) |
+ if (this.inline) |
this.addSelectors(response.selectors); |
else if (this.tracer) |
this.tracer.addSelectors(response.selectors); |
this.elemHideEmulation.apply(response.emulatedPatterns); |
}); |
} |
}; |