summaryrefslogtreecommitdiff
path: root/worker.js
blob: ea568c8a211a26ed41fd3fb496e69abddae488e8 (plain)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//importScripts('lib/localforage.min.js');

const CACHE_PREFIX = 'epube';
const CACHE_NAME = CACHE_PREFIX + '-v2';
const CACHE_URLS = [
			'manifest.json',
			'img/ic_launcher_web.png',
			'img/favicon.png',
			'read.html',
			'js/common.js',
			'js/read.js',
			'js/offline.js',
			'js/reader.js',
			'js/dict.js',
			'css/read.css',
			'css/reader.css',
			'css/index.css',
			'css/transitions.css',
			'offline.html',
			'themes/default.css',
			'themes/light.css',
			'themes/mocca.css',
			'themes/night.css',
			'themes/plan9.css',
			'themes/gray.css',
			'themes/sepia.css',
			'lib/promise.js',
			'lib/fetch.js',
			'lib/zip.min.js',
			'lib/epub.js',
			'lib/localforage.min.js',
			'lib/jquery.mobile-events.min.js',
			'lib/holder.min.js',
			'lib/bootstrap/v3/css/bootstrap-theme.min.css',
			'lib/bootstrap/v3/css/bootstrap.min.css',
			'lib/bootstrap/v3/css/theme-dark.min.css',
			'lib/bootstrap/v3/js/jquery.js',
			'lib/bootstrap/v3/js/bootstrap.min.js',
			'lib/bootstrap/v3/fonts/glyphicons-halflings-regular.woff2',
			'lib/fonts/pmn-caecilia-55.ttf',
			'lib/fonts/pmn-caecilia-56.ttf',
			'lib/fonts/pmn-caecilia-75.ttf'
		];

self.addEventListener('activate', function(event) {
	event.waitUntil(
		caches.keys().then(function(keyList) {
			return Promise.all(keyList.map((key) => {
				if (key.indexOf(CACHE_PREFIX) != -1 && key != CACHE_NAME) {
					return caches.delete(key);
				}
				return false;
			}));
		})
    );
});

function send_message(client, msg) {
	client.postMessage(msg);
}

function send_broadcast(msg) {
	self.clients.matchAll().then((clients) => {
		clients.forEach((client) => {
			send_message(client, msg);
		})
	})
}

self.addEventListener('message', function(event){
	if (event.data == 'refresh-cache') {
		console.log("refreshing cache...");

		send_broadcast('refresh-started');

		return caches.open(CACHE_NAME).then(function(cache) {

			Promise.all(CACHE_URLS.map((url) => {
				return fetch(url + "?ts=" + Date.now()).then((resp) => {
					console.log('got', resp.url, resp);

					send_broadcast('refreshed:' + resp.url);

					if (resp.status == 200) {
						return cache.put(url, resp);
					} else if (resp.status == 404) {
						return cache.delete(url);
					}
				});

			})).then(function() {
				console.log('all done');
				send_broadcast('client-reload');
			});
		});
	}
});

this.addEventListener('fetch', function(event) {
	const req = event.request.clone();

	event.respondWith(
		caches.match(req).then(function(resp) {

			if (resp) {
				return resp;
			}

			if (!navigator.onLine) {
				if (req.url.match("read.html")) {
					return caches.match("read.html");
				}

				if (req.url.match("offline.html")) {
					return caches.match("offline.html");
				}
			}

			console.log('cache miss for', req.url, 'OL:', navigator.onLine);

			return fetch(req).then(function(resp) {

				if (resp.status == 200) {
					if (resp.url.match("backend.php\\?op=(cover|getinfo)")) {
						return caches.open(CACHE_NAME).then(function(cache) {
							cache.put(resp.url, resp.clone());
							return resp;
						});
					}
				}

				return resp;
			}).catch(function() {
				if (req.url[req.url.length-1] == "/" || req.url.match("index.php")) {
					return caches.match("offline.html");
				} else {
					return caches.match(req.url);
				}
			});
		})
	);
});