chromium-proxy-settings/options.ts
Mathieu Strypsteen d2738078e0
All checks were successful
Build / build (push) Successful in 43s
Make active toggle take effect immediately
2024-03-13 09:28:46 +01:00

45 lines
1.8 KiB
TypeScript

import { Storage } from './common';
async function saveProxy() {
const host = (document.getElementById("proxyHost") as HTMLInputElement).value;
const port = (document.getElementById("proxyPort") as HTMLInputElement).valueAsNumber;
const bypass = (document.getElementById("proxyBypass") as HTMLInputElement).value;
if (!host || !port) {
const active = document.getElementById("proxyActive") as HTMLInputElement;
active.disabled = true;
active.checked = false;
await toggleProxy();
}
await chrome.storage.local.set({ host: host, port: port, bypass: bypass.split("\n") });
}
async function toggleProxy() {
await chrome.storage.session.set({ active: (document.getElementById("proxyActive") as HTMLInputElement).checked });
}
async function fillForm() {
(document.getElementById("proxySettings") as HTMLInputElement).addEventListener("submit", () => {
void saveProxy();
});
(document.getElementById("proxyActive") as HTMLInputElement).addEventListener("click", () => {
void toggleProxy();
});
const { host, port, bypass } = await chrome.storage.local.get(["host", "port", "bypass"]) as Storage;
const { active } = await chrome.storage.session.get("active");
if (host) {
(document.getElementById("proxyHost") as HTMLInputElement).value = host;
}
if (port) {
(document.getElementById("proxyPort") as HTMLInputElement).valueAsNumber = port;
}
if (bypass) {
(document.getElementById("proxyBypass") as HTMLInputElement).value = bypass.join("\n");
}
if (host && port) {
(document.getElementById("proxyActive") as HTMLInputElement).disabled = false;
if (active) {
(document.getElementById("proxyActive") as HTMLInputElement).checked = true;
}
}
}
window.addEventListener("DOMContentLoaded", () => {
void fillForm();
});