function resolve_field (type) {
    return type === "checkbox" ? "checked" : "value";
}

document.addEventListener('DOMContentLoaded', async function () {
    const names  = ["token", "group", "header", "domain", "blacklist", /*"delay",*/ "posts", "likes", "private", "screenshot"];
    const storage = await chrome.storage.local.get();

    for (let i = 0; i < names.length; i++) {
        const name = names[i];
        const element = document.getElementById(name);

        if (element) {
            const field = resolve_field(element.type);

            let saved = storage[name];

            if (saved) {
                element[field] = saved;
            }

            element.addEventListener("change", async (evt) => {
                await chrome.storage.local.set({ [name]: evt.target[field] });
            });
        }
    }
});
