[Greasemonkey] Cross-script configuration data sharing

Gareth Andrew freega at freegarethandrew.org
Tue Jan 10 00:08:07 EST 2006


Just to make this debate a little more concrete I hacked together a
version of greasemonkey that makes this happen.
The modified GM_ScriptStorage (miscapis.js) object:

function GM_ScriptStorage(script) {
  this.prefMan = new GM_PrefManager(["scriptvals.",
                                     script.namespace,
                                     "/",
                                     script.name,
                                     "."].join(""));
  this.publicPrefMan = new GM_PrefManager("publiccriptvals.");
}

GM_ScriptStorage.prototype.setValue = function(name, val, namespace){
	if(typeof(namespace) != "undefined"){
			this.publicPrefMan.setValue(namespace + name, val);
			return;
	}
	this.prefMan.setValue(name, val);
}

GM_ScriptStorage.prototype.getValue = function(name, defVal, namespace)
{
	if(typeof(namespace) != "undefined"){
		return this.publicPrefMan.getValue(namespace + name,     defVal);
	}
      return this.prefMan.getValue(name, defVal);
}

Basically I add an extra (optional) parameter to get/setValue that if
specified specifies that the object is to be accessed is in the public
namespace, if omitted it is assumed that the object to be accessed is in
the script private namespace.  The private and public spaces are
completely partitioned so there is no way to access the private
namespace from another script.

I think probably on reflection I'd rather have a separate function
(exportValue/importValue) than overload set/getValue, which would also
mean the namespace parameter could be put in a more natural position in
the arguments list.   
If a new function is created then it's also possible that namespace
could be an optional argument (defaulting to "" or the script
namespace?), or entirely left out since it is only prepended to the name
argument anyway.

If everyone's not tired of this thread already, what do you think?  

Gareth.



More information about the Greasemonkey mailing list