blob: 0d24ffc1f33330fb59996c3a65b7530ced0f6806 (
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
|
'use strict';
self.onfetch = function(event) {
if (event.request.url.indexOf('non-existent-file.txt') !== -1) {
event.respondWith(new Response('Response from service worker'));
} else if (event.request.url.indexOf('/iframe_page') !== -1) {
event.respondWith(new Response(
'<!DOCTYPE html>\n' +
'<script>\n' +
'function performSyncXHROnWorker(url) {\n' +
' return new Promise((resolve) => {\n' +
' var worker =\n' +
' new Worker(\'./worker_script\');\n' +
' worker.addEventListener(\'message\', (msg) => {\n' +
' resolve(msg.data);\n' +
' });\n' +
' worker.postMessage({\n' +
' url: url\n' +
' });\n' +
' });\n' +
'}\n' +
'</script>',
{
headers: [['content-type', 'text/html']]
}));
} else if (event.request.url.indexOf('/worker_script') !== -1) {
event.respondWith(new Response(
'self.onmessage = (msg) => {' +
' const syncXhr = new XMLHttpRequest();' +
' syncXhr.open(\'GET\', msg.data.url, false);' +
' syncXhr.send();' +
' self.postMessage({' +
' status: syncXhr.status,' +
' responseText: syncXhr.responseText' +
' });' +
'}',
{
headers: [['content-type', 'application/javascript']]
}));
}
};
|