Cross Domain Web Worker?

后端 未结 4 1748
孤独总比滥情好
孤独总比滥情好 2021-02-05 10:51

I have https://domain1.com (domain1) and https://domain2.com (domain2).

Domain2 serves a page containing javascript with this header:

"Access-Control-         


        
4条回答
  •  后悔当初
    2021-02-05 11:30

    It's pretty funny, but importScripts() inside the worker does not have the same origin restriction. So I used this plain workaround:

    const workerUrl = 'https://domain2.com/script.js';
    const workerBlob = new Blob([
        'importScripts(' + JSON.stringify(workerUrl) + ')',
    ], {type: 'application/javascript'});
    const blobUrl = window.URL.createObjectURL(workerBlob);
    const worker = new Worker(blobUrl);
    

    Somewhat simpler than fetching script manually.

提交回复
热议问题