Skip to content

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

@dotproto

Description

@dotproto

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.).

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions