Across origins
This page and the iframe below are on different origins ( and ).
I tried it in both directions: showing the other origin's DOM here as pixels, and giving this page's DOM to the other origin to draw.
A · Their DOM, shown here
The iframe publishes its slot over a MessagePort that this page posted to it. The right-hand view is an <slot-mirror> in this document; click the heart or the input there and type.
cross-origin iframe (the DOM)
<slot-mirror name="widget"> in this page
Show the code their DOM, shown here
- This page: an iframe on the other origin and a
<slot-mirror>. - This page: post the iframe one end of a
MessageChannel, attach the mirror to the other.REMOTEis the other origin. - The other origin: its own DOM in its own canvas.
- The other origin: publish the slot on the port it was given. It sends pixels of the drawn element and replays the input that comes back. See Across tabs for what
publish()does inside.
B · This page's DOM, drawn by them
The card below is this page's DOM. Its canvas was transferred to the iframe with transferControlToOffscreen(), and on every paint this page transfers an ElementImage of the card. The other origin does all the drawing (the wave is theirs) and never touches the DOM. The button works.
Show the code this page's DOM, drawn by them
- This page: a drawable canvas with the card inside it.
- This page: give the other origin the canvas, then a snapshot on every paint.
transferControlToOffscreen()andcaptureElementImage()both produce transferable objects, andpostMessagecarries them across origins.frameis the iframe andREMOTEits origin. - The other origin: keep the canvas and the latest snapshot. This is inside its
messagelistener;mis the message. - The other origin: draw the snapshot however it likes. One plain
drawElementImage()at the end, fully transparent, sets where clicks land back on this page.
What crosses an origin boundary (live)
| Result | Experiment | Detail |
|---|
Findings
- ElementImage and OffscreenCanvas both transfer across origins with
window.postMessageand MessagePorts. - A cross-origin document can render your DOM if you give it both your canvas (as an OffscreenCanvas) and the snapshots. The pair must match: the snapshot only draws into the canvas it was captured from.
- Lending a snapshot means trusting the other origin. The receiving origin can call
getImageData()on what it drew, so it gets the pixels of that element. Only content that is already "read-back allowed" is in there, but it's still your content. - It can't go the other way directly. A cross-origin iframe placed inside a drawable canvas is painted blank (same-origin iframes paint). To show another origin's DOM, that origin has to cooperate and send pixels, as in part A.
- Hit testing still works. When the other origin draws the snapshot, the element's geometry is updated on this page, so clicks reach the button.