70 lines
2.4 KiB
JavaScript
70 lines
2.4 KiB
JavaScript
import { AccountStore } from "../src/cloudfile/account-store.js";
|
|
|
|
const accountId = new URL(location.href).searchParams.get("accountId");
|
|
const form = document.getElementById("account-form");
|
|
const statusNode = document.getElementById("status");
|
|
const accountIdNode = document.getElementById("account-id");
|
|
|
|
const accountStore = new AccountStore();
|
|
|
|
function setStatus(message, isError = false) {
|
|
statusNode.textContent = message;
|
|
statusNode.style.color = isError ? "#8a1f11" : "";
|
|
}
|
|
|
|
function formDataToConfig(formElement) {
|
|
const data = new FormData(formElement);
|
|
return {
|
|
baseUrl: String(data.get("baseUrl") || "").trim(),
|
|
uploadAppPath: String(data.get("uploadAppPath") || "").trim() || "/",
|
|
uploadPassword: String(data.get("uploadPassword") || "").trim(),
|
|
defaultRetention: String(data.get("defaultRetention") || "").trim() || "604800",
|
|
customSid: String(data.get("customSid") || "").trim()
|
|
};
|
|
}
|
|
|
|
async function load() {
|
|
if (!accountId) {
|
|
setStatus("Missing Thunderbird cloudFile accountId.", true);
|
|
form.querySelector("button").disabled = true;
|
|
return;
|
|
}
|
|
|
|
accountIdNode.textContent = accountId;
|
|
|
|
const config = await accountStore.get(accountId);
|
|
if (!config) {
|
|
return;
|
|
}
|
|
|
|
document.getElementById("base-url").value = config.baseUrl || "";
|
|
document.getElementById("upload-app-path").value = config.uploadAppPath || "/";
|
|
document.getElementById("upload-password").value = config.uploadPassword || "";
|
|
document.getElementById("default-retention").value = config.defaultRetention || "604800";
|
|
document.getElementById("custom-sid").value = config.customSid || "";
|
|
}
|
|
|
|
form.addEventListener("submit", async event => {
|
|
event.preventDefault();
|
|
|
|
if (!accountId) {
|
|
setStatus("Cannot save without an accountId.", true);
|
|
return;
|
|
}
|
|
|
|
try {
|
|
const config = formDataToConfig(form);
|
|
await accountStore.set(accountId, config);
|
|
await browser.cloudFile.updateAccount(accountId, {
|
|
configured: Boolean(config.baseUrl),
|
|
managementUrl: browser.runtime.getURL(`ui/account.html?accountId=${encodeURIComponent(accountId)}`)
|
|
});
|
|
setStatus("Settings saved.");
|
|
} catch (caughtError) {
|
|
setStatus(caughtError instanceof Error ? caughtError.message : String(caughtError), true);
|
|
}
|
|
});
|
|
|
|
load().catch(caughtError => {
|
|
setStatus(caughtError instanceof Error ? caughtError.message : String(caughtError), true);
|
|
});
|