forked from matthew-andrews/workshop-making-it-work-offline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
appcache.js
51 lines (44 loc) · 1.59 KB
/
appcache.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
(function() {
var cookie = 'up';
var statuses = {
"-1": 'timeout',
"0": 'uncached',
"1": 'idle',
"2": 'checking',
"3": 'downloading',
"4": 'updateready',
"5": 'obsolete'
};
// Start the AppCache loading process when this file executes
load();
function onMessage(event) {
if (event.data && event.data.type && event.data.type === 'appcache:event') {
onEvent.apply(window, event.data.args || []);
}
}
function load() {
window.addEventListener("message", onMessage, false);
// HACK: Set a cookie so that the application
// root returns a Javascript bootstrap rather
// than content.
var cookieExpires = new Date(new Date().getTime() + 60 * 5 * 1000);
document.cookie = cookie + "=1;path=/;expires=" + cookieExpires.toGMTString();
var iframe = document.createElement('IFRAME');
iframe.setAttribute('style', 'width:0px; height:0px; visibility:hidden; position:absolute; border:none');
iframe.setAttribute('src', '/iframe.html');
iframe.setAttribute('id', 'appcache');
document.body.appendChild(iframe);
}
function onEvent(eventCode) {
var s = statuses[eventCode], loaderEl, cookieExpires;
if (s === 'uncached' || s === 'idle' || s === 'obsolete' || s === 'timeout' || s === 'updateready') {
loaderEl = document.getElementById('appcache');
loaderEl.parentNode.removeChild(loaderEl);
// Remove appcacheUpdate cookie
cookieExpires = new Date(new Date().getTime() - 60 * 5 * 1000);
document.cookie = cookie + "=;path=/;expires=" + cookieExpires.toGMTString();
// Remove message listener
window.removeEventListener("message", onMessage);
}
}
}());