(() => { const { EventManager } = ShopbySkin; // PWA 설치 프롬프트 저장 변수 let deferredPrompt = null; let isPageLoaded = false; // beforeinstallprompt 이벤트 캡처 (PAGE_LOAD_COMPLETED 보다 늦게 발생할 수 있음) window.addEventListener('beforeinstallprompt', (e) => { e.preventDefault(); deferredPrompt = e; // 페이지 로드가 이미 완료됐으면 버튼 다시 표시 if (isPageLoaded) { const installBtn = document.querySelector('.pwa-install-btn'); if (installBtn) { installBtn.style.display = ''; } } }); // 이미 설치된 경우 처리 window.addEventListener('appinstalled', () => { deferredPrompt = null; const installBtn = document.querySelector('.pwa-install-btn'); if (installBtn) { installBtn.style.display = 'none'; } }); // ShopBy 페이지 로드 완료 후 PWA 버튼 초기화 EventManager.on('PAGE_LOAD_COMPLETED', () => { isPageLoaded = true; const installBtn = document.querySelector('.pwa-install-btn'); if (!installBtn) return; // 이미 설치된 앱인지 확인 (standalone 모드 = PWA로 실행 중) const isStandalone = window.matchMedia('(display-mode: standalone)').matches || window.navigator.standalone === true; if (isStandalone) { installBtn.style.display = 'none'; return; } // 미지원 브라우저 체크 (beforeinstallprompt 미지원) const isIOS = /iPad|iPhone|iPod/.test(navigator.userAgent); const isUnsupportedBrowser = /Firefox|MSIE|Trident|Opera Mini|UCBrowser/i.test(navigator.userAgent) || (/Safari/.test(navigator.userAgent) && !/Chrome/.test(navigator.userAgent)); // 조건: deferredPrompt 있거나 미지원 브라우저면 버튼 표시 if (deferredPrompt || isUnsupportedBrowser) { installBtn.style.display = ''; } else { // 그 외 (이미 설치됨) → 숨김 installBtn.style.display = 'none'; } // 클릭 이벤트 바인딩 installBtn.addEventListener('click', () => { // 설치 프롬프트가 있으면 실행 if (deferredPrompt) { deferredPrompt.prompt(); deferredPrompt = null; return; } // iOS Safari 안내 const isSafari = /Safari/.test(navigator.userAgent) && !/Chrome/.test(navigator.userAgent); if (isIOS && isSafari) { alert( '홈 화면에 추가하려면:\n1. 하단의 공유 버튼(📤)을 탭하세요\n2. "홈 화면에 추가"를 선택하세요' ); return; } // 그 외 (미지원 브라우저) alert('이 브라우저는 앱 설치를 지원하지 않습니다.\nChrome, Edge, 웨일 등을 이용해주세요.'); }); }); })();