Cross Domain Web Worker?

后端 未结 4 1801
孤独总比滥情好
孤独总比滥情好 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条回答
  •  萌比男神i
    2021-02-05 11:43

    I also have the same problem, hope this can help you https://gist.github.com/jtyjty99999/a730a17258fca04bfca3

     function XHRWorker(url, ready, scope) {
          var oReq = new XMLHttpRequest();
          oReq.addEventListener('load', function() {
              var worker = new Worker(window.URL.createObjectURL(new Blob([this.responseText])));
              if (ready) {
                  ready.call(scope, worker);
              }
          }, oReq);
          oReq.open("get", url, true);
          oReq.send();
      }
    
      function WorkerStart() {
          XHRWorker("http://static.xxx.com/js/worker.js", function(worker) {
              worker.postMessage("hello world");
              worker.onmessage = function(e) {
                  console.log(e.data);
              }
          }, this);
      }
    
      WorkerStart();
    

提交回复
热议问题