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

Unified Diff: include.preload.js

Issue 29410607: Issue 5090 - Use user stylesheets for element hiding if possible (Closed)
Patch Set: Update message and file names Created July 9, 2017, 12:07 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 | « ext/background.js ('k') | 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
@@ -529,16 +529,17 @@
}, randomEventName);
}
function ElemHide()
{
this.shadow = this.createShadowTree();
this.style = null;
this.tracer = null;
+ this.inject = true;
this.elemHideEmulation = new ElemHideEmulation(
window,
callback =>
{
ext.backgroundPage.sendMessage({
type: "filters.get",
what: "elemhideemulation"
@@ -591,21 +592,18 @@
}
});
}, null);
}
return shadow;
},
- addSelectors(selectors, filters)
+ injectSelectors(selectors, filters)
{
- 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 ||
@@ -645,37 +643,68 @@
for (let i = 0; i < preparedSelectors.length; i += this.selectorGroupSize)
{
let selector = preparedSelectors.slice(
i, i + this.selectorGroupSize
).join(", ");
this.style.sheet.insertRule(selector + "{display: none !important;}",
this.style.sheet.cssRules.length);
}
+ },
+
+ addSelectors(selectors, filters)
+ {
+ if (!selectors || 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);
+ }
+ else
+ {
+ ext.backgroundPage.sendMessage({type: "elemhide.injectSelectors",
+ selectors});
+ }
if (this.tracer)
this.tracer.addSelectors(selectors, filters);
},
apply()
{
- ext.backgroundPage.sendMessage({type: "get-selectors"}, response =>
+ ext.backgroundPage.sendMessage({type: "elemhide.getSelectors"}, response =>
{
if (this.tracer)
this.tracer.disconnect();
this.tracer = null;
if (this.style && this.style.parentElement)
this.style.parentElement.removeChild(this.style);
this.style = null;
if (response.trace)
this.tracer = new ElementHidingTracer();
- this.addSelectors(response.selectors);
+ this.inject = response.inject;
+
+ if (this.inject)
+ this.addSelectors(response.selectors);
+ else if (this.tracer)
+ this.tracer.addSelectors(response.selectors);
+
this.elemHideEmulation.apply();
});
}
};
if (document instanceof HTMLDocument)
{
checkSitekey();
« no previous file with comments | « ext/background.js ('k') | lib/cssInjection.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld