I'm developing a XUL based Firefox extension. I'm trying to create an inline Web Worker using BLOB. The code used to work in Firefox 33 but after update to Firefox 35 I get an error. Here is a code sample:
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);
}
And I get a following error:
Failed to load script (nsresult = 0x805303f4)
I'm aware that this error appears when Web Worker tries to load a script from a different domain but I cannot figure out why this is happening in my case. The url I get from createObjectURL() function appears to be invalid. It contains "null/" prefix.
Does anybody have an explanation what is going on? What is the possible fix here?
This example work for me, tested from Firefox 37 to 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');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With