Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -443,73 +443,43 @@ def main():
|
|
443 |
|
444 |
gr.HTML("""
|
445 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
446 |
-
<
|
447 |
-
|
448 |
-
|
|
|
449 |
""")
|
450 |
|
|
|
451 |
gr.HTML("""
|
452 |
<script>
|
453 |
-
//
|
454 |
-
|
455 |
-
|
456 |
-
|
457 |
-
|
458 |
-
|
459 |
-
|
460 |
-
const swResponse = await fetch('service-worker.js');
|
461 |
-
console.log('Service Worker 回應:', {
|
462 |
-
status: swResponse.status,
|
463 |
-
type: swResponse.headers.get('content-type')
|
464 |
-
});
|
465 |
|
466 |
-
|
467 |
-
|
468 |
-
|
469 |
-
|
470 |
-
|
471 |
-
|
472 |
-
|
473 |
-
|
474 |
-
|
475 |
-
|
476 |
-
|
477 |
-
|
478 |
-
|
479 |
-
|
480 |
-
|
481 |
-
|
482 |
-
|
483 |
-
// 確保在正確的環境中執行
|
484 |
-
if (window.self === window.top) { // 不在 iframe 中
|
485 |
-
// 等待 Gradio 完全初始化
|
486 |
-
const initPWA = () => {
|
487 |
-
if ('serviceWorker' in navigator) {
|
488 |
-
navigator.serviceWorker.register('service-worker.js', {
|
489 |
-
scope: './'
|
490 |
-
}).then(registration => {
|
491 |
-
console.log('PWA 註冊成功:', registration.scope);
|
492 |
-
}).catch(error => {
|
493 |
-
console.error('PWA 註冊失敗:', error);
|
494 |
-
});
|
495 |
-
}
|
496 |
-
};
|
497 |
-
|
498 |
-
// 使用 MutationObserver 確保在 Gradio 界面完全載入後才初始化 PWA
|
499 |
-
const observer = new MutationObserver((mutations, obs) => {
|
500 |
-
const gradioApp = document.querySelector('.gradio-app');
|
501 |
-
if (gradioApp) {
|
502 |
-
console.log('Gradio 界面已載入');
|
503 |
-
initPWA();
|
504 |
-
obs.disconnect();
|
505 |
}
|
506 |
});
|
507 |
-
|
508 |
-
observer.observe(document, {
|
509 |
-
childList: true,
|
510 |
-
subtree: true
|
511 |
-
});
|
512 |
-
}
|
513 |
</script>
|
514 |
""")
|
515 |
|
|
|
443 |
|
444 |
gr.HTML("""
|
445 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
446 |
+
<meta name="apple-mobile-web-app-capable" content="yes">
|
447 |
+
<meta name="apple-mobile-web-app-status-bar-style" content="black">
|
448 |
+
<link rel="manifest" href="manifest.json">
|
449 |
+
<link rel="apple-touch-icon" href="assets/icon-192.png">
|
450 |
""")
|
451 |
|
452 |
+
# 第二步:添加一個簡單的初始化檢查
|
453 |
gr.HTML("""
|
454 |
<script>
|
455 |
+
// 使用立即執行函數來避免全局變量污染
|
456 |
+
(function() {
|
457 |
+
// 當文檔載入完成時執行
|
458 |
+
document.addEventListener('DOMContentLoaded', function() {
|
459 |
+
// 記錄環境信息
|
460 |
+
console.log('頁面已載入');
|
461 |
+
console.log('URL:', window.location.href);
|
|
|
|
|
|
|
|
|
|
|
462 |
|
463 |
+
// 檢查是否在正確的環境中
|
464 |
+
if (window.self === window.top) {
|
465 |
+
console.log('在主視窗中執行');
|
466 |
+
|
467 |
+
// 嘗試註冊 Service Worker
|
468 |
+
if ('serviceWorker' in navigator) {
|
469 |
+
// 使用延遲註冊以確保不會干擾頁面載入
|
470 |
+
setTimeout(function() {
|
471 |
+
navigator.serviceWorker.register('service-worker.js')
|
472 |
+
.then(function(registration) {
|
473 |
+
console.log('Service Worker 註冊成功');
|
474 |
+
})
|
475 |
+
.catch(function(error) {
|
476 |
+
console.error('Service Worker 註冊失敗:', error);
|
477 |
+
});
|
478 |
+
}, 1000);
|
479 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
480 |
}
|
481 |
});
|
482 |
+
})();
|
|
|
|
|
|
|
|
|
|
|
483 |
</script>
|
484 |
""")
|
485 |
|