diff --git a/extension/background.js b/extension/background.js index 68b8400..c794baa 100644 --- a/extension/background.js +++ b/extension/background.js @@ -892,13 +892,7 @@ async function importSettings(file) { // A saved key belongs to the host it was saved for (options.js savedKeyApplies): a key whose platform row, or the // Obsidian address, points at another origin after an import is dropped, never sent there; the user enters it again. async function dropMovedKeys(before, after) { - const origin = (url) => { - try { - return new URL(String(url).trim()).origin; - } catch { - return ""; - } - }; + const origin = MdgSettings.urlOrigin; const origins = (settings) => new Map((Array.isArray(settings[AI_PROVIDERS]) ? settings[AI_PROVIDERS] : []).map((p) => [String(p?.id || "").trim(), origin(p?.baseUrl)])); const [was, now] = [origins(before), origins(after)]; const keys = await loadAiProviderKeys(); @@ -912,9 +906,6 @@ async function dropMovedKeys(before, after) { const { aiApiKeys: AI_PROVIDER_KEYS_STORAGE, aiProviderList: AI_PROVIDERS } = MdgLimits.KEYS; -// An AI platform's address as requests use it: no spaces, no trailing slashes. -const trimBaseUrl = (url) => String(url || "").trim().replace(/\/+$/, ""); - function normalizeAiProvider(item) { if (!item || typeof item !== "object") return null; const id = String(item.id || "").trim(); @@ -923,7 +914,7 @@ function normalizeAiProvider(item) { id, presetId: String(item.presetId || "custom"), name: String(item.name || "").trim() || MdgErrors.LABEL.customAi, - baseUrl: trimBaseUrl(item.baseUrl), + baseUrl: MdgSettings.trimBaseUrl(item.baseUrl), model: String(item.model || "").trim(), temperature: typeof item.temperature === "number" ? item.temperature : 0.7, requiresKey: item.requiresKey !== false, @@ -1264,7 +1255,7 @@ async function testAiConnection({ baseUrl, apiKey, model, requiresKey }) { return { ok: false, error: problem.text, code: problem.code }; } return probeAiChatCompletion({ - baseUrl: trimBaseUrl(baseUrl), + baseUrl: MdgSettings.trimBaseUrl(baseUrl), apiKey, model: String(model).trim() }); diff --git a/extension/options.js b/extension/options.js index 8c9bc0a..1020ed5 100644 --- a/extension/options.js +++ b/extension/options.js @@ -513,7 +513,7 @@ function syncObsidianKeyTag() { // While a save or a test runs it stays disabled; setBusy(null) calls this again. // The result next to it is for the 地址 and Key it tested: an edit away from them clears it. function syncObsidianTest() { - const typed = { obsidianApiBaseUrl: normalizeBaseUrl(elements.obsidianApiBaseUrl.value), obsidianApiKey: MdgSettings.normalizeApiKey(elements.obsidianApiKey.value) }; + const typed = { obsidianApiBaseUrl: MdgSettings.trimBaseUrl(elements.obsidianApiBaseUrl.value), obsidianApiKey: MdgSettings.normalizeApiKey(elements.obsidianApiKey.value) }; if (obsidianTest && obsidianTest.target !== obsidianTarget(typed)) { obsidianTests.next(); setTestResult(""); @@ -552,7 +552,7 @@ function readFormPayload() { const normalized = MdgSettings.normalizeSyncSettings(form); return { ...Object.fromEntries(Object.keys(form).map((k) => [k, normalized[k]])), - obsidianApiBaseUrl: normalizeBaseUrl(elements.obsidianApiBaseUrl.value), + obsidianApiBaseUrl: MdgSettings.trimBaseUrl(elements.obsidianApiBaseUrl.value), obsidianApiKey: MdgSettings.normalizeApiKey(elements.obsidianApiKey.value) }; } @@ -988,11 +988,6 @@ function closeAllFixedPropertyMenus() { }); } -function normalizeBaseUrl(value) { - return String(value || "").trim().replace(/\/+$/g, ""); -} - - async function testConnection() { clearInputErrors(); const payload = collectFormPayload(); @@ -1118,7 +1113,7 @@ function addAiProviderRow(item = {}) { row.className = "ai-provider-row"; row.dataset.providerId = id; row.dataset.hasSavedKey = hasSavedKey ? "1" : "0"; - row.dataset.keyOrigin = urlOrigin(baseUrl); + row.dataset.keyOrigin = MdgSettings.urlOrigin(baseUrl); row.dataset.currentPresetId = presetId; row.innerHTML = `