Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Script to save settings

Is there a way to save some settings to the local computer that is not cookies with a user script?

It is difficult to make a user script that is for multiple domains if the settings are not global.

From a comment: "I am using scriptish ".

like image 219
John Avatar asked Dec 13 '22 05:12

John


1 Answers

Absolutely, it's very easy. The Greasemonkey wiki documents four methods that allow you to deal with saving values, which can be settings or anything else you want to store:

  • GM_setValue(name, string)
  • GM_getValue(name[, default])
  • GM_deleteValue(name)
  • GM_listValues()

You might want to check out the main API page for other useful methods, and there's also a complete metadata block documentation page.

The only way this might not work is in a Google Chrome Content Script. There are a few solutions though: you can either use the Google Chrome GM_* userscript in addition to yours, or you can make the GM_setValue and GM_getValue methods available by including this at the beginning of your user script (from Devine.me):

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];
    };
}
like image 145
BenjaminRH Avatar answered Mar 27 '23 06:03

BenjaminRH