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