Minimal Greasemonkey GM_*Value functions

By keesc Last update Jun 6, 2009 — Installed 351 times.
// ==UserScript==
// @name Minimal Greasemonkey GM_*Value functions
// @namespace      http://www.nexopia.com/users/kees/
// @description    This script emulates GM_setValue and GM_getValue
// @author         Kees Couprie
// @ujs:category browser: enhancements
// ==/UserScript==


// Emulate GM_setValue by storing the value as a cookie. Use a lifetime of one year.
function GM_setValue(theName, theValue) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate() + 365);
	document.cookie="GM_value_" + theName+ "=" + escape(theValue) + 
			";expires=" + exdate.toGMTString() + ";path=/" ;
}

// Emulate GM_getValue by retrieving a cookie.
function GM_getValue(theName, theDefault) {
	var theValue = theDefault;
	if (document.cookie.length > 0) {
	  var long_name = "GM_value_" + theName;
	  var theStart = document.cookie.indexOf(long_name + "=");
	  if (theStart != -1) {
	    theStart = theStart + long_name.length + 1;
	    var theEnd = document.cookie.indexOf(";", theStart);
	    if (theEnd == -1) theEnd = document.cookie.length;
	    theValue = unescape(document.cookie.substring(theStart, theEnd));
	  }
	}
	return theValue;
}