mirror of
https://github.com/vibegif/vibegif.lol.git
synced 2026-04-07 02:12:12 +00:00
Fix: Handle localStorage crashes and simplify init
This commit is contained in:
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user