fix: 修复 localStorage 保存后 alert 仍显示的问题,统一用 DOMContentLoaded 包装初始化
This commit is contained in:
51
ui.js
51
ui.js
@@ -179,12 +179,14 @@ async function copySrc(src, fmt, btn) {
|
|||||||
|
|
||||||
// ── Init ────────────────────────────────────────────────────────
|
// ── Init ────────────────────────────────────────────────────────
|
||||||
|
|
||||||
async function init() {
|
function init() {
|
||||||
const savedKey = localStorage.getItem('imgGen-apiKey') || '';
|
const savedKey = (localStorage.getItem('imgGen-apiKey') || '').trim();
|
||||||
const savedBaseUrl = localStorage.getItem('imgGen-baseUrl') || 'https://api.minimaxi.com';
|
const savedBaseUrl = localStorage.getItem('imgGen-baseUrl') || 'https://api.minimaxi.com';
|
||||||
if (!savedKey) apiKeyAlert.style.display = 'flex';
|
if (!savedKey && apiKeyAlert) {
|
||||||
apiKeyInput.value = savedKey;
|
apiKeyAlert.style.display = 'flex';
|
||||||
baseUrlInput.value = savedBaseUrl;
|
}
|
||||||
|
if (apiKeyInput) apiKeyInput.value = savedKey;
|
||||||
|
if (baseUrlInput) baseUrlInput.value = savedBaseUrl;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ── Generate ────────────────────────────────────────────────────
|
// ── 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 => {
|
document.querySelectorAll('.tag-pill').forEach(btn => {
|
||||||
btn.addEventListener('click', () => {
|
btn.addEventListener('click', () => {
|
||||||
const tag = btn.dataset.tag;
|
const tag = btn.dataset.tag;
|
||||||
const current = promptInput.value.trim();
|
const current = (promptInput.value || '').trim();
|
||||||
if (current) {
|
promptInput.value = current ? current + ',' + tag : tag;
|
||||||
promptInput.value = current + ',' + tag;
|
|
||||||
} else {
|
|
||||||
promptInput.value = tag;
|
|
||||||
}
|
|
||||||
promptInput.dispatchEvent(new Event('input'));
|
promptInput.dispatchEvent(new Event('input'));
|
||||||
promptInput.focus();
|
promptInput.focus();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// ── Prompt char count ──────────────────────────────────────────
|
// Prompt char count
|
||||||
|
|
||||||
promptInput.addEventListener('input', () => {
|
promptInput.addEventListener('input', () => {
|
||||||
charCount.textContent = `${promptInput.value.length} / 1500`;
|
charCount.textContent = `${promptInput.value.length} / 1500`;
|
||||||
});
|
});
|
||||||
|
|
||||||
// ── Settings modal ──────────────────────────────────────────────
|
// Settings modal
|
||||||
|
|
||||||
function openModal() {
|
function openModal() {
|
||||||
settingsModal.classList.add('open');
|
settingsModal.classList.add('open');
|
||||||
apiKeyInput.focus();
|
apiKeyInput.focus();
|
||||||
@@ -266,20 +268,19 @@ toggleKey.addEventListener('click', () => {
|
|||||||
apiKeyInput.type = apiKeyInput.type === 'password' ? 'text' : 'password';
|
apiKeyInput.type = apiKeyInput.type === 'password' ? 'text' : 'password';
|
||||||
});
|
});
|
||||||
|
|
||||||
saveSettingsBtn.addEventListener('click', async () => {
|
saveSettingsBtn.addEventListener('click', () => {
|
||||||
clearError();
|
clearError();
|
||||||
const apiKey = apiKeyInput.value.trim();
|
const apiKey = (apiKeyInput.value || '').trim();
|
||||||
const baseUrl = baseUrlInput.value.trim() || 'https://api.minimaxi.com';
|
const baseUrl = (baseUrlInput.value || '').trim() || 'https://api.minimaxi.com';
|
||||||
|
|
||||||
localStorage.setItem('imgGen-apiKey', apiKey);
|
localStorage.setItem('imgGen-apiKey', apiKey);
|
||||||
localStorage.setItem('imgGen-baseUrl', baseUrl);
|
localStorage.setItem('imgGen-baseUrl', baseUrl);
|
||||||
|
|
||||||
if (apiKey) apiKeyAlert.style.display = 'none';
|
if (apiKey && apiKeyAlert) apiKeyAlert.style.display = 'none';
|
||||||
closeModal();
|
closeModal();
|
||||||
});
|
});
|
||||||
|
|
||||||
// ── Theme switcher ─────────────────────────────────────────────
|
// Theme switcher
|
||||||
|
|
||||||
function applyTheme(theme) {
|
function applyTheme(theme) {
|
||||||
document.documentElement.dataset.theme = theme;
|
document.documentElement.dataset.theme = theme;
|
||||||
localStorage.setItem('imgGen-theme', theme);
|
localStorage.setItem('imgGen-theme', theme);
|
||||||
@@ -292,10 +293,6 @@ document.querySelectorAll('.theme-btn').forEach(btn => {
|
|||||||
btn.addEventListener('click', () => applyTheme(btn.dataset.theme));
|
btn.addEventListener('click', () => applyTheme(btn.dataset.theme));
|
||||||
});
|
});
|
||||||
|
|
||||||
// Restore saved theme
|
|
||||||
const savedTheme = localStorage.getItem('imgGen-theme');
|
const savedTheme = localStorage.getItem('imgGen-theme');
|
||||||
if (savedTheme) applyTheme(savedTheme);
|
if (savedTheme) applyTheme(savedTheme);
|
||||||
|
});
|
||||||
// ── Start ──────────────────────────────────────────────────────
|
|
||||||
|
|
||||||
init();
|
|
||||||
|
|||||||
Reference in New Issue
Block a user