Search code examples
javascriptweb-worker

is there a way to create SharedWorker that loads the source from another site?


Say my html file is from http://foo.com/index.html, in it, there's a <script> tag to http://bar.com/bar.js. In bar.js, I want to start a SharedWorker where the url is http://bar.com/worker.js. Is there a way to achieve this (maybe something like jsonp)?


Solution

  • This is a solution I found:

    • Write the script inside a function (can be an inner function)
    • get the text using function.toString() (removing the function declaration and closing brace)
    • append the text to a BlobBuilder and get the blob
    • Use window.URL.createObjectURL to convert the blob to a url
    • use that url for the worker