<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>ImJoy Plugin (web-worker)</title>
    <meta name="description" content="Template for ImJoy plugin">
    <meta name="author" content="ImJoy-Team">
</head>

<body>
<script id="worker" type="javascript/worker">
window = self;

self.onmessage = function(e) {
const config = e.data

importScripts("https://cdn.jsdelivr.net/npm/imjoy-rpc@0.5.18-alpha.2/dist/hypha-rpc-websocket.min.js")

hyphaWebsocketClient.connectToServer(config).then(async (api)=>{
await hyphaWebsocketClient.loadRequirements([{% for req in requirements %}"{{req}}", {% endfor %}])
{{ script | safe }}
}).catch(console.error)
}
</script>
<script>
window.onload = function() {
    const blob = new Blob([
        document.querySelector('#worker').textContent
    ], { type: "text/javascript" })
    const worker = new Worker(window.URL.createObjectURL(blob));
    worker.onerror = console.error
    worker.onmessage = console.log
    const cfg = {{ config | tojson(indent=2) }}
    const config = Object.assign(cfg, Object.fromEntries(new URLSearchParams(window.location.search)));
    if(!config.server_url) config.server_url = window.location.origin;
    worker.postMessage(config); 
}
</script>
</body>
</html>