From 378e415f18585f88fe08fd6f3ccb89941183f5b1 Mon Sep 17 00:00:00 2001 From: William Toohey Date: Thu, 20 Oct 2016 00:03:24 +1000 Subject: [PATCH] Query string settings support. Closes @22 --- README.md | 8 ++++++++ src/js/HuesSettings.js | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/README.md b/README.md index dd229ef..c31d5a5 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,14 @@ var defaults = { ## Settings object See [HuesSettings.js](./src/js/HuesSettings.js#L29) for the possible options you can put into the `defaults` object. +## Query string +Any setting that can go in the `defaults` object can also be dynamically specified in the URL. +For example: http://0x40.mon.im/custom.html?packs=BIOS.zip,kitchen.zip¤tUI=v4.20 + +There are two special settings here: +* `firstSong` can just be written as `song`. +* Anything given as `packs` or `respacks` will be appended to the respacks specified in the `defaults` object, as opposed to overwriting them. + ## Building Install [Node.js](https://nodejs.org/en/), v5 preferred. Install the required packages for the build: diff --git a/src/js/HuesSettings.js b/src/js/HuesSettings.js index 36abe63..e82aa0f 100644 --- a/src/js/HuesSettings.js +++ b/src/js/HuesSettings.js @@ -30,6 +30,9 @@ const defaultSettings = { // Location relative to root - where do the audio/zip workers live // This is required because Web Workers need an absolute path workersPath : "lib/workers/", + // ONLY USED FOR QUERY STRINGS this will be prepended to any respacks + // passed in as a ?packs=query + respackPath : "respacks/", // Debugging var, for loading zips or not load : true, // Debug, play first song automatically? @@ -85,6 +88,7 @@ const ephemeralSettings = [ "autoplay", "overwriteLocal", "respacks", + "respackPath", "firstSong", "firstImage", "disableRemoteResources", @@ -255,6 +259,42 @@ class HuesSettings { } this.defaults = defaults; + + // Override with our query string + let querySettings = this.getQuerySettings(); + this.defaults.respacks = this.defaults.respacks.concat(querySettings.respacks); + for(let attr in querySettings) { + if(querySettings.hasOwnProperty(attr) && attr != "respacks") { + this.defaults[attr] = querySettings[attr]; + + if(ephemeralSettings.indexOf(attr) == -1) { + // TODO: Everything that checks localStorage for settings should + // change to get() to preserve local changes + localStorage[attr] = querySettings[attr]; + } + } + } + } + + getQuerySettings() { + let results = {}; + results.respacks = []; + let query = window.location.search.substring(1); + let vars = query.split("&"); + for (let i=0;i