Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot create inline Web Worker in Firefox 35 addon

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?

like image 486
saloman Avatar asked Jan 26 '15 15:01

saloman


1 Answers

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');
like image 105
Alexander Salas Bastidas Avatar answered Oct 01 '22 18:10

Alexander Salas Bastidas