mirror of
https://github.com/vibegif/vibegif.lol.git
synced 2026-04-07 10:12:13 +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";
|
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();
|
||||||
|
try {
|
||||||
localStorage.setItem("openrouter_api_key", state.apiKey);
|
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 = "";
|
||||||
|
try {
|
||||||
localStorage.removeItem("openrouter_api_key");
|
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();
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user