From c3726f0d4d4cfa9e746024434baf1733c10d34e9 Mon Sep 17 00:00:00 2001 From: zwbcc Date: Sat, 28 Mar 2026 21:32:31 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20localStorage=20?= =?UTF-8?q?=E4=BF=9D=E5=AD=98=E5=90=8E=20alert=20=E4=BB=8D=E6=98=BE?= =?UTF-8?q?=E7=A4=BA=E7=9A=84=E9=97=AE=E9=A2=98=EF=BC=8C=E7=BB=9F=E4=B8=80?= =?UTF-8?q?=E7=94=A8=20DOMContentLoaded=20=E5=8C=85=E8=A3=85=E5=88=9D?= =?UTF-8?q?=E5=A7=8B=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ui.js | 153 ++++++++++++++++++++++++++++------------------------------ 1 file changed, 75 insertions(+), 78 deletions(-) diff --git a/ui.js b/ui.js index 4e8ed3d..5c38762 100644 --- a/ui.js +++ b/ui.js @@ -179,12 +179,14 @@ async function copySrc(src, fmt, btn) { // ── Init ──────────────────────────────────────────────────────── -async function init() { - const savedKey = localStorage.getItem('imgGen-apiKey') || ''; +function init() { + const savedKey = (localStorage.getItem('imgGen-apiKey') || '').trim(); const savedBaseUrl = localStorage.getItem('imgGen-baseUrl') || 'https://api.minimaxi.com'; - if (!savedKey) apiKeyAlert.style.display = 'flex'; - apiKeyInput.value = savedKey; - baseUrlInput.value = savedBaseUrl; + if (!savedKey && apiKeyAlert) { + apiKeyAlert.style.display = 'flex'; + } + if (apiKeyInput) apiKeyInput.value = savedKey; + if (baseUrlInput) baseUrlInput.value = savedBaseUrl; } // ── Generate ──────────────────────────────────────────────────── @@ -222,80 +224,75 @@ generateForm.addEventListener('submit', async (e) => { } }); -// ── Quick prompt tags ────────────────────────────────────────── -document.querySelectorAll('.tag-pill').forEach(btn => { - btn.addEventListener('click', () => { - const tag = btn.dataset.tag; - const current = promptInput.value.trim(); - if (current) { - promptInput.value = current + ',' + tag; - } else { - promptInput.value = tag; - } - promptInput.dispatchEvent(new Event('input')); - promptInput.focus(); - }); -}); - -// ── Prompt char count ────────────────────────────────────────── - -promptInput.addEventListener('input', () => { - charCount.textContent = `${promptInput.value.length} / 1500`; -}); - -// ── Settings modal ────────────────────────────────────────────── - -function openModal() { - settingsModal.classList.add('open'); - apiKeyInput.focus(); -} - -function closeModal() { - settingsModal.classList.remove('open'); - clearError(); -} - -openSettings.addEventListener('click', openModal); -alertSettings.addEventListener('click', openModal); -closeSettings.addEventListener('click', closeModal); -cancelSettings.addEventListener('click', closeModal); -settingsModal.addEventListener('click', e => { if (e.target === settingsModal) closeModal(); }); - -toggleKey.addEventListener('click', () => { - apiKeyInput.type = apiKeyInput.type === 'password' ? 'text' : 'password'; -}); - -saveSettingsBtn.addEventListener('click', async () => { - clearError(); - const apiKey = apiKeyInput.value.trim(); - const baseUrl = baseUrlInput.value.trim() || 'https://api.minimaxi.com'; - - localStorage.setItem('imgGen-apiKey', apiKey); - localStorage.setItem('imgGen-baseUrl', baseUrl); - - if (apiKey) apiKeyAlert.style.display = 'none'; - closeModal(); -}); - -// ── Theme switcher ───────────────────────────────────────────── - -function applyTheme(theme) { - document.documentElement.dataset.theme = theme; - localStorage.setItem('imgGen-theme', theme); - document.querySelectorAll('.theme-btn').forEach(btn => - btn.classList.toggle('active', btn.dataset.theme === theme) - ); -} - -document.querySelectorAll('.theme-btn').forEach(btn => { - btn.addEventListener('click', () => applyTheme(btn.dataset.theme)); -}); - -// Restore saved theme -const savedTheme = localStorage.getItem('imgGen-theme'); -if (savedTheme) applyTheme(savedTheme); // ── Start ────────────────────────────────────────────────────── -init(); +document.addEventListener('DOMContentLoaded', () => { + init(); + + // Quick prompt tags + document.querySelectorAll('.tag-pill').forEach(btn => { + btn.addEventListener('click', () => { + const tag = btn.dataset.tag; + const current = (promptInput.value || '').trim(); + promptInput.value = current ? current + ',' + tag : tag; + promptInput.dispatchEvent(new Event('input')); + promptInput.focus(); + }); + }); + + // Prompt char count + promptInput.addEventListener('input', () => { + charCount.textContent = `${promptInput.value.length} / 1500`; + }); + + // Settings modal + function openModal() { + settingsModal.classList.add('open'); + apiKeyInput.focus(); + } + + function closeModal() { + settingsModal.classList.remove('open'); + clearError(); + } + + openSettings.addEventListener('click', openModal); + alertSettings.addEventListener('click', openModal); + closeSettings.addEventListener('click', closeModal); + cancelSettings.addEventListener('click', closeModal); + settingsModal.addEventListener('click', e => { if (e.target === settingsModal) closeModal(); }); + + toggleKey.addEventListener('click', () => { + apiKeyInput.type = apiKeyInput.type === 'password' ? 'text' : 'password'; + }); + + saveSettingsBtn.addEventListener('click', () => { + clearError(); + const apiKey = (apiKeyInput.value || '').trim(); + const baseUrl = (baseUrlInput.value || '').trim() || 'https://api.minimaxi.com'; + + localStorage.setItem('imgGen-apiKey', apiKey); + localStorage.setItem('imgGen-baseUrl', baseUrl); + + if (apiKey && apiKeyAlert) apiKeyAlert.style.display = 'none'; + closeModal(); + }); + + // Theme switcher + function applyTheme(theme) { + document.documentElement.dataset.theme = theme; + localStorage.setItem('imgGen-theme', theme); + document.querySelectorAll('.theme-btn').forEach(btn => + btn.classList.toggle('active', btn.dataset.theme === theme) + ); + } + + document.querySelectorAll('.theme-btn').forEach(btn => { + btn.addEventListener('click', () => applyTheme(btn.dataset.theme)); + }); + + const savedTheme = localStorage.getItem('imgGen-theme'); + if (savedTheme) applyTheme(savedTheme); +});