Reproject DOM into another tab
The widgets below are DOM, slotted into a canvas on this page. Open a viewer and each named slot
is streamed to it over a MessagePort. Click and type in the viewer: the events come back over
the same port and are replayed on the elements here.
…or open the viewer in a new tab
0 viewers
slot="counter"
slot="clock"
Show the code a slot in another tab
- This page: DOM, slotted into a canvas. Each
slotname is something a viewer can ask for. - This page: draw the slots, publish them, and give each viewer a port. A popup is handed a
MessagePortdirectly. An unrelated tab gets one through a SharedWorker. - The other tab: one
<slot-mirror>per slot. - The other tab: get the port and attach the mirrors to it.
- What
publish()does: cut each drawn element out of the canvas and transfer it. AfterdrawElementImage()the element's client rect says where it sits in the canvas, socreateImageBitmap(canvas, x, y, w, h)gets exactly that element. - What
publish()does with input: replay it on the real element. The viewer sends element-local coordinates and keys. - The SharedWorker that introduces two tabs.
- The whole library, including
<slot-mirror>.
How it travels
- Introductions. A popup gets a port directly:
popup.postMessage({…}, '*', [port2]). An unrelated tab has no window reference, so both tabs connect to aSharedWorkerthat creates aMessageChanneland hands one end to each. After that they talk port to port. - Frames. On every
paintevent the publisher draws the slotted elements, then cuts each element's region out withcreateImageBitmap(canvas, …)and transfers it. The element's client rect gives the region, becausedrawElementImage()moved its geometry to where it was drawn. - Input. The viewer sends element-local pointer coordinates and keys. The publisher maps them with
elementFromPoint(), focuses, clicks, and edits inputs withsetRangeText().
What can cross a tab boundary (live)
| Result | Experiment | Detail |
|---|
Findings
- The snapshot can't follow the DOM to another tab.
ElementImageis transferable, sopostMessageto a popup succeeds, but the popup can't draw it: an ElementImage can only be drawn into the canvas it was captured from (or that canvas's OffscreenCanvas). BroadcastChannelcan't carry it. ElementImage is transferable but not serializable, sostructuredCloneand BroadcastChannel throwDataCloneError.- Pixels can. Because HTML-in-canvas strips sensitive content when painting, the canvas isn't tainted and an
ImageBitmapof the drawn DOM can go anywhere: MessagePort, BroadcastChannel, popups. - So showing an element in another tab means sending frames and forwarding input. The viewer updates and takes input, but text in the viewer isn't selectable or accessible; the semantics stay in the source tab.
- The source tab has to be rendering. The
paintevent runs in the rendering loop, so a hidden or backgrounded source tab stops sending frames. Side-by-side windows (or a popup) work best.