diff --git a/pages/_lib/connection-ui.js b/pages/_lib/connection-ui.js index b42382dc..fb4735d7 100644 --- a/pages/_lib/connection-ui.js +++ b/pages/_lib/connection-ui.js @@ -138,7 +138,7 @@ export async function injectConnectionUI({
@@ -178,7 +178,7 @@ export async function injectConnectionUI({ @@ -191,7 +191,7 @@ export async function injectConnectionUI({ @@ -203,7 +203,7 @@ export async function injectConnectionUI({
@@ -243,7 +243,7 @@ export async function injectConnectionUI({ @@ -257,7 +257,7 @@ export async function injectConnectionUI({ @@ -269,7 +269,7 @@ export async function injectConnectionUI({ @@ -313,7 +313,7 @@ export async function injectConnectionUI({ @@ -324,7 +324,7 @@ export async function injectConnectionUI({ @@ -347,7 +347,7 @@ export async function injectConnectionUI({ @@ -366,7 +366,7 @@ export async function injectConnectionUI({ @@ -379,7 +379,7 @@ export async function injectConnectionUI({
@@ -406,7 +406,7 @@ export async function injectConnectionUI({ @@ -431,7 +431,7 @@ export async function injectConnectionUI({
@@ -471,7 +471,7 @@ export async function injectConnectionUI({ @@ -484,7 +484,7 @@ export async function injectConnectionUI({ @@ -493,7 +493,7 @@ export async function injectConnectionUI({ __MSG_prefs_OptionText_anthropic_max_tokens__ @@ -514,6 +514,8 @@ export async function injectConnectionUI({ } }); + const getPrefixedId = (id) => `${modelId_prefix ? `${modelId_prefix}` : ''}${id}`; + // Bindings // const bindClick = (id, cb) => { const el = document.getElementById(id); if (el && typeof cb === 'function') el.addEventListener('click', cb); }; // const bindChange = (id, cb) => { const el = document.getElementById(id); if (el && typeof cb === 'function') el.addEventListener('change', cb); }; @@ -536,15 +538,15 @@ export async function injectConnectionUI({ conntype_select.addEventListener("change", (ev) => warn_Anthropic_VersionEmpty(modelId_prefix)); document.getElementById("chatgpt_web_project").addEventListener("input", validateCustomData_ChatGPTWeb); document.getElementById("chatgpt_web_custom_gpt").addEventListener("input", validateCustomData_ChatGPTWeb); - document.getElementById("chatgpt_api_key").addEventListener("change", (ev) => warn_ChatGPT_APIKeyEmpty(modelId_prefix)); - document.getElementById("ollama_host").addEventListener("change", (ev) => warn_Ollama_HostEmpty(modelId_prefix)); - document.getElementById("openai_comp_host").addEventListener("change", (ev) => warn_OpenAIComp_HostEmpty(modelId_prefix)); - document.getElementById("google_gemini_api_key").addEventListener("change", (ev) => warn_GoogleGemini_APIKeyEmpty(modelId_prefix)); - document.getElementById("anthropic_api_key").addEventListener("change", (ev) => warn_Anthropic_APIKeyEmpty(modelId_prefix)); - document.getElementById("anthropic_version").addEventListener("change", (ev) => warn_Anthropic_VersionEmpty(modelId_prefix)); - document.getElementById("openai_comp_host").addEventListener("input", resetOpenAICompConfigs); - document.getElementById("openai_comp_chat_name").addEventListener("input", resetOpenAICompConfigs); - document.getElementById("openai_comp_use_v1").addEventListener("input", resetOpenAICompConfigs); + document.getElementById(getPrefixedId("chatgpt_api_key")).addEventListener("change", (ev) => warn_ChatGPT_APIKeyEmpty(modelId_prefix)); + document.getElementById(getPrefixedId("ollama_host")).addEventListener("change", (ev) => warn_Ollama_HostEmpty(modelId_prefix)); + document.getElementById(getPrefixedId("openai_comp_host")).addEventListener("change", (ev) => warn_OpenAIComp_HostEmpty(modelId_prefix)); + document.getElementById(getPrefixedId("google_gemini_api_key")).addEventListener("change", (ev) => warn_GoogleGemini_APIKeyEmpty(modelId_prefix)); + document.getElementById(getPrefixedId("anthropic_api_key")).addEventListener("change", (ev) => warn_Anthropic_APIKeyEmpty(modelId_prefix)); + document.getElementById(getPrefixedId("anthropic_version")).addEventListener("change", (ev) => warn_Anthropic_VersionEmpty(modelId_prefix)); + document.getElementById(getPrefixedId("openai_comp_host")).addEventListener("input", () => resetOpenAICompConfigs(modelId_prefix)); + document.getElementById(getPrefixedId("openai_comp_chat_name")).addEventListener("input", () => resetOpenAICompConfigs(modelId_prefix)); + document.getElementById(getPrefixedId("openai_comp_use_v1")).addEventListener("input", () => resetOpenAICompConfigs(modelId_prefix)); showConnectionOptions(conntype_select); loadOpenAICompConfigs(); @@ -555,7 +557,7 @@ export async function injectConnectionUI({ warn_Anthropic_APIKeyEmpty(modelId_prefix); warn_Anthropic_VersionEmpty(modelId_prefix); - const passwordField_chatgpt_api_key = document.getElementById('chatgpt_api_key'); + const passwordField_chatgpt_api_key = document.getElementById(getPrefixedId('chatgpt_api_key')); const toggleIcon_chatgpt_api_key = document.getElementById('toggle_chatgpt_api_key'); const icon_img_chatgpt_api_key = document.getElementById('pwd-icon_chatgpt_api_key'); @@ -566,7 +568,7 @@ export async function injectConnectionUI({ icon_img_chatgpt_api_key.src = type === 'password' ? "/images/pwd-show.png" : "/images/pwd-hide.png"; }); - const passwordField_google_gemini_api_key = document.getElementById('google_gemini_api_key'); + const passwordField_google_gemini_api_key = document.getElementById(getPrefixedId('google_gemini_api_key')); const toggleIcon_google_gemini_api_key = document.getElementById('toggle_google_gemini_api_key'); const icon_img_google_gemini_api_key = document.getElementById('pwd-icon_google_gemini_api_key'); @@ -577,7 +579,7 @@ export async function injectConnectionUI({ icon_img_google_gemini_api_key.src = type === 'password' ? "/images/pwd-show.png" : "/images/pwd-hide.png"; }); - const passwordField_openai_comp_api_key = document.getElementById('openai_comp_api_key'); + const passwordField_openai_comp_api_key = document.getElementById(getPrefixedId('openai_comp_api_key')); const toggleIcon_openai_comp_api_key = document.getElementById('toggle_openai_comp_api_key'); const icon_img_openai_comp_api_key = document.getElementById('pwd-icon_openai_comp_api_key'); @@ -588,7 +590,7 @@ export async function injectConnectionUI({ icon_img_openai_comp_api_key.src = type === 'password' ? "/images/pwd-show.png" : "/images/pwd-hide.png"; }); - const passwordField_anthropic_api_key = document.getElementById('anthropic_api_key'); + const passwordField_anthropic_api_key = document.getElementById(getPrefixedId('anthropic_api_key')); const toggleIcon_anthropic_api_key = document.getElementById('toggle_anthropic_api_key'); const icon_img_anthropic_api_key = document.getElementById('pwd-icon_anthropic_api_key'); @@ -632,20 +634,20 @@ export async function injectConnectionUI({ if (!confirm(browser.i18n.getMessage('OpenAIComp_Configs_ConfirmApply', config.name))) { return; } - document.getElementById('openai_comp_host').value = config.host || ''; + document.getElementById(getPrefixedId('openai_comp_host')).value = config.host || ''; // Clear all options from the select except the first (placeholder) one const openaiCompModelSelect = getModelEl('openai_comp_model', modelId_prefix); openaiCompModelSelect.value = ''; while (openaiCompModelSelect.options.length > 0) { openaiCompModelSelect.remove(0); } - document.getElementById('openai_comp_use_v1').checked = !!config.use_v1; - document.getElementById('openai_comp_chat_name').value = config.chat_name || ''; + document.getElementById(getPrefixedId('openai_comp_use_v1')).checked = !!config.use_v1; + document.getElementById(getPrefixedId('openai_comp_chat_name')).value = config.chat_name || ''; // Trigger change events if needed - document.getElementById('openai_comp_host').dispatchEvent(new Event('change', { bubbles: true })); + document.getElementById(getPrefixedId('openai_comp_host')).dispatchEvent(new Event('change', { bubbles: true })); getModelEl('openai_comp_model', modelId_prefix).dispatchEvent(new Event('change', { bubbles: true })); - document.getElementById('openai_comp_use_v1').dispatchEvent(new Event('change', { bubbles: true })); - document.getElementById('openai_comp_chat_name').dispatchEvent(new Event('change', { bubbles: true })); + document.getElementById(getPrefixedId('openai_comp_use_v1')).dispatchEvent(new Event('change', { bubbles: true })); + document.getElementById(getPrefixedId('openai_comp_chat_name')).dispatchEvent(new Event('change', { bubbles: true })); } }); @@ -662,7 +664,7 @@ export async function injectConnectionUI({ document.getElementById('btnUpdateChatGPTModels').addEventListener('click', async () => { document.getElementById('chatgpt_model_fetch_loading').style.display = 'inline'; let openai = new OpenAI({ - apiKey: document.getElementById("chatgpt_api_key").value, + apiKey: document.getElementById(getPrefixedId("chatgpt_api_key")).value, }); let granted = await messenger.permissions.request({ origins: ["https://*.openai.com/*"] }); if(!granted){ @@ -711,7 +713,7 @@ export async function injectConnectionUI({ document.getElementById('btnUpdateGoogleGeminiModels').addEventListener('click', async () => { document.getElementById('google_gemini_model_fetch_loading').style.display = 'inline'; let google_gemini = new GoogleGemini({ - apiKey: document.getElementById("google_gemini_api_key").value, + apiKey: document.getElementById(getPrefixedId("google_gemini_api_key")).value, }); google_gemini.fetchModels().then((data) => { if(!data.ok){ @@ -753,7 +755,7 @@ export async function injectConnectionUI({ document.getElementById('btnUpdateOllamaModels').addEventListener('click', async () => { document.getElementById('ollama_model_fetch_loading').style.display = 'inline'; let ollama = new Ollama({ - host: document.getElementById("ollama_host").value, + host: document.getElementById(getPrefixedId("ollama_host")).value, }); try { let data = await ollama.fetchModels(); @@ -812,9 +814,9 @@ export async function injectConnectionUI({ document.getElementById('btnUpdateOpenAICompModels').addEventListener('click', async () => { document.getElementById('openai_comp_model_fetch_loading').style.display = 'inline'; let openai_comp = new OpenAIComp({ - host: document.getElementById("openai_comp_host").value, - apiKey: document.getElementById("openai_comp_api_key").value, - use_v1: document.getElementById("openai_comp_use_v1").checked, + host: document.getElementById(getPrefixedId("openai_comp_host")).value, + apiKey: document.getElementById(getPrefixedId("openai_comp_api_key")).value, + use_v1: document.getElementById(getPrefixedId("openai_comp_use_v1")).checked, }); openai_comp.fetchModels().then((data) => { if(!data.ok){ @@ -857,8 +859,8 @@ export async function injectConnectionUI({ document.getElementById('btnUpdateAnthropicModels').addEventListener('click', async () => { document.getElementById('anthropic_model_fetch_loading').style.display = 'inline'; let anthropic = new Anthropic({ - apiKey: document.getElementById("anthropic_api_key").value, - version: document.getElementById("anthropic_version").value, + apiKey: document.getElementById(getPrefixedId("anthropic_api_key")).value, + version: document.getElementById(getPrefixedId("anthropic_version")).value, }); let granted = await messenger.permissions.request({ origins: ["https://*.anthropic.com/*"] }); if(!granted){ @@ -1164,7 +1166,8 @@ function populateConnectionTypeOptions(selectId, no_chatgpt_web = false) { } function warn_ChatGPT_APIKeyEmpty(modelId_prefix) { - let apiKeyInput = document.getElementById('chatgpt_api_key'); + const getPrefixedId = (id) => `${modelId_prefix ? `${modelId_prefix}` : ''}${id}`; + let apiKeyInput = document.getElementById(getPrefixedId('chatgpt_api_key')); let btnFetchChatGPTModels = document.getElementById('btnUpdateChatGPTModels'); let modelChatGPT = getModelEl('chatgpt_model', modelId_prefix); if(apiKeyInput.value === ''){ @@ -1186,7 +1189,8 @@ function warn_ChatGPT_APIKeyEmpty(modelId_prefix) { } function warn_GoogleGemini_APIKeyEmpty(modelId_prefix) { - let apiKeyInput = document.getElementById('google_gemini_api_key'); + const getPrefixedId = (id) => `${modelId_prefix ? `${modelId_prefix}` : ''}${id}`; + let apiKeyInput = document.getElementById(getPrefixedId('google_gemini_api_key')); let btnFetchGoogleGeminiModels = document.getElementById('btnUpdateGoogleGeminiModels'); let modelGoogleGemini = getModelEl('google_gemini_model', modelId_prefix); if(apiKeyInput.value === ''){ @@ -1208,7 +1212,8 @@ function warn_GoogleGemini_APIKeyEmpty(modelId_prefix) { } function warn_Ollama_HostEmpty(modelId_prefix) { - let hostInput = document.getElementById('ollama_host'); + const getPrefixedId = (id) => `${modelId_prefix ? `${modelId_prefix}` : ''}${id}`; + let hostInput = document.getElementById(getPrefixedId('ollama_host')); let btnFetchOllamaModels = document.getElementById('btnUpdateOllamaModels'); let modelOllama = getModelEl('ollama_model', modelId_prefix); if(hostInput.value === ''){ @@ -1230,7 +1235,8 @@ function warn_Ollama_HostEmpty(modelId_prefix) { } function warn_OpenAIComp_HostEmpty(modelId_prefix) { - let hostInput = document.getElementById('openai_comp_host'); + const getPrefixedId = (id) => `${modelId_prefix ? `${modelId_prefix}` : ''}${id}`; + let hostInput = document.getElementById(getPrefixedId('openai_comp_host')); let btnUpdateOpenAICompModels = document.getElementById('btnUpdateOpenAICompModels'); let modelOpenAIComp = getModelEl('openai_comp_model', modelId_prefix); if(hostInput.value === ''){ @@ -1252,7 +1258,8 @@ function warn_OpenAIComp_HostEmpty(modelId_prefix) { } function warn_Anthropic_APIKeyEmpty(modelId_prefix) { - let apiKeyInput = document.getElementById('anthropic_api_key'); + const getPrefixedId = (id) => `${modelId_prefix ? `${modelId_prefix}` : ''}${id}`; + let apiKeyInput = document.getElementById(getPrefixedId('anthropic_api_key')); let btnFetchAnthropicModels = document.getElementById('btnUpdateAnthropicModels'); let modelAnthropic = getModelEl('anthropic_model', modelId_prefix); if(apiKeyInput.value === ''){ @@ -1274,7 +1281,8 @@ function warn_Anthropic_APIKeyEmpty(modelId_prefix) { } function warn_Anthropic_VersionEmpty(modelId_prefix) { - let versionInput = document.getElementById('anthropic_version'); + const getPrefixedId = (id) => `${modelId_prefix ? `${modelId_prefix}` : ''}${id}`; + let versionInput = document.getElementById(getPrefixedId('anthropic_version')); let btnFetchAnthropicModels = document.getElementById('btnUpdateAnthropicModels'); let modelAnthropic = getModelEl('anthropic_model', modelId_prefix); if(versionInput.value === ''){