Sending Blob through `runtime.sendMessage` is working, but why?

Hello,

I’m now refactoring my add-on from base64 images to blobs and I noticed some strange behavior - in Chrome - where sending blobs through internal messaging doesn’t work (blob gets converted to empty object).

Then I checked the MDN:

Which says that the message is:

An object that can be serialized to JSON.

I’m pretty sure blob cannot be serialized to JSON.

I’ve tried to find a W3C spec on this topic but could find any details:
https://browserext.github.io/browserext/#dom-browserextbrowserruntime-sendmessage

So who is doing this right? What is the expected behavior?

I wonder if Firefox is passing structured clones: https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm.

1 Like

Blobs certainly can’t be represented directly in JSON.

I hope that neither browser actually used JSON, as it is a rather indifferent format.
Firefox may be using something that encodes blobs, which is nice, but should be documented (in the compatibility table, I guess).

In other browsers you can either use base64 or URL.createObhectURL and fetch().blob().
But the latter probably doesn’t work when sending from content scripts and, depending on how you convert to base64, will only be more efficient from a certain size onwards.

I’m pretty sure that’s what Firefox uses for sendMessage.

Well the spec actually defines the message type as “any”:
https://heycam.github.io/webidl/#idl-any

The any type is the union of all other possible non-union types.

So I guess Blob is fine?

And there is also 6 years old request for this feature in Chrome (using the Structured clone):
https://bugs.chromium.org/p/chromium/issues/detail?id=112163

…what a superior browser this Firefox :smiley:, I love it even more now!

1 Like