options: load and save params

This commit is contained in:
He4eT 2024-08-15 20:47:15 +02:00
commit 48a86cea74
3 changed files with 88 additions and 11 deletions

View file

@ -1 +1,34 @@
// browser.storage.local.set({'params': {scaleFactor: 16}})
import {
fields,
assureParams,
getParamsFromStorage,
setParamsToStorage,
} from '../../scripts/params.js'
Promise.resolve()
.then(getParamsFromStorage)
.then(assureParams)
.then(fillForm)
.then(addFormHandler)
function fillForm (params) {
Object.entries(params).forEach(([key, value]) => {
const input = document.getElementById(key)
input.value = value
input.dispatchEvent(new Event('input'))
})
}
function addFormHandler () {
const form = document.getElementById('settingsForm')
form.onsubmit = (e) => {
e.preventDefault()
const formData = new FormData(e.target)
const entries = [...formData.entries()]
.filter(([key]) => fields.includes(key))
const params = Object.fromEntries(entries)
setParamsToStorage(params)
}
}