I've written a user script for Greasemonkey that requires some user configuration. To specify how they want the script to behave, a user needs to set a couple variables.
Right now, the script is set up like this:
// ==UserScript==
// @name My script
// @description A simplified example
// @include http://www.example.com/
// @version 0.0.1
// @updateURL https://www.example.com/myscript.meta.js
// ==/UserScript==
// Configuration
var config1 = "on";
var config2 = "off";
// Programs
[various functions that refer to the configuration variables]
I'd like to be able to update the script using Greasemonkey's automatic updates, while leaving the user's configuration lines intact. Basically, I don't want to force every user to redo their configuration after each update.
Is there a good method for updating a Greasemonkey userscript while leaving some configuration intact?
You may wish to utilise the greasemonkey functions GM_getValue() and GM_setValue(), which will store values that stay that way until they're changed again. The script can set values based on user needs, and get the value where required.
Specific GM functions require a special grant, in the metadata for the userscript:
// @grant GM_getValue
// @grant GM_setValue
Your code with the GM value functions may look like this:
var value = 18;
GM_setValue('dataName', value);
if (GM_getValue('dataName') == 18) ...
When you make updates to the script, instead of having the values set by GM_setValue overwritten, you could first check to see if they have been written:
var value = 18;
if (typeof GM_getValue('dataName') === 'undefined')
GM_setValue('dataName', value);
To enable users to control these settings, you could inject an HTML interface that shows the values of the database entries, and allows them to be set. This approach would then make such settings immune to script updates (as long as the script doesn't overwrite the database values).
Also, this question may also be a beneficial read.