// /utils/loadWindowVariables.ts // ✅ Interface für `window`-Objekt zur TypeScript-Sicherheit interface CustomWindow extends Window { [key: string]: any; } // ✅ Hauptfunktion zum Laden von `window`-Variablen export async function loadWindowVariables(): Promise> { return new Promise((resolve, reject) => { const requiredVars: string[] = [ "win_kueID", // z. B. für die Anzeige der Modulnamen "win_deviceName", // z. B. für die Kopfzeile/Übersicht "win_de_state", "win_de_label", ]; const scripts: string[] = ["system.js"]; // ✅ Erkenne Umgebung anhand von `window.location.hostname` const isDev = window.location.hostname === "localhost"; const loadScript = (src: string): Promise => { return new Promise((resolve, reject) => { const script = document.createElement("script"); script.src = isDev ? `/CPLmockData/SERVICE/${src}` // Entwicklungsumgebung : `/CPL?/CPL/SERVICE/${src}`; // Produktionsumgebung script.async = true; script.onload = () => resolve(); script.onerror = () => reject(new Error(`Script load error: ${src}`)); document.head.appendChild(script); }); }; // ✅ Lade alle Skripte nacheinander scripts .reduce( (promise, script) => promise.then(() => loadScript(script)), Promise.resolve() ) .then(() => { const win = window as unknown as CustomWindow; // ✅ Erstelle ein Objekt mit allen geladenen Variablen const variablesObj: Record = requiredVars.reduce( (acc, variable) => { if (win[variable] !== undefined) { acc[variable.replace("win_", "")] = win[variable]; } return acc; }, {} ); resolve(variablesObj); }) .catch((error) => { console.error("❌ Fehler beim Laden eines Skripts:", error); reject(error); }); }); }