resize-iframe 0.1.0 → 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 ADDED
@@ -0,0 +1,31 @@
1
+ # resize-iframe
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
+
23
+ ## 0.1.1
24
+
25
+ ### Patch Changes
26
+
27
+ - d9fd60f: Document the third-party embedding flow and cover the library with a Playwright suite.
28
+
29
+ - 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.
30
+ - README: note that `sendMessage` is dropped if the frame is still on `about:blank`, so send it after `ready`.
31
+ - 33 specs across Chromium, Firefox and WebKit, served from two origins so they run against real cross-site restrictions rather than same-document shortcuts.
package/README.md CHANGED
@@ -175,13 +175,21 @@ turned off for.
175
175
  Parent, on the handle returned by `iframeResize` (also at `iframe.iframeResizer`):
176
176
 
177
177
  ```javascript
178
+ // Wait for ready first: a message posted at a frame still on about:blank is
179
+ // dropped, and nothing retries it.
178
180
  frame.sendMessage({ hello: 'world' }, 'https://anotherdomain.com');
179
181
  frame.disconnect(); // call before removing the iframe from the page
180
182
  ```
181
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
+
182
189
  Child, on `window.parentIframe`:
183
190
 
184
191
  ```javascript
192
+ parentIframe.onMessage = (message) => { … }; // ← parent's sendMessage
185
193
  parentIframe.sendMessage('ping'); // → parent's onMessage / 'frame-message' event
186
194
  parentIframe.autoResize(false); // pause resizing; returns the current state
187
195
  parentIframe.resize(); // nudge, for a change neither observer sees
@@ -220,10 +228,15 @@ declare module 'react' {
220
228
  ## Testing
221
229
 
222
230
  ```bash
223
- npx serve
231
+ pnpm install
232
+ pnpm exec playwright install
233
+ pnpm test
224
234
  ```
225
235
 
226
- Open `resize-iframe-test.html`; it prints ten PASS lines.
236
+ 34 Playwright specs across Chromium, Firefox and WebKit. The suite serves the
237
+ parent page and the child pages from two different origins — `localhost` and
238
+ `127.0.0.1`, which are cross-*site*, not merely cross-origin — so every test runs
239
+ against the same partitioning and `contentDocument` restrictions as production.
227
240
 
228
241
  ## Browser Support
229
242
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "resize-iframe",
3
- "version": "0.1.0",
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",
@@ -9,8 +9,13 @@
9
9
  "files": [
10
10
  "resize-iframe.js",
11
11
  "resize-iframe-child.js",
12
- "resize-iframe.d.ts"
12
+ "resize-iframe.d.ts",
13
+ "CHANGELOG.md"
13
14
  ],
15
+ "publishConfig": {
16
+ "access": "public",
17
+ "provenance": true
18
+ },
14
19
  "keywords": [
15
20
  "iframe",
16
21
  "iframe-resizer",
@@ -23,8 +28,24 @@
23
28
  ],
24
29
  "repository": {
25
30
  "type": "git",
26
- "url": "https://github.com/jagreehal/resize-iframe"
31
+ "url": "git+https://github.com/jagreehal/resize-iframe.git"
27
32
  },
33
+ "homepage": "https://github.com/jagreehal/resize-iframe#readme",
34
+ "bugs": "https://github.com/jagreehal/resize-iframe/issues",
28
35
  "author": "Jag Reehal <jag@jagreehal.com>",
29
- "license": "MIT"
36
+ "license": "MIT",
37
+ "devDependencies": {
38
+ "@changesets/cli": "2.31.0",
39
+ "@playwright/test": "1.60.0",
40
+ "serve": "14.2.6",
41
+ "typescript": "6.0.3"
42
+ },
43
+ "scripts": {
44
+ "test": "playwright test",
45
+ "test:ui": "playwright test --ui",
46
+ "typecheck": "tsc --noEmit --strict --lib es2022,dom resize-iframe.d.ts",
47
+ "changeset": "changeset",
48
+ "version-packages": "changeset version",
49
+ "release": "changeset publish"
50
+ }
30
51
  }
@@ -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;