48 lines
1.5 KiB
JavaScript
48 lines
1.5 KiB
JavaScript
const extOptions = globalThis.browser ?? globalThis.chrome;
|
|
|
|
function runtimeSend(message) {
|
|
return new Promise((resolve, reject) => {
|
|
extOptions.runtime.sendMessage(message, (response) => {
|
|
const error = extOptions.runtime.lastError;
|
|
if (error) {
|
|
reject(new Error(error.message));
|
|
return;
|
|
}
|
|
resolve(response);
|
|
});
|
|
});
|
|
}
|
|
|
|
async function loadSettings() {
|
|
const response = await runtimeSend({ type: "keepassgo-load-settings" });
|
|
if (!response?.success) {
|
|
throw new Error(response?.error || "Could not load settings.");
|
|
}
|
|
document.getElementById("grpc-address").value = response.settings.grpcAddress || "";
|
|
document.getElementById("bearer-token").value = response.settings.bearerToken || "";
|
|
}
|
|
|
|
async function saveSettings(event) {
|
|
event.preventDefault();
|
|
const status = document.getElementById("settings-status");
|
|
status.textContent = "Saving…";
|
|
try {
|
|
const response = await runtimeSend({
|
|
type: "keepassgo-save-settings",
|
|
settings: {
|
|
grpcAddress: document.getElementById("grpc-address").value,
|
|
bearerToken: document.getElementById("bearer-token").value
|
|
}
|
|
});
|
|
if (!response?.success) {
|
|
throw new Error(response?.error || "Could not save settings.");
|
|
}
|
|
status.textContent = "Saved.";
|
|
} catch (error) {
|
|
status.textContent = error instanceof Error ? error.message : String(error);
|
|
}
|
|
}
|
|
|
|
document.getElementById("settings-form").addEventListener("submit", saveSettings);
|
|
void loadSettings();
|