E5note commited on
Commit
15eb296
·
verified ·
1 Parent(s): c072f04

Fix embedded demo status without cross-origin fetch

Browse files
Files changed (1) hide show
  1. index.html +18 -19
index.html CHANGED
@@ -367,32 +367,31 @@
367
  </main>
368
 
369
  <script>
370
- const upstreamInfo =
371
- "https://24yearsold-see-through-demo.hf.space/gradio_api/info";
372
  const dot = document.getElementById("status-dot");
373
  const label = document.getElementById("status-label");
374
  const frame = document.getElementById("demo-frame");
375
  const fallback = document.getElementById("fallback");
376
-
377
- async function checkUpstream() {
378
- try {
379
- const response = await fetch(upstreamInfo, {
380
- method: "GET",
381
- mode: "cors",
382
- signal: AbortSignal.timeout(12000),
383
- });
384
- if (!response.ok) throw new Error(`HTTP ${response.status}`);
385
- dot.classList.add("online");
386
- label.textContent = "上游 Demo 在线";
387
- } catch (error) {
 
 
 
 
388
  dot.classList.add("offline");
389
- label.textContent = "上游正在休眠或暂不可用";
390
  fallback.classList.add("visible");
391
  }
392
- }
393
-
394
- frame.addEventListener("error", () => fallback.classList.add("visible"));
395
- checkUpstream();
396
  </script>
397
  </body>
398
  </html>
 
367
  </main>
368
 
369
  <script>
 
 
370
  const dot = document.getElementById("status-dot");
371
  const label = document.getElementById("status-label");
372
  const frame = document.getElementById("demo-frame");
373
  const fallback = document.getElementById("fallback");
374
+ let frameLoaded = false;
375
+
376
+ frame.addEventListener("load", () => {
377
+ frameLoaded = true;
378
+ dot.classList.add("online");
379
+ label.textContent = "上游 Demo 已加载";
380
+ });
381
+
382
+ frame.addEventListener("error", () => {
383
+ dot.classList.add("offline");
384
+ label.textContent = "上游暂不可用";
385
+ fallback.classList.add("visible");
386
+ });
387
+
388
+ window.setTimeout(() => {
389
+ if (!frameLoaded) {
390
  dot.classList.add("offline");
391
+ label.textContent = "上游加载超时";
392
  fallback.classList.add("visible");
393
  }
394
+ }, 20000);
 
 
 
395
  </script>
396
  </body>
397
  </html>