I have this content script that downloads some binary data using XHR, which is sent later to the background script:
var self = this;
var xhr = new XMLHttpReq
There is a better way to pass Blob
(or ArrayBuffer
) between any parts of the same Chrome extension (content scripts, background page and ordinary pages) then creating an ordinary JS Array or a binary string and passing this (sometimes extremely big) chunk of data in a message body! Remember that they are JSONified at the sender's end and then unJSONified at the receiver's end!
Just create and pass Object URL:
sendResponse(URL.createObjectURL(blob));
or create Blob first from ArrayBuffer:
var blob = new Blob([ arrayBuffer ], { type: 'image/jpeg' });
sendResponse(URL.createObjectURL(blob));
BTW XMLHttpRequest 2
can return both Blob
and ArrayBuffer
.