Passed
Push — master ( cc2712...3e5b93 )
by Michael
01:07
created

T_IF ➔ jQuery   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 1
rs 10
c 1
b 0
f 0
1
if ('serviceWorker' in navigator) {
0 ignored issues
show
Bug introduced by
The variable navigator seems to be never declared. If this is a global, consider adding a /** global: navigator */ comment.

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.

Loading history...
2
3
    jQuery(function () {
4
        showMessage('Service Worker active!', 'success');
5
    });
6
    const serviceWorkerScript = DOKU_BASE + 'doku.php?do=pwaoffline_serviceworker';
0 ignored issues
show
Bug introduced by
The variable DOKU_BASE seems to be never declared. If this is a global, consider adding a /** global: DOKU_BASE */ comment.

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.

Loading history...
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);
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
24
        switch(event.data.type) {
25
            case 'lastUpdate':
26
                jQuery.get(DOKU_BASE + 'lib/exe/ajax.php', {
0 ignored issues
show
Bug introduced by
The variable DOKU_BASE seems to be never declared. If this is a global, consider adding a /** global: DOKU_BASE */ comment.

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.

Loading history...
27
                    call: 'plugin_pwaoffline',
28
                    ts: event.data.ts,
29
                }).done(function (data) {
30
                    navigator.serviceWorker.controller.postMessage({
0 ignored issues
show
Bug introduced by
The variable navigator seems to be never declared. If this is a global, consider adding a /** global: navigator */ comment.

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.

Loading history...
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 => {
0 ignored issues
show
Bug introduced by
The variable navigator seems to be never declared. If this is a global, consider adding a /** global: navigator */ comment.

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.

Loading history...
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;
0 ignored issues
show
Bug introduced by
The variable JSINFO seems to be never declared. If this is a global, consider adding a /** global: JSINFO */ comment.

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.

Loading history...
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
});