DawnC commited on
Commit
40363f2
·
1 Parent(s): 7e1d4db

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -60
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
- <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
 
 
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