Skip to content

Commit

Permalink
Override default settings through url parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromebon committed Oct 28, 2021
1 parent 68c4d22 commit cb9ccc9
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
7 changes: 6 additions & 1 deletion src/js/page/main-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,12 @@ export default class MainController {
}

async _loadSettings() {
const settings = await storage.get('settings');
let settings = await storage.get('settings');
settings = Object.assign(
{},
settings,
this._settingsUi.getSettingsOverride(),
);
if (settings) this._settingsUi.setSettings(settings);
}

Expand Down
50 changes: 47 additions & 3 deletions src/js/page/ui/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,32 @@ export default class Settings {
_onReset() {
this._resetRipple.animate();
const oldSettings = this.getSettings();
const overrides = this.getSettingsOverride();

// Set all inputs according to their initial attributes
for (const inputEl of this._globalInputs) {
if (inputEl.type === 'checkbox') {
inputEl.checked = inputEl.hasAttribute('checked');
inputEl.checked = Object.prototype.hasOwnProperty.call(
overrides,
inputEl.name,
)
? overrides[inputEl.name]
: inputEl.hasAttribute('checked');
} else if (inputEl.type === 'range') {
this._sliderMap.get(inputEl).value = inputEl.getAttribute('value');
this._sliderMap.get(inputEl).value =
Object.prototype.hasOwnProperty.call(overrides, inputEl.name)
? overrides[inputEl.name]
: inputEl.getAttribute('value');
}
}

for (const inputEl of this._pluginInputs) {
inputEl.checked = inputEl.hasAttribute('checked');
inputEl.checked = Object.prototype.hasOwnProperty.call(
overrides.plugins,
inputEl.name,
)
? overrides.plugins[inputEl.name]
: inputEl.hasAttribute('checked');
}

this.emitter.emit('reset', oldSettings);
Expand Down Expand Up @@ -140,4 +154,34 @@ export default class Settings {

return output;
}

getSettingsOverride() {
const searchParams = new URLSearchParams(window.location.search);

const overrides = {
plugins: {},
};

for (const inputEl of this._globalInputs) {
if (!searchParams.has(inputEl.name) || inputEl.name === 'original')
continue;

const value = searchParams.get(inputEl.name);
if (inputEl.type === 'checkbox' && ['true', 'false'].includes(value)) {
overrides[inputEl.name] = value === 'true';
} else if (inputEl.type === 'range') {
const rangeValue = Number.parseInt(value, 10);
if (rangeValue < inputEl.min || inputEl.max < rangeValue) continue;
overrides[inputEl.name] = rangeValue;
}
}

for (const inputEl of this._pluginInputs) {
const value = searchParams.get(inputEl.name);
if (!['true', 'false'].includes(value)) continue;
overrides.plugins[inputEl.name] = value === 'true';
}

return overrides;
}
}

0 comments on commit cb9ccc9

Please sign in to comment.