diff options
-rw-r--r-- | DOCS/man/javascript.rst | 16 | ||||
-rw-r--r-- | player/javascript/defaults.js | 52 |
2 files changed, 60 insertions, 8 deletions
diff --git a/DOCS/man/javascript.rst b/DOCS/man/javascript.rst index cd553510eb..5b446cea69 100644 --- a/DOCS/man/javascript.rst +++ b/DOCS/man/javascript.rst @@ -27,16 +27,16 @@ otherwise, the documented Lua options, script directories, loading, etc apply to JavaScript files too. Script initialization and lifecycle is the same as with Lua, and most of the Lua -functions at the modules ``mp``, ``mp.utils`` and ``mp.msg`` are available to -JavaScript with identical APIs - including running commands, getting/setting -properties, registering events/key-bindings/property-changes/hooks, etc. +functions at the modules ``mp``, ``mp.utils``, ``mp.msg`` and ``mp.options`` are +available to JavaScript with identical APIs - including running commands, +getting/setting properties, registering events/key-bindings/hooks, etc. Differences from Lua -------------------- -No need to load modules. ``mp``, ``mp.utils`` and ``mp.msg`` are preloaded, and -you can use e.g. ``var cwd = mp.utils.getcwd();`` without prior setup. -``mp.options`` is currently not implemented, but ``mp.get_opt(...)`` is. +No need to load modules. ``mp``, ``mp.utils``, ``mp.msg`` and ``mp.options`` +are preloaded, and you can use e.g. ``var cwd = mp.utils.getcwd();`` without +prior setup. Errors are slightly different. Where the Lua APIs return ``nil`` for error, the JavaScript ones return ``undefined``. Where Lua returns ``something, error`` @@ -87,8 +87,6 @@ Unsupported Lua APIs and their JS alternatives ``mp.dispatch_events([allow_wait])`` see event loop below. -``mp.options`` module is not implemented currently for JS. - Scripting APIs - identical to Lua --------------------------------- @@ -184,6 +182,8 @@ Otherwise, where the Lua APIs return ``nil`` on error, JS returns ``undefined``. ``mp.add_hook(type, priority, fn)`` +``mp.options.read_options(obj [, identifier])`` (types: string/boolean/number) + Additional utilities -------------------- diff --git a/player/javascript/defaults.js b/player/javascript/defaults.js index 56c83f8abb..c5c1f35161 100644 --- a/player/javascript/defaults.js +++ b/player/javascript/defaults.js @@ -420,6 +420,58 @@ function new_require(base_id) { g.require = new_require(SCRIPTDIR_META + "/" + main_script[1]); /********************************************************************** + * mp.options + *********************************************************************/ +function read_options(opts, id) { + id = String(typeof id != "undefined" ? id : mp.get_script_name()); + mp.msg.debug("reading options for " + id); + + var conf, fname = "~~/script-opts/" + id + ".conf"; + try { + conf = mp.utils.read_file(fname); + } catch (e) { + mp.msg.verbose(fname + " not found."); + } + + // data as config file lines array, or empty array + var data = conf ? conf.replace(/\r\n/g, "\n").split("\n") : [], + conf_len = data.length; // before we append script-opts below + + // Append relevant script-opts as <key-sans-id>=<value> to data + var sopts = mp.get_property_native("options/script-opts"), + prefix = id + "-"; + for (var key in sopts) { + if (key.indexOf(prefix) == 0) + data.push(key.substring(prefix.length) + "=" + sopts[key]); + } + + // Update opts from data + data.forEach(function(line, i) { + if (line[0] == "#" || line.trim() == "") + return; + + var key = line.substring(0, line.indexOf("=")), + val = line.substring(line.indexOf("=") + 1), + type = typeof opts[key], + info = i < conf_len ? fname + ":" + (i + 1) // 1-based line number + : "script-opts:" + prefix + key; + + if (!opts.hasOwnProperty(key)) + mp.msg.warn(info, "Ignoring unknown key '" + key + "'"); + else if (type == "string") + opts[key] = val; + else if (type == "boolean" && (val == "yes" || val == "no")) + opts[key] = (val == "yes"); + else if (type == "number" && val.trim() != "" && !isNaN(val)) + opts[key] = Number(val); + else + mp.msg.error(info, "Error: can't convert '" + val + "' to " + type); + }); +} + +mp.options = { read_options: read_options }; + +/********************************************************************** * various *********************************************************************/ g.print = mp.msg.info; // convenient alias |