DawnC commited on
Commit
4b17b6f
·
verified ·
1 Parent(s): 6807bb0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -43
app.py CHANGED
@@ -437,60 +437,72 @@ def show_details_html(choice, previous_output, initial_state):
437
  print(error_msg)
438
  return format_hint_html(error_msg), gr.update(visible=True), initial_state
439
 
440
-
441
  def get_pwa_html():
442
- # 先定義基本的 manifest 內容,使用最簡單的字串連接方式
443
- manifest = (
444
- '{'
445
- '"name": "PawMatch AI",'
446
- '"short_name": "PawMatch",'
447
- '"start_url": "/index.html",'
448
- '"display": "standalone",'
449
- '"background_color": "#ffffff",'
450
- '"theme_color": "#4299e1",'
451
- '"icons": [{'
452
- ' "src": "/icon-192.png",'
453
- ' "sizes": "192x192",'
454
- ' "type": "image/png"'
455
- '}]'
456
- '}'
457
- )
458
-
459
- # 構建 HTML,避免使用複雜的格式化
460
- html = '''
461
  <!DOCTYPE html>
462
  <meta charset="UTF-8">
463
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
464
  <meta name="apple-mobile-web-app-capable" content="yes">
465
- <meta name="theme-color" content="#4299e1">
466
- '''
467
 
468
- # 添加 manifest 相關代碼
469
- html += f'''
470
  <script>
471
- window.addEventListener('load', function() {{
472
- var manifest = {manifest};
473
- var blob = new Blob([JSON.stringify(manifest)], {{type: 'application/json'}});
474
- var link = document.createElement('link');
475
- link.rel = 'manifest';
476
- link.href = URL.createObjectURL(blob);
477
- document.head.appendChild(link);
478
- }});
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
479
  </script>
480
- '''
481
 
482
- # 添加基本樣式
483
- html += '''
484
  <style>
485
- body {
486
- margin: 0;
487
- padding: 0;
488
- -webkit-overflow-scrolling: touch;
489
- }
 
 
 
 
 
490
  </style>
491
- '''
492
-
493
- return html
494
 
495
  def main():
496
  with gr.Blocks(css=get_css_styles()) as iface:
 
437
  print(error_msg)
438
  return format_hint_html(error_msg), gr.update(visible=True), initial_state
439
 
 
440
  def get_pwa_html():
441
+ return """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
442
  <!DOCTYPE html>
443
  <meta charset="UTF-8">
444
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
445
  <meta name="apple-mobile-web-app-capable" content="yes">
 
 
446
 
 
 
447
  <script>
448
+ // 檢測環境並根據支援程度提供功能
449
+ function checkEnvironment() {
450
+ if ('serviceWorker' in navigator) {
451
+ // PWA 支援環境
452
+ initPWA();
453
+ } else {
454
+ // 基礎環境
455
+ initBasicFeatures();
456
+ }
457
+ }
458
+
459
+ function initBasicFeatures() {
460
+ // 基本的移動端優化
461
+ document.documentElement.style.webkitTouchCallout = 'none';
462
+ document.documentElement.style.webkitUserSelect = 'none';
463
+ }
464
+
465
+ function initPWA() {
466
+ // 只在支援的環境中嘗試 PWA 功能
467
+ try {
468
+ const manifestData = {
469
+ name: 'PawMatch AI',
470
+ short_name: 'PawMatch',
471
+ display: 'standalone'
472
+ };
473
+
474
+ const blob = new Blob(
475
+ [JSON.stringify(manifestData)],
476
+ {type: 'application/json'}
477
+ );
478
+
479
+ const link = document.createElement('link');
480
+ link.rel = 'manifest';
481
+ link.href = URL.createObjectURL(blob);
482
+ document.head.appendChild(link);
483
+ } catch(e) {
484
+ console.log('PWA features not available');
485
+ initBasicFeatures();
486
+ }
487
+ }
488
+
489
+ window.addEventListener('load', checkEnvironment);
490
  </script>
 
491
 
 
 
492
  <style>
493
+ /* 基本的移動端優化樣式 */
494
+ body {
495
+ -webkit-overflow-scrolling: touch;
496
+ overflow-x: hidden;
497
+ }
498
+
499
+ /* 防止 iOS 上的一些預設行為 */
500
+ * {
501
+ -webkit-tap-highlight-color: transparent;
502
+ }
503
  </style>
504
+ """
505
+
 
506
 
507
  def main():
508
  with gr.Blocks(css=get_css_styles()) as iface: