Created
January 21, 2014 17:29
-
-
Save Ventero/8544318 to your computer and use it in GitHub Desktop.
Greasemonkey user variable definition example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function makeUserModifiable(varName, defaultVal, parseFunc) { | |
if(typeof parseFunc !== "function") | |
parseFunc = function(a) { return a; }; | |
var currentVal = GM_getValue(varName, defaultVal); | |
GM_registerMenuCommand("Set variable " + varName, function() { | |
var val = prompt("Value for " + varName, currentVal); | |
GM_setValue(varName, val); | |
// if you need live modification, uncomment this (requires varName to be a | |
// global variable) | |
// window[varName] = parseFunc(val); | |
}); | |
return parseFunc(currentVal); | |
} | |
var var1 = makeUserModifiable("var1", "foo"); | |
var var2 = makeUserModifiable("var2", "foo1,foo2,foo3", function(a){return a.split(/,\s*/);}); | |
console.log(var1); | |
console.log(var2.join(" - ")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment