feat: add advanced context actions and optional translation

This commit is contained in:
golem
2026-10-05 15:38:16 -06:00
parent 33c4dad00f
commit 5308932bf1
34 changed files with 1579 additions and 159 deletions
+22 -6
View File
@@ -1,4 +1,10 @@
import { DEFAULT_SETTINGS, OPEN_IN_VALUES, MAX_MODEL_CHARS } from "./defaults.js";
import { DEFAULT_SETTINGS, OPEN_IN_VALUES } from "./defaults.js";
export function normalizeLanguage(value) {
if (typeof value !== "string") return "";
const trimmed = value.trim();
return [...trimmed].length <= 100 && !/[\r\n\x00-\x1f]/u.test(trimmed) ? trimmed : "";
}
// Pure. Layers stored values over the defaults, dropping unknown keys and
// falling back on wrong types or out-of-range values. A fresh or signed-out
@@ -8,9 +14,11 @@ export function mergeSettings(stored) {
if (stored === null || typeof stored !== "object") {
return merged;
}
if (typeof stored.model === "string" && stored.model.length <= MAX_MODEL_CHARS) {
merged.model = stored.model;
}
merged.contextMenuMode = ["off", "simple", "advanced"].includes(stored.contextMenuMode)
? stored.contextMenuMode : stored.showContextMenu === false ? "off" : "simple";
if (typeof stored.showTranslate === "boolean") merged.showTranslate = stored.showTranslate;
if (["ask", "preferred"].includes(stored.translationMode)) merged.translationMode = stored.translationMode;
merged.translationLanguage = normalizeLanguage(stored.translationLanguage);
if (typeof stored.temporaryChat === "boolean") {
merged.temporaryChat = stored.temporaryChat;
}
@@ -33,7 +41,7 @@ export function mergeSettings(stored) {
// there is nothing useful to branch on.
export async function load() {
try {
const stored = await browser.storage.sync.get(Object.keys(DEFAULT_SETTINGS));
const stored = await browser.storage.sync.get([...Object.keys(DEFAULT_SETTINGS), "showContextMenu"]);
return mergeSettings(stored);
} catch {
return mergeSettings(null);
@@ -41,5 +49,13 @@ export async function load() {
}
export async function save(partial) {
await browser.storage.sync.set(partial);
const normalized = mergeSettings(partial);
const patch = {};
if (partial && typeof partial === "object") {
for (const key of Object.keys(DEFAULT_SETTINGS)) {
if (Object.hasOwn(partial, key)) patch[key] = normalized[key];
}
if (Object.hasOwn(partial, "showContextMenu")) patch.contextMenuMode = normalized.contextMenuMode;
}
await browser.storage.sync.set(patch);
}