|
9 | 9 | }); |
10 | 10 | }); |
11 | 11 |
|
12 | | - const banner = document.getElementById('install-banner'); |
13 | 12 | const installBtn = document.getElementById('install-btn'); |
| 13 | + const iosInfo = document.getElementById('ios-info'); |
14 | 14 |
|
15 | 15 | const isStandalone = () => { |
16 | 16 | // iOS |
|
19 | 19 | return !!(window.matchMedia && window.matchMedia('(display-mode: standalone)').matches); |
20 | 20 | }; |
21 | 21 |
|
22 | | - function showBanner() { |
23 | | - if (!banner) return; |
24 | | - banner.hidden = false; |
25 | | - } |
26 | | - |
27 | | - function hideBanner() { |
28 | | - if (!banner) return; |
29 | | - banner.hidden = true; |
30 | | - } |
31 | | - |
32 | | - // اگر اپ نصب شده و در حالت standalone اجرا شده، بنر نصب را نشان نده |
33 | | - if (isStandalone()) { |
34 | | - hideBanner(); |
| 22 | + // اگر اپ نصب شده است، ریدایرکت به ایندکس |
| 23 | + if (isStandalone() && window.location.pathname.includes('install.html')) { |
| 24 | + window.location.href = 'index.html'; |
35 | 25 | return; |
36 | 26 | } |
37 | 27 |
|
38 | 28 | window.addEventListener('beforeinstallprompt', (e) => { |
39 | 29 | e.preventDefault(); |
40 | 30 | deferredPrompt = e; |
41 | | - showBanner(); |
| 31 | + if (installBtn) installBtn.style.display = 'block'; |
42 | 32 | }); |
43 | 33 |
|
44 | 34 | window.addEventListener('appinstalled', () => { |
45 | 35 | deferredPrompt = null; |
46 | | - hideBanner(); |
| 36 | + window.location.href = 'index.html'; |
47 | 37 | }); |
48 | 38 |
|
49 | 39 | if (installBtn) { |
50 | 40 | installBtn.addEventListener('click', async () => { |
51 | 41 | if (!deferredPrompt) return; |
52 | 42 | deferredPrompt.prompt(); |
53 | | - try { |
54 | | - await deferredPrompt.userChoice; |
55 | | - } finally { |
| 43 | + const { outcome } = await deferredPrompt.userChoice; |
| 44 | + if (outcome === 'accepted') { |
56 | 45 | deferredPrompt = null; |
57 | | - hideBanner(); |
58 | 46 | } |
59 | 47 | }); |
60 | 48 | } |
61 | 49 |
|
62 | | - // iOS Safari: beforeinstallprompt وجود ندارد |
| 50 | + // iOS Safari |
63 | 51 | const isIos = /iphone|ipad|ipod/i.test(navigator.userAgent); |
64 | | - if (isIos && !isStandalone() && banner && installBtn) { |
65 | | - banner.querySelector('.install-banner__text').textContent = 'برای نصب: Share → Add to Home Screen'; |
66 | | - installBtn.hidden = true; |
67 | | - showBanner(); |
| 52 | + if (isIos && !isStandalone() && iosInfo) { |
| 53 | + iosInfo.style.display = 'block'; |
| 54 | + if (installBtn) installBtn.style.display = 'none'; |
68 | 55 | } |
69 | 56 | })(); |
0 commit comments