|
1
|
|
|
if ('serviceWorker' in navigator) { |
|
|
|
|
|
|
2
|
|
|
|
|
3
|
|
|
jQuery(function () { |
|
4
|
|
|
showMessage('Service Worker active!', 'success'); |
|
5
|
|
|
}); |
|
6
|
|
|
const serviceWorkerScript = DOKU_BASE + 'doku.php?do=pwaoffline_serviceworker'; |
|
|
|
|
|
|
7
|
|
|
navigator.serviceWorker |
|
8
|
|
|
.register(serviceWorkerScript, { |
|
9
|
|
|
scope: '.' |
|
10
|
|
|
} |
|
11
|
|
|
) |
|
12
|
|
|
.then(function (registration) { |
|
13
|
|
|
if (registration.active) { |
|
14
|
|
|
registration.active.postMessage({ |
|
15
|
|
|
type: 'getLastUpdate', |
|
16
|
|
|
}); |
|
17
|
|
|
} |
|
18
|
|
|
} |
|
19
|
|
|
) |
|
20
|
|
|
; |
|
21
|
|
|
|
|
22
|
|
|
navigator.serviceWorker.addEventListener('message', function swMessageListener(event){ |
|
23
|
|
|
console.log('[Main Script] received message: ', event.data); |
|
|
|
|
|
|
24
|
|
|
switch(event.data.type) { |
|
25
|
|
|
case 'lastUpdate': |
|
26
|
|
|
jQuery.get(DOKU_BASE + 'lib/exe/ajax.php', { |
|
|
|
|
|
|
27
|
|
|
call: 'plugin_pwaoffline', |
|
28
|
|
|
ts: event.data.ts, |
|
29
|
|
|
}).done(function (data) { |
|
30
|
|
|
navigator.serviceWorker.controller.postMessage({ |
|
|
|
|
|
|
31
|
|
|
type: 'updatePages', |
|
32
|
|
|
pages: data, |
|
33
|
|
|
}); |
|
34
|
|
|
}); |
|
35
|
|
|
} |
|
36
|
|
|
}); |
|
37
|
|
|
|
|
38
|
|
|
} else { |
|
39
|
|
|
jQuery(function () { |
|
40
|
|
|
showMessage('Service Worker not supported!', 'error'); |
|
41
|
|
|
}); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
function reportStorageUsage() { |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param {int} size the size in byte |
|
48
|
|
|
* @returns {string} The size in mebibyte with appended unit |
|
49
|
|
|
*/ |
|
50
|
|
|
function getAsStringMiB(size) { |
|
51
|
|
|
const CONVERSION_FACTOR = 1024; |
|
52
|
|
|
return Math.round(size/(CONVERSION_FACTOR * CONVERSION_FACTOR) * 10) / 10 + ' MiB'; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
navigator.storage.estimate().then(estimate => { |
|
|
|
|
|
|
56
|
|
|
const perc = Math.round((estimate.usage / estimate.quota) * 100 * 100) / 100; |
|
57
|
|
|
const severity = perc > 80 ? 'error' : perc > 20 ? 'notify' : 'info'; |
|
58
|
|
|
const usage = getAsStringMiB(estimate.usage); |
|
59
|
|
|
const quota = getAsStringMiB(estimate.quota); |
|
60
|
|
|
const msg = 'Current storage usage on this device for this origin: ' + usage + '/' + quota; |
|
61
|
|
|
showMessage(msg + ' ( ' + perc + ' % )', severity); |
|
62
|
|
|
}); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
function showMessage(message, severity) { |
|
66
|
|
|
let $msgArea = jQuery('div.pwaOfflineMSGArea'); |
|
67
|
|
|
if (!$msgArea.length) { |
|
68
|
|
|
$msgArea = jQuery('<div>').addClass('pwaOfflineMSGArea'); |
|
69
|
|
|
jQuery('#dokuwiki__header').after($msgArea); |
|
70
|
|
|
} |
|
71
|
|
|
$msgArea.append(jQuery('<div>') |
|
72
|
|
|
.text(message) |
|
73
|
|
|
.addClass(severity) |
|
74
|
|
|
); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
jQuery(function () { |
|
78
|
|
|
|
|
79
|
|
|
const LIVE_DELAY = 10; |
|
80
|
|
|
const now = Math.floor(Date.now() / 1000); |
|
81
|
|
|
|
|
82
|
|
|
const lag = now - JSINFO.plugins.pwaoffline.ts; |
|
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
if (lag > LIVE_DELAY) { |
|
85
|
|
|
showMessage('This page may have been loaded from cache. Age in seconds: ' + lag, 'notify'); |
|
86
|
|
|
jQuery('.dokuwiki').addClass('pwa--is-offline'); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
reportStorageUsage(); |
|
90
|
|
|
|
|
91
|
|
|
// if (!navigator.onLine) { |
|
92
|
|
|
// jQuery('<div></div>').text('You appear to be offline') |
|
93
|
|
|
// } |
|
94
|
|
|
|
|
95
|
|
|
}); |
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.