Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -443,38 +443,73 @@ def main():
|
|
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 |
-
<meta name="apple-mobile-web-app-title" content="PawMatch AI">
|
449 |
-
<meta name="theme-color" content="#4299e1">
|
450 |
-
|
451 |
-
<!-- PWA 必要檔案的連結,使用相對路徑 -->
|
452 |
-
<link rel="manifest" href="manifest.json">
|
453 |
-
<link rel="apple-touch-icon" href="assets/icon-192.png">
|
454 |
-
|
455 |
-
<!-- Service Worker 註冊程序 -->
|
456 |
<script>
|
457 |
-
|
458 |
-
|
459 |
-
|
460 |
-
|
461 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
462 |
if ('serviceWorker' in navigator) {
|
463 |
-
|
464 |
-
|
465 |
-
.then(
|
466 |
-
console.log('
|
467 |
-
})
|
468 |
-
|
469 |
-
console.log('Service Worker 註冊失敗:', error);
|
470 |
});
|
471 |
-
} else {
|
472 |
-
console.log('此瀏覽器不支援 Service Worker');
|
473 |
}
|
474 |
-
}
|
475 |
-
|
476 |
-
|
477 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
478 |
</script>
|
479 |
""")
|
480 |
|
|
|
443 |
|
444 |
gr.HTML("""
|
445 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
446 |
<script>
|
447 |
+
console.log('基本初始化測試');
|
448 |
+
</script>
|
449 |
+
""")
|
450 |
+
|
451 |
+
gr.HTML("""
|
452 |
+
<script>
|
453 |
+
// 檢查環境
|
454 |
+
console.log('當前 URL:', window.location.href);
|
455 |
+
console.log('是否在 iframe 中:', window !== window.top);
|
456 |
+
|
457 |
+
// 檢查檔案存取
|
458 |
+
async function checkFiles() {
|
459 |
+
try {
|
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 |
+
const manifestResponse = await fetch('manifest.json');
|
467 |
+
console.log('Manifest 回應:', {
|
468 |
+
status: manifestResponse.status,
|
469 |
+
type: manifestResponse.headers.get('content-type')
|
470 |
+
});
|
471 |
+
} catch (error) {
|
472 |
+
console.error('檔案檢查錯誤:', error);
|
473 |
+
}
|
474 |
+
}
|
475 |
+
|
476 |
+
// 在頁面載入後執行檢查
|
477 |
+
window.addEventListener('load', checkFiles);
|
478 |
+
</script>
|
479 |
+
""")
|
480 |
+
|
481 |
+
gr.HTML("""
|
482 |
+
<script>
|
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 |
|