chromium-proxy-settings/options.ts

46 lines
1.8 KiB
TypeScript
Raw Permalink Normal View History

2024-02-18 14:10:13 +01:00
import { Storage } from './common';
async function saveProxy() {
2024-02-18 10:27:44 +01:00
const host = (document.getElementById("proxyHost") as HTMLInputElement).value;
2024-02-18 14:10:13 +01:00
const port = (document.getElementById("proxyPort") as HTMLInputElement).valueAsNumber;
2024-02-18 10:27:44 +01:00
const bypass = (document.getElementById("proxyBypass") as HTMLInputElement).value;
2024-02-18 08:28:00 +01:00
if (!host || !port) {
const active = document.getElementById("proxyActive") as HTMLInputElement;
active.disabled = true;
active.checked = false;
await toggleProxy();
2024-02-18 08:28:00 +01:00
}
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 });
2024-02-18 08:28:00 +01:00
}
2024-02-18 14:10:13 +01:00
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");
2024-02-18 08:28:00 +01:00
if (host) {
(document.getElementById("proxyHost") as HTMLInputElement).value = host;
}
if (port) {
2024-02-18 14:10:13 +01:00
(document.getElementById("proxyPort") as HTMLInputElement).valueAsNumber = port;
2024-02-18 08:28:00 +01:00
}
2024-02-18 10:27:44 +01:00
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;
}
}
2024-02-18 14:10:13 +01:00
}
window.addEventListener("DOMContentLoaded", () => {
void fillForm();
2024-02-18 08:28:00 +01:00
});