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

Unified Diff: include.preload.js

Issue 29575739: Issue 5864 - Remove previous style sheet before adding one (Closed) Base URL: https://hg.adblockplus.org/adblockpluschrome/
Patch Set: Remove style sheet if selectors list is empty Created Oct. 14, 2017, 1:34 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
« no previous file with comments | « no previous file | lib/cssInjection.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: include.preload.js
===================================================================
--- a/include.preload.js
+++ b/include.preload.js
@@ -341,16 +341,17 @@
function ElemHide()
{
this.shadow = this.createShadowTree();
this.style = null;
this.tracer = null;
this.inject = 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)
{
// 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);
Manish Jethani 2017/10/14 13:38:18 injectSelectors is now addSelectorsInline; the new
}
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);
« no previous file with comments | « no previous file | lib/cssInjection.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld