[Greasemonkey] storing arrays with GM_setValue

chris feldmann cfeldmann at gmail.com
Sat Jun 4 03:06:47 EDT 2005


No matter how I try to serialize an array through GM_setValue, I get an 
error: (roughly) "Attempt to set a preference with variable type: Object."

But I really want to store an array in there. Here's what I've tossed 
together. Can more experienced js devs tell me if it's stupid? Is there a 
simpler way to do this? Included find the three operative functions and some 
test commands in classic GM anonymous function style (this appears to do 
what I want but may be too slow on most machines?) Once you store the array, 
you could pull it back into javascript to push or pop it:

(function() 
{

	function setGMArray(array, key){
		for (i=0; i<array.length; i++){
			GM_setValue(key+i , array[i]);
		}
	}
	
	function countGMArray(key) // utility function
	{
		var dummy; 
		var j=0; 
		while(dummy = GM_getValue(key+j)){
			j++;
		}
		return j;
	}
		
	function getGMArray(key)
	{
		var extracted = [];
		var k=0;
		var count = countGMArray(key);
		for(k;k<count;k++){
			extracted[k] = GM_getValue(key+k);
		}
		return extracted;
	}
	
	var arrayLiteral =['username1', 'username2', 'username3'];
	
	setGMArray(arrayLiteral, 'stored');
	
	var output = getGMArray('stored');
	
	for(var m=0; m<output.length;m++){
		alert(output[m]);
	}
		

})();
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mozdev.org/pipermail/greasemonkey/attachments/20050604/23e74b1e/attachment.htm


More information about the Greasemonkey mailing list