问题描述
如何使用字符串创建Web worker(通过POST请求提供)?
How can I use create a Web worker from a string (which is supplied via a POST request)?
我能想到的一种方式,但我是不确定如何实现它,是通过从服务器响应创建一个数据URI,并将其传递给Worker构造函数,但我听说有些浏览器不允许这样做,因为相同的源策略。
One way I can think of, but I'm not sure how to implement it, is by creating a data-URI from the server response, and passing that to the Worker constructor, but I've heard that some browsers don't allow this, because of the same origin policy.
: / p>
MDN states the uncertainty about the origin policy around data URI's:
这里还有一篇文章
推荐答案
可用于从字符串创建Web worker。可以使用 API 或。
URL.createObjectURL(<Blob blob>)
can be used to create a Web worker from a string. The blob can be created using the BlobBuilder
API or the Blob
constructor.
演示:
// URL.createObjectURL
window.URL = window.URL || window.webkitURL;
// "Server response", used in all examples
var response = "self.onmessage=function(e){postMessage('Worker: '+e.data);}";
var blob;
try {
blob = new Blob([response], {type: 'application/javascript'});
} catch (e) { // Backwards-compatibility
window.BlobBuilder = window.BlobBuilder || window.WebKitBlobBuilder || window.MozBlobBuilder;
blob = new BlobBuilder();
blob.append(response);
blob = blob.getBlob();
}
var worker = new Worker(URL.createObjectURL(blob));
// Test, used in all examples:
worker.onmessage = function(e) {
alert('Response: ' + e.data);
};
worker.postMessage('Test');
兼容性
支持网络工作者以下浏览器:
- Chrome 3
- Firefox 3.5
- IE 10
- Opera 10.60
- Safari 4
- Chrome 3
- Firefox 3.5
- IE 10
- Opera 10.60
- Safari 4
此方法的支持基于支持 Blob
API和 URL.createObjectUrl
方法。 :
This method's support is based on the support of the Blob
API and the URL.createObjectUrl
method. Blob
compatibility:
- Chrome 8+(
WebKitBlobBuilder
),20 +(Blob
构造函数) - Firefox 6+(
MozBlobBuilder
),13 +(Blob
构造函数) - Safari 6+(
Blob
构造函数)
- Chrome 8+ (
WebKitBlobBuilder
), 20+ (Blob
constructor) - Firefox 6+ (
MozBlobBuilder
), 13+ (Blob
constructor) - Safari 6+ (
Blob
constructor)
IE10支持 MSBlobBuilder
和 URL.createObjectURL
。但是,尝试从 blob:
-URL创建Web Worker会引发SecurityError。
IE10 supports MSBlobBuilder
and URL.createObjectURL
. However, trying to create a Web Worker from a blob:
-URL throws a SecurityError.
Opera 12不会支持 URL
API。由于。
Opera 12 does not support URL
API. Some users may have a fake version of the URL
object, thanks to this hack in browser.js
.
Opera支持将数据URI作为 Worker的参数
构造函数。注意:不要忘记(例如#
和%
)。
Opera supports data-URIs as an argument to the Worker
constructor. Note: Do not forget to escape special characters (Such as #
and %
).
// response as defined in the first example
var worker = new Worker('data:application/javascript,' +
encodeURIComponent(response) );
// ... Test as defined in the first example
演示:
eval
可用作Safari(< 6)和IE的后备10。
eval
can be used as a fallback for Safari (<6) and IE 10.
// Worker-helper.js
self.onmessage = function(e) {
self.onmessage = null; // Clean-up
eval(e.data);
};
// Usage:
var worker = new Worker('Worker-helper.js');
// `response` as defined in the first example
worker.postMessage(response);
// .. Test as defined in the first example
这篇关于如何从字符串创建Web Worker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!