无法在Firefox 35插件中创建内联Web Worker

时间:2015-01-26 15:39:44

标签: 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);
}

我收到以下错误:

Failed to load script (nsresult = 0x805303f4)

我知道当Web Worker尝试从其他域加载脚本时会出现此错误,但我无法弄清楚为什么会出现这种情况。我从createObjectURL()函数获取的url似乎无效。它包含" null /"前缀。

有人解释发生了什么事吗?这里有什么可能的解决办法?

1 个答案:

答案 0 :(得分:1)

此示例适用于我,从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');