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

Unified Diff: lib/elemHide.js

Issue 6201308310667264: Issue 521- Inject our stylesheet on per-site basis rather than globally (Closed)
Patch Set: Created May 20, 2014, 6:09 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 | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/elemHide.js
===================================================================
--- a/lib/elemHide.js
+++ b/lib/elemHide.js
@@ -66,59 +66,90 @@ let styleURL = null;
let ElemHide = exports.ElemHide =
{
/**
* Indicates whether filters have been added or removed since the last apply() call.
* @type Boolean
*/
isDirty: false,
- /**
- * Inidicates whether the element hiding stylesheet is currently applied.
- * @type Boolean
- */
- applied: false,
+ QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver, Ci.nsISupportsWeakReference]),
/**
* Called on module startup.
*/
init: function()
{
TimeLine.enter("Entered ElemHide.init()");
Prefs.addListener(function(name)
{
if (name == "enabled")
ElemHide.apply();
});
Wladimir Palant 2014/05/20 19:36:12 This code is no longer necessary.
onShutdown.add(function()
{
- ElemHide.unapply();
- });
+ Services.obs.removeObserver(this, "content-document-global-created");
+ }.bind(this));
TimeLine.log("done adding prefs listener");
let styleFile = IO.resolveFilePath(Prefs.data_directory);
styleFile.append("elemhide.css");
styleURL = Services.io.newFileURI(styleFile).QueryInterface(Ci.nsIFileURL);
TimeLine.log("done determining stylesheet URL");
+ Services.obs.addObserver(this, "content-document-global-created", true);
+
TimeLine.leave("ElemHide.init() done");
},
/**
* Removes all known filters
*/
clear: function()
{
filterByKey = {__proto__: null};
keyByFilter = {__proto__: null};
knownExceptions = {__proto__: null};
exceptions = {__proto__: null};
ElemHide.isDirty = false;
- ElemHide.unapply();
+ },
+
+ observe: function (subject, topic, data, additional)
+ {
+ if (topic != "content-document-global-created")
+ return;
+
+ if (!Prefs.enabled)
+ return;
+
+ let uri = subject.document.documentURIObject;
+ if (!uri)
+ return;
+
+ switch(uri.scheme)
+ {
+ case "http":
+ case "https":
+ case "mailbox":
+ case "imap":
+ case "news":
+ case "snews":
+ break;
+ default:
+ // Avoid injecting in chrome documents
+ return;
+ }
Wladimir Palant 2014/05/20 19:36:12 Please use Policy.isBlockableScheme() here (from c
+
+ // XXX is there some better way to test if it's disabled on that domain that we should do before?
+ if (!ElemHide.anySelectorForDomain(uri.host))
+ return;
Wladimir Palant 2014/05/20 19:36:12 Wow, performance will go down the drain here. We c
+
+ var windowUtils = subject.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowUtils);
+ windowUtils.loadSheet(styleURL, Ci.nsIStyleSheetService.USER_SHEET);
},
/**
* Add a new element hiding filter
* @param {ElemHideFilter} filter
*/
add: function(filter)
{
@@ -205,54 +236,29 @@ let ElemHide = exports.ElemHide =
/**
* Will be set to true if an apply() call arrives while apply() is already
* running (delayed execution).
* @type Boolean
*/
_needsApply: false,
/**
- * Generates stylesheet URL and applies it globally
+ * Generates stylesheet URL.
*/
+ // XXX Rename
apply: function()
Wladimir Palant 2014/05/20 19:36:12 saveStylesheet()?
{
if (this._applying)
{
this._needsApply = true;
return;
}
TimeLine.enter("Entered ElemHide.apply()");
- if (!ElemHide.isDirty || !Prefs.enabled)
- {
- // Nothing changed, looks like we merely got enabled/disabled
- if (Prefs.enabled && !ElemHide.applied)
- {
- try
- {
- Utils.styleService.loadAndRegisterSheet(styleURL, Ci.nsIStyleSheetService.USER_SHEET);
- ElemHide.applied = true;
- }
- catch (e)
- {
- Cu.reportError(e);
- }
- TimeLine.log("Applying existing stylesheet finished");
- }
- else if (!Prefs.enabled && ElemHide.applied)
- {
- ElemHide.unapply();
- TimeLine.log("ElemHide.unapply() finished");
- }
-
- TimeLine.leave("ElemHide.apply() done (no file changes)");
- return;
- }
-
IO.writeToFile(styleURL.file, this._generateCSSContent(), function(e)
{
TimeLine.enter("ElemHide.apply() write callback");
this._applying = false;
// _generateCSSContent is throwing NS_ERROR_NOT_AVAILABLE to indicate that
// there are no filters. If that exception is passed through XPCOM we will
// see a proper exception here, otherwise a number.
@@ -269,33 +275,16 @@ let ElemHide = exports.ElemHide =
{
this._needsApply = false;
this.apply();
}
else if (!e)
{
ElemHide.isDirty = false;
- ElemHide.unapply();
- TimeLine.log("ElemHide.unapply() finished");
-
- if (!noFilters)
- {
- try
- {
- Utils.styleService.loadAndRegisterSheet(styleURL, Ci.nsIStyleSheetService.USER_SHEET);
- ElemHide.applied = true;
- }
- catch (e)
- {
- Cu.reportError(e);
- }
- TimeLine.log("Applying stylesheet finished");
- }
-
FilterNotifier.triggerListeners("elemhideupdate");
Wladimir Palant 2014/05/20 19:36:12 Apparently, this is dead code. This notification h
}
TimeLine.leave("ElemHide.apply() write callback done");
}.bind(this), "ElemHideWrite");
this._applying = true;
TimeLine.leave("ElemHide.apply() done", "ElemHideWrite");
@@ -356,35 +345,23 @@ let ElemHide = exports.ElemHide =
}
},
/**
* Unapplies current stylesheet URL
*/
unapply: function()
{
- if (ElemHide.applied)
- {
- try
- {
- Utils.styleService.unregisterSheet(styleURL, Ci.nsIStyleSheetService.USER_SHEET);
- }
- catch (e)
- {
- Cu.reportError(e);
- }
- ElemHide.applied = false;
- }
},
Wladimir Palant 2014/05/20 19:36:12 This function should be removed.
/**
* Retrieves the currently applied stylesheet URL
* @type String
*/
- get styleURL() ElemHide.applied ? styleURL.spec : null,
+ get styleURL() styleURL.spec,
Wladimir Palant 2014/05/20 19:36:12 This getter was also introduced in https://hg.adbl
/**
* Retrieves an element hiding filter by the corresponding protocol key
*/
getFilterByKey: function(/**String*/ key) /**Filter*/
{
return (key in filterByKey ? filterByKey[key] : null);
},
@@ -407,10 +384,23 @@ let ElemHide = exports.ElemHide =
if (specificOnly && (!domains || domains[""]))
continue;
if (filter.isActiveOnDomain(domain) && !this.getException(filter, domain))
result.push(filter.selector);
}
return result;
+ },
+
+ anySelectorForDomain: function(/**String*/ domain)
+ {
+ for (let key in filterByKey)
+ {
+ let filter = filterByKey[key];
+
+ if (filter.isActiveOnDomain(domain) && !this.getException(filter, domain))
+ return true;
+ }
+
+ return false;
}
};
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld