Talk:Greasemonkey Manual:API: Difference between revisions

From GreaseSpot Wiki
Jump to navigationJump to search
m (Note to Aavindraa)
 
m (Arantius moved page User:Greasemonkey Manual:API to Talk:Greasemonkey Manual:API over redirect: revert)
 
(49 intermediate revisions by 28 users not shown)
Line 1: Line 1:
'''@Aavindraa''' Consider where to put in a brief blurb about XPathResult in this page please... this is also part of the API and because this is now the main entrance to anything API related, it should be mentioned rather than having someone go hunt for it via the GM Manual TOC on the far right under environment. Thanks [[User:Marti|Marti]] 05:41, 4 January 2009 (EST)
== 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));
}
 
: It is the general policy of Greasemonkey to ''not'' implement a special API just to build something that a user script can easily do on its own. Something like this is a perfect candidate for a reusable component in the [[:Category:@require Library]]. [[User:Arantius|Arantius]] 12:55, 27 July 2010 (UTC)

Latest revision as of 23:31, 2 October 2023

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));
}
It is the general policy of Greasemonkey to not implement a special API just to build something that a user script can easily do on its own. Something like this is a perfect candidate for a reusable component in the Category:@require Library. Arantius 12:55, 27 July 2010 (UTC)