|
| 1 | +/* ============================================================================== |
| 2 | + - File: pwa-handler.js (Progressive Web Application Orchestrator) |
| 3 | + - Author: Amey Thakur |
| 4 | + - Profile: https://github.com/Amey-Thakur |
| 5 | + - Repository: https://github.com/Amey-Thakur/Amey-Thakur.github.io |
| 6 | + - Release Date: December 16, 2025 |
| 7 | + - License: MIT License |
| 8 | + - ============================================================================== |
| 9 | + - |
| 10 | + - DESCRIPTION: |
| 11 | + - This component manages the client-side lifecycle of the AmeyArc PWA. |
| 12 | + - It handles service worker registration, installation event capturing, |
| 13 | + - and non-intrusive UI suggestions for device-level integration. |
| 14 | + - |
| 15 | + - HOW IT WORKS: |
| 16 | + - The script validates browser capabilities before registering the site-wide |
| 17 | + - service worker. It intercepts the native 'beforeinstallprompt' event to |
| 18 | + - deliver a bespoke, high-fidelity installation banner that encourages users |
| 19 | + - to add the archive to their home screen without disrupting the focal experience. |
| 20 | + - |
| 21 | + - TECH STACK: |
| 22 | + - - Service Worker API |
| 23 | + - - Web App Manifest Specification |
| 24 | + - - Client-side UI Orchestration (Vanilla JS) |
| 25 | + - |
| 26 | + - ============================================================================== */ |
| 27 | + |
| 28 | +(function () { |
| 29 | + /* Technical Guard: Validating browser capabilities for service worker support. */ |
| 30 | + if ('serviceWorker' in navigator) { |
| 31 | + window.addEventListener('load', () => { |
| 32 | + navigator.serviceWorker.register('/sw.js').then((registration) => { |
| 33 | + // Registration successful: Site is now offline-capable. |
| 34 | + }).catch((err) => { |
| 35 | + console.warn('Service worker orchestration failed:', err); |
| 36 | + }); |
| 37 | + }); |
| 38 | + } |
| 39 | + |
| 40 | + let deferredPrompt; |
| 41 | + /* Installation Hook: Capturing the browser's native installation trigger. */ |
| 42 | + window.addEventListener('beforeinstallprompt', (e) => { |
| 43 | + // Prevent default browser prompt sequence. |
| 44 | + e.preventDefault(); |
| 45 | + deferredPrompt = e; |
| 46 | + |
| 47 | + // Deployment of the personalized installation suggestion at the top. |
| 48 | + renderInstallSuggestion(); |
| 49 | + }); |
| 50 | + |
| 51 | + /** |
| 52 | + * renderInstallSuggestion: Orchestrates a non-intrusive UI suggestion |
| 53 | + * at the document apex, encouraging the archival of AmeyArc to the device. |
| 54 | + */ |
| 55 | + function renderInstallSuggestion() { |
| 56 | + if (localStorage.getItem('pwa-suggestion-dismissed')) return; |
| 57 | + |
| 58 | + const banner = document.createElement('div'); |
| 59 | + banner.id = 'pwa-install-banner'; |
| 60 | + banner.style.cssText = ` |
| 61 | + position: fixed; |
| 62 | + top: 0; |
| 63 | + left: 0; |
| 64 | + width: 100%; |
| 65 | + background: var(--entry); |
| 66 | + border-bottom: 1px solid var(--border); |
| 67 | + padding: 10px 24px; |
| 68 | + z-index: 10001; |
| 69 | + display: flex; |
| 70 | + align-items: center; |
| 71 | + justify-content: center; |
| 72 | + cursor: pointer; |
| 73 | + box-shadow: 0 2px 8px rgba(0,0,0,0.1); |
| 74 | + animation: slideDown 0.5s cubic-bezier(0.4, 0, 0.2, 1); |
| 75 | + transition: opacity 0.3s ease; |
| 76 | + `; |
| 77 | + |
| 78 | + banner.innerHTML = ` |
| 79 | + <div style="display: flex; align-items: center; gap: 12px;"> |
| 80 | + <span style="font-size: 1.2rem;">💭</span> |
| 81 | + <span style="font-size: 13px; color: var(--primary); font-weight: 500;"> |
| 82 | + Install <strong>AmeyArc</strong> for a focused, offline-ready archival experience. |
| 83 | + </span> |
| 84 | + </div> |
| 85 | + <button id="pwa-dismiss" style="position: absolute; right: 24px; background: none; border: none; color: var(--secondary); cursor: pointer; font-size: 16px;">×</button> |
| 86 | + `; |
| 87 | + |
| 88 | + banner.onclick = (e) => { |
| 89 | + if (e.target.id === 'pwa-dismiss') { |
| 90 | + banner.style.opacity = '0'; |
| 91 | + setTimeout(() => banner.remove(), 300); |
| 92 | + localStorage.setItem('pwa-suggestion-dismissed', 'true'); |
| 93 | + return; |
| 94 | + } |
| 95 | + banner.remove(); |
| 96 | + deferredPrompt.prompt(); |
| 97 | + deferredPrompt.userChoice.then((choiceResult) => { |
| 98 | + deferredPrompt = null; |
| 99 | + }); |
| 100 | + }; |
| 101 | + |
| 102 | + document.body.prepend(banner); |
| 103 | + |
| 104 | + // Inject animation keyframes |
| 105 | + const style = document.createElement('style'); |
| 106 | + style.textContent = ` |
| 107 | + @keyframes slideDown { |
| 108 | + from { transform: translateY(-100%); } |
| 109 | + to { transform: translateY(0); } |
| 110 | + } |
| 111 | + `; |
| 112 | + document.head.appendChild(style); |
| 113 | + } |
| 114 | +})(); |
0 commit comments