Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Draft] Override default settings through url parameters #174 #209

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be a spread instead

{},
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;
}
}