forked from bitbaymarket/web-wallet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainSW.js
47 lines (42 loc) · 1.51 KB
/
mainSW.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
var isOnline = true; // Can be used to check if device is in offline/online mode
var isWebAPK = window.matchMedia('(display-mode: fullscreen)').matches; // Can be used to check if browser or webapk currently running
console.log("[*] The app is running as a "+(isWebAPK?"WebAPK":"Browser-Page"));
function checkOnlineStatus(){
isOnline = navigator.onLine;
console.log("[*] Connection status: "+(isOnline?"online":"offline"));
if (isOnline){
console.log("You are online");
}
else {
console.log("You are offline");
}
}
function init(){
// Register service worker
if ('serviceWorker' in navigator) {
console.log("[*] Register serviceWorker ...");
navigator.serviceWorker.register('/sw.js').then(function(registration) {
// Registration was successful
console.log('[*] ServiceWorker registration successful with scope: ', registration.scope);
}, function(err) {
// registration failed :(
console.log('[*] ServiceWorker registration failed: ', err);
});
}
else console.log("[*] ServiceWorker not supported by your browser!");
// Trigger install prompt for WebAPK
window.addEventListener("beforeinstallprompt",function(event){
console.log("[*] WebAPK install event fired!");
var btn = $("<button>install</button>");
$("body").append(btn);
btn.click(function(e){
event.prompt();
btn.remove();
});
});
// Initialize online/offline detection
checkOnlineStatus();
window.addEventListener("online",checkOnlineStatus);
window.addEventListener("offline",checkOnlineStatus);
}
init();