1

I am writing a Chrome extension, where the background page has created a Blob object and now I want to somehow send this object via message passing to the main page with a content script.

Apparently, sending Blob directly is not possible, since it just ends up as being an ordinary Object on the other side, what is the best way to actually send the content of the Blob?

edit: to make sure what I want

  • I have Blob on the sender's side
  • I want Blob on the receiver side with exactly the same content as the original Blob. It doesn't need to be the same object.

Relevant documentation: http://developer.chrome.com/extensions/messaging.html

Karel Bílek
  • 36,467
  • 31
  • 94
  • 149
  • 1
    Method in the "possible duplicate" has this small caveat - *WARNING: This method only works for small amounts of data. When the size of the typed array exceeds 125836, a RangeError will be thrown. If you need to handle large pieces of data, use other methods to do the conversion between typed arrays and plain arrays.* - and that's a deal breaker for me – Karel Bílek Jul 07 '13 at 11:39

1 Answers1

3

How about this?

blob = new window.Blob(["a", "b", 3, "c"], {
    "type": 'text/plain'
});
href = URL.createObjectURL(blob);

Then you should be able to send the href to the blob in a message.

stackunderflow
  • 953
  • 7
  • 17
  • 2
    That's not going to work because of the Same Origin Policy. Even if the extension has the `` permission, it can't read `blob:`-URIs from a different domain. – Rob W Jul 07 '13 at 11:06
  • Works for me! It doesn't answer the question I was asking, but it tells me what I *actually needed*. Thanks! – Karel Bílek Jul 07 '13 at 11:22
  • OK, now this is really weird. If I still want to send the blob and use it on the other side for something, the easiest way is to generate URL and ***download it again by XML HTTP request*** on the other side. That feels totally hacky but it works the best. – Karel Bílek Jul 07 '13 at 11:53
  • Yeah, I actually use a blob in the Popchrom extension for downloading all abbreviations into a text file via a[download] attribute. See https://code.google.com/p/trnsfrmr/source/browse/Transformer/scripts/options.js – stackunderflow Jul 07 '13 at 14:01
  • @Rob W See my other comment above. For message passing one would just pass JSON.stringify(obj) so that the blob would be created at the receiving end, right? – stackunderflow Jul 07 '13 at 14:08
  • 1
    @stackunderflow No The channel itself is already JSON-serialized, so the manual JSON-serialization is unnecessary. Typed arrays are converted to object literals, not arrays, so you should be prepared to convert it back though. – Rob W Jul 07 '13 at 14:13
  • Now I am lost. I am not JSON-serializing anything. I create a blob and as a message, I pass just the URL and on the other end, I download it again by XHR. So I am not JSON-ing anything, I just pass URL. – Karel Bílek Jul 07 '13 at 17:40
  • Yeah, I'm consfused too. Please flesh out the question a bit better with actual code fragments, then we will all be on the same page. See, e.g. http://stackoverflow.com/questions/6969403/cant-get-execcommandpaste-to-work-in-chrome/15625874#15625874 – stackunderflow Jul 07 '13 at 18:11
  • URL.createObjectURL() is prohibited in service workers in extension manifest v3. Refer to this article for latest answer: https://stackoverflow.com/questions/8593896/ – Matthew C Nov 21 '22 at 16:38