fix: 修复 localStorage 保存后 alert 仍显示的问题,统一用 DOMContentLoaded 包装初始化

This commit is contained in:
zwbcc
2026-03-28 21:32:31 +08:00
parent 535573f95a
commit c3726f0d4d

51
ui.js
View File

@@ -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,30 +224,30 @@ generateForm.addEventListener('submit', async (e) => {
}
});
// ── Quick prompt tags ──────────────────────────────────────────
// ── Start ──────────────────────────────────────────────────────
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();
if (current) {
promptInput.value = current + '' + tag;
} else {
promptInput.value = tag;
}
const current = (promptInput.value || '').trim();
promptInput.value = current ? current + '' + tag : tag;
promptInput.dispatchEvent(new Event('input'));
promptInput.focus();
});
});
// ── Prompt char count ──────────────────────────────────────────
// Prompt char count
promptInput.addEventListener('input', () => {
charCount.textContent = `${promptInput.value.length} / 1500`;
});
// ── Settings modal ──────────────────────────────────────────────
// Settings modal
function openModal() {
settingsModal.classList.add('open');
apiKeyInput.focus();
@@ -266,20 +268,19 @@ toggleKey.addEventListener('click', () => {
apiKeyInput.type = apiKeyInput.type === 'password' ? 'text' : 'password';
});
saveSettingsBtn.addEventListener('click', async () => {
saveSettingsBtn.addEventListener('click', () => {
clearError();
const apiKey = apiKeyInput.value.trim();
const baseUrl = baseUrlInput.value.trim() || 'https://api.minimaxi.com';
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';
if (apiKey && apiKeyAlert) apiKeyAlert.style.display = 'none';
closeModal();
});
// ── Theme switcher ─────────────────────────────────────────────
// Theme switcher
function applyTheme(theme) {
document.documentElement.dataset.theme = theme;
localStorage.setItem('imgGen-theme', theme);
@@ -292,10 +293,6 @@ 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();
});