resize-iframe 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,25 @@
1
1
  # resize-iframe
2
2
 
3
+ ## 0.2.0
4
+
5
+ ### Minor Changes
6
+
7
+ - f2e12a7: Give the framed page a way to receive what the parent sends.
8
+
9
+ `parentIframe.onMessage = (message) => …` delivers whatever the embedder posted with `sendMessage`. Until now the channel was send-only: the parent could post into the frame, but the child script never listened, so every embedded page had to hand-roll a `message` listener and work out for itself which messages came from the embedder.
10
+
11
+ Both directions now travel in the same `resize-iframe-message` envelope, and the child checks `event.source === parent` the same way the parent checks the frame's own window. Unrelated postMessage traffic aimed at your frame no longer reaches the callback.
12
+
13
+ **Breaking for hand-rolled child listeners.** A child page reading `event.data` directly now sees `{ 'resize-iframe-message': … }` rather than the bare payload. Use `parentIframe.onMessage` instead.
14
+
15
+ ### Patch Changes
16
+
17
+ - f2e12a7: Document the third-party embedding flow and cover the library with a Playwright suite.
18
+
19
+ - README: Storage Access API setup for cross-site embeds, including the `allow="storage-access"` and `allow-storage-access-by-user-activation` requirements, and why the request has to come from a click inside the frame.
20
+ - README: note that `sendMessage` is dropped if the frame is still on `about:blank`, so send it after `ready`.
21
+ - 33 specs across Chromium, Firefox and WebKit, served from two origins so they run against real cross-site restrictions rather than same-document shortcuts.
22
+
3
23
  ## 0.1.1
4
24
 
5
25
  ### Patch Changes
package/README.md CHANGED
@@ -181,9 +181,15 @@ frame.sendMessage({ hello: 'world' }, 'https://anotherdomain.com');
181
181
  frame.disconnect(); // call before removing the iframe from the page
182
182
  ```
183
183
 
184
+ Both directions travel in the same envelope, so `parentIframe.onMessage` only ever
185
+ sees what the embedder sent — not the postMessage traffic every other script on the
186
+ page also aims at your frame. A plain `addEventListener('message', …)` in the child
187
+ would have to sort that out itself.
188
+
184
189
  Child, on `window.parentIframe`:
185
190
 
186
191
  ```javascript
192
+ parentIframe.onMessage = (message) => { … }; // ← parent's sendMessage
187
193
  parentIframe.sendMessage('ping'); // → parent's onMessage / 'frame-message' event
188
194
  parentIframe.autoResize(false); // pause resizing; returns the current state
189
195
  parentIframe.resize(); // nudge, for a change neither observer sees
@@ -227,21 +233,11 @@ pnpm exec playwright install
227
233
  pnpm test
228
234
  ```
229
235
 
230
- 33 Playwright specs across Chromium, Firefox and WebKit. The suite serves the
236
+ 34 Playwright specs across Chromium, Firefox and WebKit. The suite serves the
231
237
  parent page and the child pages from two different origins — `localhost` and
232
238
  `127.0.0.1`, which are cross-*site*, not merely cross-origin — so every test runs
233
239
  against the same partitioning and `contentDocument` restrictions as production.
234
240
 
235
- ## Testing
236
-
237
- ```bash
238
- pnpm install
239
- pnpm exec playwright install
240
- pnpm test
241
- ```
242
-
243
- 21 Playwright specs, run across Chromium, Firefox and WebKit.
244
-
245
241
  ## Browser Support
246
242
 
247
243
  - Chrome/Edge 80+
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "resize-iframe",
3
- "version": "0.1.1",
3
+ "version": "0.2.0",
4
4
  "description": "Iframes that size themselves to their content, cross-origin included. No build step, no dependencies.",
5
5
  "type": "module",
6
6
  "main": "resize-iframe.js",
@@ -12,6 +12,7 @@ const start = () => {
12
12
  let last = { height: 0, width: 0 };
13
13
  let queued = false;
14
14
  let auto = true;
15
+ let inbound = null;
15
16
 
16
17
  // Measure the bottom and right edges of the marked elements, or of body's
17
18
  // children. Never body itself: it stretches to fill the frame in quirks mode
@@ -64,6 +65,18 @@ const start = () => {
64
65
  });
65
66
  addEventListener('load', schedule); // images and fonts landing late
66
67
 
68
+ // Messages from the embedder. event.source is set by the browser and cannot be
69
+ // forged, so this is the same guard the parent uses in the other direction, and
70
+ // the envelope keeps unrelated postMessage traffic (analytics, wallets, other
71
+ // embeds) out of the callback.
72
+ addEventListener('message', (event) => {
73
+ if (event.source !== parent) return;
74
+ const data = event.data;
75
+ if (data && typeof data === 'object' && 'resize-iframe-message' in data) {
76
+ inbound?.(data['resize-iframe-message']);
77
+ }
78
+ });
79
+
67
80
  // Third-party cookies: an embedded page gets partitioned storage until the user
68
81
  // grants access. Probe the API rather than sniffing the browser — Safari and
69
82
  // Chrome both partition, and which browsers do is not a stable list.
@@ -77,6 +90,13 @@ const start = () => {
77
90
  };
78
91
 
79
92
  window.parentIframe = {
93
+ // Assign a function to receive what the parent sends with sendMessage().
94
+ get onMessage() {
95
+ return inbound;
96
+ },
97
+ set onMessage(fn) {
98
+ inbound = fn;
99
+ },
80
100
  autoResize(state) {
81
101
  if (state !== undefined) auto = state;
82
102
  if (auto) schedule();
@@ -32,6 +32,8 @@ export function iframeResize(
32
32
 
33
33
  /** Available inside the framed page once resize-iframe-child.js has loaded. */
34
34
  export interface ParentIframe {
35
+ /** Called with whatever the parent sends via `sendMessage`. Assign to subscribe. */
36
+ onMessage: ((message: unknown) => void) | null;
35
37
  autoResize(state?: boolean): boolean;
36
38
  resize(): void;
37
39
  sendMessage(message: unknown, targetOrigin?: string): void;
package/resize-iframe.js CHANGED
@@ -88,8 +88,10 @@ function connect(iframe, settings) {
88
88
  clearTimeout(warning);
89
89
  delete iframe.iframeResizer;
90
90
  },
91
+ // Same envelope the child sends back in, so the framed page can tell an
92
+ // embedder message from every other script posting at it.
91
93
  sendMessage(message, targetOrigin = '*') {
92
- iframe.contentWindow?.postMessage(message, targetOrigin);
94
+ iframe.contentWindow?.postMessage({ 'resize-iframe-message': message }, targetOrigin);
93
95
  },
94
96
  };
95
97
  return iframe.iframeResizer;