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";
// 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 = {
apiKey: localStorage.getItem("openrouter_api_key") || "",
apiKey: savedApiKey,
frames: [],
gifUrl: "",
loading: false
@@ -118,14 +126,22 @@ function closeSettings() {
function saveApiKey() {
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();
}
function clearApiKey() {
state.apiKey = "";
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() {
@@ -252,8 +268,5 @@ function init() {
bind();
}
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", init);
} else {
init();
}
// Since module scripts are natively deferred, the DOM is guaranteed to be fully parsed.
init();