-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker_proxy_iframe.js
69 lines (58 loc) · 1.89 KB
/
worker_proxy_iframe.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
65
66
67
68
69
// https://github.com/Rob--W/chrome-api/tree/master/worker_proxy
(function() {
let MSG_GET_TOKEN = "worker_proxy wants to get communication token"
/**
* Spawn a worker.
*
* @param {MessagePort} messagePort Messages received on this port will be
* sent to the Worker and vice versa.
* @param {MessagePort} metadataPort Port used for sending internal data
* such as error events.
* @param {string} url URL of Web worker (relative to the location of
* the HTML file that embeds this script).
*
* @return {undefined}
*/
function createWorker(messagePort, metadataPort, url) {
let worker = new Worker(url)
worker.onmessage = function(event) {
messagePort.postMessage(event.data)
}
worker.onerror = function(event) {
metadataPort.postMessage({
type: "error",
errorDetails: {
message: event.message,
filename: event.filename,
lineno: event.lineno,
colno: event.colno,
}
})
}
metadataPort.onmessage = function(event) {
if (event.data.type == "terminate") {
worker.terminate()
messagePort.close()
metadataPort.close()
}
}
messagePort.onmessage = function(event) {
worker.postMessage(event.data)
}
metadataPort.start()
messagePort.start()
}
function extensionProxyMessageHandler(event) {
if (!event.data || !event.data.channel_token) {
return
}
chrome.runtime.sendMessage(MSG_GET_TOKEN, function(token) {
if (!token || event.data.channel_token !== token) {
console.error("Auth failed, refused to create Worker channel.")
return
}
createWorker(event.ports[0], event.ports[1], event.data.worker_url)
})
}
window.addEventListener("message", extensionProxyMessageHandler)
}())