question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Sample: Transfer a blob from a background context to a page

See original GitHub issue

Goal 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

  1. Have a content script inject an iframe into a page
  2. Run a script in the iframe to post a message back to the service worker using navigator.serviceWorker.controller.postMessage()
  3. In the SW’s message handler, generate a blob OR use structuredClone() to create a copy of a blob.
  4. Use event.source.postMessage(msg, [transferable]) to reply to the client and transfer the blob
  5. 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:open
  • Created a year ago
  • Reactions:3
  • Comments:113 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
guest271314commented, Nov 1, 2022

my initial experiments seem to indicate that transferables cannot be sent across origin

Yes they can be, if we are talking about from chrome://<id> to arbitrary Web page, and vice versa.

1reaction
tophfcommented, Nov 6, 2022

Ideally chrome messaging should use the standard web platform messaging mechanism under the hood. The linked issue also mentions adding a transfer parameter to enable what the web platform’s “transferrable” parameter does - at least in the non-broadcast methods i.e. ports.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found