Fix: Handle localStorage crashes and simplify init

This commit is contained in:
2026-03-20 21:39:16 -07:00
parent 41685017a8
commit ac283d522e

View File

@@ -4,8 +4,16 @@ import { firstPrompt, nextFramePrompt } from "./ui.js";
const GEMINI_MODEL = "google/gemini-3.1-flash-image-preview"; const GEMINI_MODEL = "google/gemini-3.1-flash-image-preview";
// Wrap in try-catch to prevent fatal crashes in strict iframes/sandboxes
let savedApiKey = "";
try {
savedApiKey = localStorage.getItem("openrouter_api_key") || "";
} catch (err) {
console.warn("localStorage is blocked in this environment.");
}
const state = { const state = {
apiKey: localStorage.getItem("openrouter_api_key") || "", apiKey: savedApiKey,
frames: [], frames: [],
gifUrl: "", gifUrl: "",
loading: false loading: false
@@ -118,14 +126,22 @@ function closeSettings() {
function saveApiKey() { function saveApiKey() {
state.apiKey = el.apiKeyInput.value.trim(); state.apiKey = el.apiKeyInput.value.trim();
localStorage.setItem("openrouter_api_key", state.apiKey); try {
localStorage.setItem("openrouter_api_key", state.apiKey);
} catch (err) {
console.warn("Could not save to localStorage.");
}
closeSettings(); closeSettings();
} }
function clearApiKey() { function clearApiKey() {
state.apiKey = ""; state.apiKey = "";
el.apiKeyInput.value = ""; el.apiKeyInput.value = "";
localStorage.removeItem("openrouter_api_key"); try {
localStorage.removeItem("openrouter_api_key");
} catch (err) {
console.warn("Could not modify localStorage.");
}
} }
async function generate() { async function generate() {
@@ -252,8 +268,5 @@ function init() {
bind(); bind();
} }
if (document.readyState === "loading") { // Since module scripts are natively deferred, the DOM is guaranteed to be fully parsed.
document.addEventListener("DOMContentLoaded", init); init();
} else {
init();
}