44 < meta charset ="UTF-8 ">
55 < meta name ="viewport " content ="width=device-width, initial-scale=1.0 ">
66 < title > HTMLRunner</ title >
7+ < link rel ="shortcut icon " type ="image/x-icon " href ="favicon.png ">
8+ < link rel ="apple-touch-icon " href ="favicon.png ">
9+ < link rel ="manifest " href ="manifest.json ">
710 <!-- Font Awesome CDN -->
811 < link rel ="stylesheet " href ="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css ">
912 <!-- CodeMirror CSS -->
2528 < div class ="header ">
2629 < div class ="logo "> HTMLRunner</ div >
2730 < div class ="controls ">
31+ < button class ="btn " id ="install-button " hidden > Install App</ button >
2832 < button class ="btn btn-run " onclick ="runCode() "> < i class ="fas fa-play "> </ i > Run</ button >
2933 < button class ="btn btn-format " onclick ="formatCode() "> < i class ="fas fa-code "> </ i > Format</ button >
3034 < button class ="btn btn-reset " onclick ="resetCode() "> < i class ="fas fa-undo "> </ i > Reset</ button >
6064 </ div >
6165 </ div >
6266 </ div >
67+ < script >
68+ document . addEventListener ( "DOMContentLoaded" , function ( ) {
69+ // Register the service worker
70+ if ( "serviceWorker" in navigator ) {
71+ navigator . serviceWorker . register ( "service-worker.js" ) ;
72+ }
73+
74+ let deferredPrompt ;
75+ const installButton = document . getElementById ( "install-button" ) ;
76+
77+ // Listen for the install prompt
78+ window . addEventListener ( "beforeinstallprompt" , ( e ) => {
79+ e . preventDefault ( ) ;
80+ deferredPrompt = e ;
81+ installButton . hidden = false ;
82+ } ) ;
83+
84+ // Handle install button click
85+ installButton . addEventListener ( "click" , async ( ) => {
86+ const isStandalone = window . matchMedia ( "(display-mode: standalone)" ) . matches || window . navigator . standalone ;
87+
88+ if ( isStandalone ) {
89+ console . log ( "App is already installed" ) ;
90+ return ;
91+ }
92+
93+ if ( deferredPrompt ) {
94+ deferredPrompt . prompt ( ) ;
95+ const { outcome } = await deferredPrompt . userChoice ;
96+ deferredPrompt = null ;
97+ console . log ( outcome === "accepted" ? "User accepted install prompt" : "User dismissed install prompt" ) ;
98+ } else {
99+ if ( window . matchMedia ( "(display-mode: browser)" ) . matches ) {
100+ window . location . assign ( "chrome://apps/" ) ;
101+ }
102+ }
103+ } ) ;
104+ } ) ;
105+ </ script >
63106 < script src ="main.js "> </ script >
64107</ body >
65108</ html >
0 commit comments