Greasemonkey’s GM_getValue/GM_setValue functions for Google Chrome

Posted in Greasemonkey

[ Update, Mar 16th, 2011: Firefox 4's Greasemonkey changed GM_getValue and it doesn't support .toString() anymore, code updated accordingly. Added GM_deleteValue ]

As there’s no implementation for GM_getValue/setValue in user scripts (Greasemonkey) for Google Chrome, it makes a lot of user scripts unsupported.

Here’s a little code that brings the support of these methods to Chrome, by saving data in localStorage (HTML5′s way to store data on client side).

Include it in your Greasemonkey script to have it working on Chrome.

if (!this.GM_getValue || (this.GM_getValue.toString && this.GM_getValue.toString().indexOf("not supported")>-1)) {
	this.GM_getValue=function (key,def) {
		return localStorage[key] || def;
	};
	this.GM_setValue=function (key,value) {
		return localStorage[key]=value;
	};
	this.GM_deleteValue=function (key) {
		return delete localStorage[key];
	};
}

This makes Google Reader Filter available for Chrome.

Tags: , ,


  • Gurjeet Singh

    Well, this sure did not work for my ‘GMail toggle search’ script : http://userscripts.org/scripts/show/50486

    Will take cue from the example at http://people.w3.org/mike/localstorage.html

  • http://devign.me/ elado

    @Gurjeet Singh

    Just debugged your script with Chrome, you have a different problem on your script. It might be just Chrome related if it works on Firefox.
    I checked, and the GM_getValue/setValue seem to work fine there.

  • Gurjeet Singh

    @elado thanks for the idea.

    My script worked by replacing localStorage with sessionStorage. localStorage doesn’t seem to work across iframes. And I used getItem()/setItem() functions.

  • phaze75

    Great script! I have a feature request: please make it configurable per feed! Thanks!

  • Vaithy Soft

    the given grease monkey function is not working

  • http://www.esrun.co.uk/blog/ Esrun

    Very useful, thank you. I expanded a little bit on your code to support GM_listValues.

        this.GM_listValues=function () {    var localKeys = new Array();    for (key in localStorage) {localKeys.push(key)}    return localKeys;    };

  • http://www.facebook.com/elado Elad Ossadon

    Thanks!
    Looks like you could use Object.keys(localStorage) to get the keys instead of a loop.

  • http://twitter.com/tambnguyen ★★★ Tam Nguyen ★★★

Page optimized by WP Minify WordPress Plugin