downloader-extension/js/settings.js

65 lines
2.1 KiB
JavaScript

export async function api_url(api_path = "") {
const domain = await get_saved_domain();
if (!domain) throw Error("No Domain Set");
if (api_path.startsWith('/')) api_path = api_path.substring(1);
return domain + api_path;
}
const get_saved_domain = async () => (await browser.storage.local.get("domain")).domain;
const set_saved_domain = async (domain) => await browser.storage.local.set({ domain });
const get_saved_debug = async () => (await browser.storage.local.get("debug")).debug;
const set_saved_debug = async (enabled) => await browser.storage.local.set({ debug: enabled });
function enable_domain_input() {
const save_button = document.getElementById("save_domain");
const host_input = document.getElementById("host");
const disable_save_button = () => save_button.setAttribute('disabled', true);
const enable_save_button = () => save_button.removeAttribute('disabled');
async function populate_domain_textbox_from_storage() {
const domain = await get_saved_domain()
if (!domain) return;
host_input.value = domain;
disable_save_button();
}
async function domain_input_changed() {
const saved_domain = await get_saved_domain();
const current_domain = host_input.value;
if(saved_domain !== current_domain) enable_save_button();
else disable_save_button();
}
async function save_domain_input_to_storage() {
if (!host_input.value.endsWith("/"))
host_input.value = host_input.value + "/";
const saved = set_saved_domain(host_input.value);
const granted = await browser.permissions.request({
origins: [host_input.value]
});
if (!granted) throw new Error("Permission denied");
await saved;
disable_save_button();
}
populate_domain_textbox_from_storage()
host_input.addEventListener('change', domain_input_changed);
host_input.addEventListener('keypress', domain_input_changed);
host_input.addEventListener('keyup', domain_input_changed);
host_input.addEventListener('keydown', domain_input_changed);
save_button.addEventListener('click', save_domain_input_to_storage);
}
function enable_debug_toggle() {
}
export function enable() {
enable_domain_input();
enable_debug_toggle();
}