sal*_*man 7 javascript firefox firefox-addon web-worker
我正在开发基于XUL的Firefox扩展.我正在尝试使用BLOB创建内联Web Worker.代码曾经在Firefox 33中运行但在更新到Firefox 35后我收到错误.这是一个代码示例:
try {
var blob = new Blob(["function f(){}"], {type: "application/javascript"});
var url = window.URL.createObjectURL(blob); //blob:null/371e34bd-1fbf-4f66-89cc-24d0c1c7bad5
return new Worker(url);
} catch(e) {
console.error(e);
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
Failed to load script (nsresult = 0x805303f4)
Run Code Online (Sandbox Code Playgroud)
我知道当Web Worker尝试从其他域加载脚本时会出现此错误,但我无法弄清楚为什么会发生这种情况.我从createObjectURL()函数获取的url似乎无效.它包含"null /"前缀.
有没有人解释发生了什么?这可能有什么问题?
这个例子对我有用,在 Firefox 37 到 39.0a2 上进行了测试。
// 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');
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
220 次 |
| 最近记录: |