forked from markmarijnissen/cordova-app-loader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
autoupdate.js
64 lines (56 loc) · 1.43 KB
/
autoupdate.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
(function(){
// Check for Cordova
var isCordova = typeof cordova !== 'undefined',
// CordovaPromiseFS
fs,
// CordovaFileLoader
loader,
// script-tag...
script,
// ...that contains the serverRoot
serverRoot;
// Get serverRoot from script tag.
script = document.querySelector('script[server]');
if(script) serverRoot= script.getAttribute('server');
if(!serverRoot) {
throw new Error('Add a "server" attribute to the bootstrap.js script!');
}
// Initialize filesystem and loader
fs = new CordovaPromiseFS({
persistent: isCordova, // Chrome should use temporary storage.
Promise: Promise
});
loader = new CordovaAppLoader({
fs: fs,
localRoot: 'app',
serverRoot: serverRoot,
mode: 'mirror',
cacheBuster: true
});
// Check > Download > Update
function check(){
loader.check()
.then(function(){
return loader.download();
})
.then(function(){
return loader.update();
},function(err){
console.error('Auto-update error:',err);
});
}
// Couple events:
// 1. On launch
check();
// 2. Cordova: On resume
fs.deviceready.then(function(){
document.addEventListener('resume',check);
});
// 3. Chrome: On page becomes visible again
function handleVisibilityChange() {
if (!document.webkitHidden) {
check();
}
}
document.addEventListener("webkitvisibilitychange", handleVisibilityChange, false);
})();