blob: 3fad2c9228ceb1573a09a18cba480fdd6747cdc4 (
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
|
<!doctype html>
<script>
const baseLocation = window.location;
const parentWorkerScript = `
const workerUrl =
new URL('postmessage-fetched-text.js', '${baseLocation}').href;
const childWorker = new Worker(workerUrl);
// When a message comes from the parent frame, sends a resource url to the
// child worker.
self.onmessage = (e) => {
childWorker.postMessage(e.data);
};
// When a message comes from the child worker, sends a content of fetch() to
// the parent frame.
childWorker.onmessage = (e) => {
self.postMessage(e.data);
};
`;
const blob = new Blob([parentWorkerScript], { type: 'text/javascript' });
const blobUrl = URL.createObjectURL(blob);
const worker = new Worker(blobUrl);
function fetch_in_worker(url) {
const resourceUrl = new URL(url, baseLocation).href;
return new Promise((resolve) => {
worker.onmessage = (event) => {
resolve(event.data);
};
worker.postMessage(resourceUrl);
});
}
</script>
|