Sample: Transfer a blob from a background context to a page
See original GitHub issueGoal of the demo
Demonstrate how an extension can create a blob in the extension’s service worker and transfer it to a page’s main world.
Suggested implementation
Sketch of my initial implementation plan
- Have a content script inject an iframe into a page
- Run a script in the iframe to post a message back to the service worker using
navigator.serviceWorker.controller.postMessage()
- In the SW’s message handler, generate a blob OR use
structuredClone()
to create a copy of a blob. - Use
event.source.postMessage(msg, [transferable])
to reply to the client and transfer the blob - In the iframe’s message handler, use
window.parent.postMessage(msg, "<origin>", [transferable])
to transfer the data to the page
Related links
Notes
Initial findings suggest that it’s not possible to transfer variables across origins. I’m tentatively thinking that to work around this, we can use URL.createObjectURL()
in the iframe and directly reference the object URL somewhere that’s easily visible to the end user (e.g. canvas, Audio/Video element, etc.).
Issue Analytics
- State:
- Created a year ago
- Reactions:3
- Comments:113 (2 by maintainers)
Top Results From Across the Web
How to transfer blobs from a Chrome Extension (with Gotchas)
On click of “Upload”, a blob is generated on the background.js file. We need to pass this blob to the web app after...
Read more >Azure Quickstart - Create a blob in object storage using Go
This command clones the repository to your local git folder. To open the Go sample for Blob storage, look for storage-quickstart.go file.
Read more >Blobs! - CSS-Tricks
I was recently a guest editor for an issue of Bizarro Devs. It's a great newsletter! Go sign up! I put in a...
Read more >Pass large blob or file from chrome extension - Stack Overflow
After creating the blob: -URL on the background page and passing it to the content script, don't forward it to the web page....
Read more >HTMLCanvasElement.toBlob() - Web APIs | MDN
The HTMLCanvasElement.toBlob() method creates a Blob object representing the image contained in the canvas. This file may be cached on the ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Yes they can be, if we are talking about from
chrome://<id>
to arbitrary Web page, and vice versa.Ideally
chrome
messaging should use the standard web platform messaging mechanism under the hood. The linked issue also mentions adding atransfer
parameter to enable what the web platform’s “transferrable” parameter does - at least in the non-broadcast methods i.e. ports.