diff --git a/monkeyconfig.js b/monkeyconfig.js index 4676ae9..a0a8a33 100755 --- a/monkeyconfig.js +++ b/monkeyconfig.js @@ -8,7 +8,7 @@ /* * MonkeyConfig * version 0.1.3 - * + * * Copyright (c) 2011-2013 Michal Wojciechowski (odyniec.net) */ @@ -30,10 +30,10 @@ function MonkeyConfig() { container, /* Darkened overlay used in the layer display mode */ overlay; - + /** - * Initialize configuration - * + * Initialize configuration + * * @param newData New data object */ function init(newData) { @@ -41,32 +41,36 @@ function MonkeyConfig() { if (data) { params = data.parameters || data.params; - + if (data.buttons === undefined) /* Set default buttons */ data.buttons = [ 'save', 'defaults', 'cancel' ]; - + if (data.title === undefined) /* - * If GM_getMetadata is available, get the name of the script + * If GM_getMetadata or GM_info are available, get the name of the script * and use it in the dialog title */ - if (typeof GM_getMetadata == 'function') { - var scriptName = GM_getMetadata('name'); - data.title = scriptName + ' Configuration'; + var scriptName; + + if (typeof GM_getMetadata === 'function') { // Scriptish + scriptName = GM_getMetadata('name'); } - else - data.title = 'Configuration'; + else if (typeof GM_info !== 'undefined') { // Greasemonkey, Tampermonkey &c. + scriptName = GM_info.script.name; + } + + data.title = (scriptName ? scriptName + ' ' : '') + 'Configuration'; } - - /* Make a safe version of title to be used as stored value identifier */ + + /* Make a safe version of title to be used as stored value identifier */ var safeTitle = data && data.title ? data.title.replace(/[^a-zA-Z0-9]/g, '_') : ''; storageKey = '_MonkeyConfig_' + safeTitle + '_cfg'; - + var storedValues; - + /* Load stored values (if present) */ if (GM_getValue(storageKey)) storedValues = JSON.parse(GM_getValue(storageKey)); @@ -77,7 +81,7 @@ function MonkeyConfig() { set(name, params[name].value); /* Check if there's a stored value for this parameter */ else if (storedValues && storedValues[name] !== undefined) - set(name, storedValues[name]); + set(name, storedValues[name]); /* Otherwise, set the default value (if defined) */ else if (params[name]['default'] !== undefined) set(name, params[name]['default']); @@ -89,7 +93,7 @@ function MonkeyConfig() { /* Add an item to the User Script Commands menu */ var caption = data.menuCommand !== true ? data.menuCommand : data.title; - + GM_registerMenuCommand(caption, function () { cfg.open(); }); } @@ -102,30 +106,30 @@ function MonkeyConfig() { update(); }; } - + /** * Get the value of a configuration parameter - * + * * @param name Name of the configuration parameter * @returns Value of the configuration parameter */ function get(name) { return values[name]; } - + /** * Set the value of a configuration parameter - * + * * @param name Name of the configuration parameter * @param value New value of the configuration parameter */ function set(name, value) { values[name] = value; } - + /** * Reset configuration parameters to default values - */ + */ function setDefaults() { for (var name in params) { if (typeof params[name]['default'] !== 'undefined') { @@ -133,7 +137,7 @@ function MonkeyConfig() { } } } - + /** * Render the configuration dialog */ @@ -151,7 +155,7 @@ function MonkeyConfig() { /* Render buttons */ for (var button in data.buttons) { html += ''; - + switch (data.buttons[button]) { case 'cancel': html += '