问题
I am aware that this question might be considered duplicate, but it is a new technology and I can not find a recent confirmation of my findings. I also think it potentially useful to have all the error messages in one place (feel free to add any other browsers).
trying to loads a worker script from another domain:
new Worker('http://otherdomain.co/worker.js');
I have set headers (using ModHeader Chrome Extension) to:
Access-Control-Allow-Methods:*
Access-Control-Allow-Origin:*
But in Chrome I get:
Uncaught SecurityError: Failed to construct 'Worker': Script at 'http:otherdomain.co/worker.js' cannot be accessed from origin
Safari give me:
[Error] SecurityError: DOM Exception 18: An attempt was made to break through the security policy of the user agent
Firefox gives me:
SecurityError: The operation is insecure.
Is it still that this is not something we can do? If so, what is considered the best practise work around?
回答1:
You are not allowed to create cross-domain web workers.
Note : The URI passed as parameter of the Worker constructor must obey the same-origin policy . There is currently disagreement among browsers vendors on what URIs are of the same-origin; Gecko 10.0 (Firefox 10.0 / Thunderbird 10.0 / SeaMonkey 2.7) and later do allow data URIs and Internet Explorer 10 does not allow Blob URIs as a valid script for workers.
Source: https://developer.mozilla.org/en/docs/Web/Guide/Performance/Using_web_workers
One workaround that I can think of is to create a server-side script to load the required remote JS file, and supply it to the browser from your domain.
Eg: You supply url to :
http://YOUR_DOMAIN/getRemoteJS.php
This PHP file will request the remote file on the server side, and echo it as the response, and set mime-type to application/javascript.
I have not personally tried this workaround, but you can perhaps look into it.
Good Luck!
回答2:
I know it's a little late now, but is this what you're looking for?
"Creates a url for the specified blob that can be passed to methods that expect a url. When done with the returned url, call revokeObjectURL() to free the resources associated with the created url."
This method easily allows you to create a Worker using a local script instead of a remote url.
来源:https://stackoverflow.com/questions/23953543/cross-domain-web-workers