Talk:Greasemonkey Manual:API: Difference between revisions

From GreaseSpot Wiki
Jump to navigationJump to search
m (Reverted edits by 189.81.153.24 (Talk) to last revision by Marti)
Line 1: Line 1:
== GM_getObject/GM_setObject ==


I think these two additional functions might be handy:
{{Function|GM_getObject|name, default}}<br/>
{{Function|GM_setObject|name, value}}
==== Implementation: ====
function GM_getObject(name, defaultValue) {
var s = GM_getValue(name);
if (s === undefined) {
return defaultValue;
}
else {
return JSON.parse(s);
}
}
function GM_setObject(name, value) {
GM_setValue(name, JSON.stringify(value));
}

Revision as of 16:43, 26 July 2010

GM_getObject/GM_setObject

I think these two additional functions might be handy:

function GM_getObject( name, default )
function GM_setObject( name, value )

Implementation:

function GM_getObject(name, defaultValue) {
	var s = GM_getValue(name);

	if (s === undefined) {
		return defaultValue;
	}
	else {
		return JSON.parse(s);
	}
}

function GM_setObject(name, value) {
	GM_setValue(name, JSON.stringify(value));
}