-
Notifications
You must be signed in to change notification settings - Fork 2
/
script.js
113 lines (98 loc) · 3.61 KB
/
script.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
if ('serviceWorker' in navigator) {
jQuery(function () {
showMessage('Service Worker active!', 'success');
});
const serviceWorkerScript = DOKU_BASE + 'doku.php?do=pwaoffline_serviceworker';
navigator.serviceWorker
.register(serviceWorkerScript, {
scope: '.'
}
)
.then(function (registration) {
if (registration.active) {
registration.active.postMessage({
type: 'getLastUpdate',
});
registration.active.postMessage({
type: 'getHashVersion',
});
}
}
)
;
navigator.serviceWorker.addEventListener('message', function swMessageListener(event){
console.log('[Main Script] received message: ', event.data);
switch(event.data.type) {
case 'lastUpdate':
jQuery.get(DOKU_BASE + 'lib/exe/ajax.php', {
call: 'plugin_pwaoffline',
ts: event.data.ts,
}).done(function (data) {
navigator.serviceWorker.controller.postMessage({
type: 'updatePages',
pages: data,
});
});
break;
case 'swHashVersion':
if (event.data.hash !== JSINFO.plugins.pwaoffline.swHashVersion) {
showMessage(
`You are using an outdated serviceWorker! active: ${event.data.hash} current: ${JSINFO.plugins.pwaoffline.swHashVersion}`,
'notify'
);
return;
}
showMessage('ServiceWorker is up-to-date!', 'success');
}
});
} else {
jQuery(function () {
showMessage('Service Worker not supported!', 'error');
});
}
function reportStorageUsage() {
/**
* @param {int} size the size in byte
* @returns {string} The size in mebibyte with appended unit
*/
function getAsStringMiB(size) {
const CONVERSION_FACTOR = 1024;
return Math.round(size/(CONVERSION_FACTOR * CONVERSION_FACTOR) * 10) / 10 + ' MiB';
}
if (!navigator.storage) {
showMessage('Storage API is not available?', 'notify');
return;
}
navigator.storage.estimate().then(estimate => {
const perc = Math.round((estimate.usage / estimate.quota) * 100 * 100) / 100;
const severity = perc > 80 ? 'error' : perc > 20 ? 'notify' : 'info';
const usage = getAsStringMiB(estimate.usage);
const quota = getAsStringMiB(estimate.quota);
const msg = 'Current storage usage on this device for this origin: ' + usage + '/' + quota;
showMessage(msg + ' ( ' + perc + ' % )', severity);
});
}
function showMessage(message, severity) {
let $msgArea = jQuery('div.pwaOfflineMSGArea');
if (!$msgArea.length) {
$msgArea = jQuery('<div>').addClass('pwaOfflineMSGArea');
jQuery('#dokuwiki__header').after($msgArea);
}
$msgArea.append(jQuery('<div>')
.text(message)
.addClass(severity)
);
}
jQuery(function () {
const LIVE_DELAY = 10;
const now = Math.floor(Date.now() / 1000);
const lag = now - JSINFO.plugins.pwaoffline.ts;
if (lag > LIVE_DELAY) {
showMessage('This page may have been loaded from cache. Age in seconds: ' + lag, 'notify');
jQuery('.dokuwiki').addClass('pwa--is-offline');
}
reportStorageUsage();
// if (!navigator.onLine) {
// jQuery('<div></div>').text('You appear to be offline')
// }
});