@open-iframe-resizer/core 1.0.0 → 1.1.1

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/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  ## Overview
4
4
 
5
- A modern, lightweight library for resizing iframes dynamically. It is shipped under the MIT license, making it usable in commercial projects.
5
+ A modern, lightweight alternative for resizing iframes dynamically. It is shipped under the MIT license, making it usable in commercial projects.
6
6
 
7
7
  If you found this plugin helpful, please consider starring the repository!
8
8
 
@@ -21,18 +21,33 @@ If you found this plugin helpful, please consider starring the repository!
21
21
  You can found a working example [here](https://codesandbox.io/p/sandbox/open-iframe-resize-browser-m655zt)
22
22
 
23
23
  ### Package
24
- Note you can also install the package through npm:
24
+ Note you can also install the core package through [npm](https://www.npmjs.com/package/@open-iframe-resizer/core):
25
25
  ```bash
26
26
  npm install @open-iframe-resizer/core
27
27
  ```
28
28
 
29
+ ### React
30
+ A React component is also available:
31
+ ```bash
32
+ npm install @open-iframe-resizer/react
33
+ ```
34
+
29
35
  ## Notes
30
36
 
31
- ### Retro-compatibility
32
- I decided to replicate the API from the well-known iframe-resizer library, as it may facilitate migration to this project (you only need to change the script).
37
+ ### Comparison with iframe-resizer
38
+ This library is very good, but it has changed its license, so it is no longer usable in closed-source projects for free.
39
+ I decided to replicate some parts of the API, as it may facilitate migration to this project.
40
+
41
+ Some features from this library are missing, but they could be implemented in future versions.
33
42
 
34
- ### Resize cross-origin iframes
35
- To resize iframes from a different origin, you also need to load the script on your child page to enable messaging between the two windows (you do not need to call the initialize function in the child; loading the module is sufficient).
43
+ ### Resize iframes from a different origin
44
+ - If you have control over the embedded page, you need to load the script on your child page to enable messaging between the two windows (you do not need to call the initialize function in the child; loading the module is sufficient).
45
+
46
+ - If you have no control over the child iframe domain, and, by chance, the child page loads the legacy *iframe-resizer* script, you can initialize the library with the compatibility mode; it will try to connect to the child iframe:
47
+
48
+ ```javascript
49
+ initialize({ enableLegacyLibSupport: true }, "#my-iframe");
50
+ ```
36
51
 
37
52
  ## Browser support
38
53
 
package/dist/common.d.ts CHANGED
@@ -1,11 +1,19 @@
1
1
  import { Settings } from './type';
2
- declare const isInIframe: () => boolean;
3
- declare const isSameOriginIframe: (iframe: HTMLIFrameElement) => iframe is HTMLIFrameElement & {
2
+ export declare const isInIframe: () => boolean;
3
+ export declare const isSameOriginIframe: (iframe: HTMLIFrameElement) => iframe is HTMLIFrameElement & {
4
4
  contentDocument: Document;
5
5
  };
6
- declare const isHtmlIframeElement: (element: Element) => element is HTMLIFrameElement;
7
- declare const deferWhenWindowIsLoaded: (_window: Window, executable: () => void) => void;
8
- declare const deferWhenIframeIsLoaded: (iframe: HTMLIFrameElement, executable: () => void) => void;
9
- declare const getDefaultSettings: () => Settings;
10
- declare const isIframeSameOrigin: (iframe: HTMLIFrameElement) => boolean;
11
- export { isInIframe, isSameOriginIframe, isHtmlIframeElement, deferWhenIframeIsLoaded, isIframeSameOrigin, deferWhenWindowIsLoaded, getDefaultSettings };
6
+ export declare const isHtmlIframeElement: (element: Element) => element is HTMLIFrameElement;
7
+ export declare const deferWhenWindowIsLoaded: (_window: Window, executable: () => void) => void;
8
+ /**
9
+ * Post the message twice, it assures the target to receive the message at least once
10
+ */
11
+ export declare const safePostMessageToCrossOriginIframe: (iframe: HTMLIFrameElement, executable: () => void) => void;
12
+ export declare const deferWhenSameOriginIframeIsLoaded: (iframe: HTMLIFrameElement, executable: () => void) => void;
13
+ export declare const getDefaultSettings: () => Settings;
14
+ export declare const isIframeSameOrigin: (iframe: HTMLIFrameElement) => boolean;
15
+ export declare const extractIframeOrigin: (iframe: HTMLIFrameElement) => string | null;
16
+ export declare const removeUndefinedProperties: <T extends {
17
+ [key: string]: unknown;
18
+ }>(object: T) => T;
19
+ export declare const getBoundingRectHeight: (document: Document) => number | undefined;
@@ -0,0 +1,2 @@
1
+ export declare function sendLegacyLibInitMessageOnIframeLoad(iframe: HTMLIFrameElement): void;
2
+ export declare function handleLegacyLibResizeMessage(event: MessageEvent): number | null;
package/dist/index.js CHANGED
@@ -1,64 +1,97 @@
1
- const g = () => window && window.self !== window.top, l = (e) => e instanceof HTMLIFrameElement, h = (e, n) => {
1
+ const h = () => window && window.self !== window.top, l = (e) => e instanceof HTMLIFrameElement, b = (e, n) => {
2
2
  e.document.readyState === "complete" ? n() : e.addEventListener("load", n);
3
3
  }, w = (e, n) => {
4
+ n(), e.addEventListener("load", n);
5
+ }, L = (e, n) => {
4
6
  var i, o;
5
7
  const t = ((i = e.contentWindow) == null ? void 0 : i.document.readyState) === "complete";
6
8
  return e.src !== "about:blank" && ((o = e.contentWindow) == null ? void 0 : o.location.href) !== "about:blank" && t ? n() : e.addEventListener("load", n);
7
- }, b = () => ({ offsetSize: 0, checkOrigin: !0 }), y = (e) => {
9
+ }, y = () => ({ offsetSize: 0, checkOrigin: !0, enableLegacyLibSupport: !1 }), p = (e) => {
8
10
  try {
9
11
  return new URL(e.src, window.location.origin).origin === window.location.origin;
10
12
  } catch {
11
13
  return !1;
12
14
  }
13
- }, u = E(), a = [], k = (e, n) => {
14
- const t = { ...b(), ...e }, r = O(n), i = v(t, r);
15
+ }, g = (e) => {
16
+ try {
17
+ const n = new URL(e.src).origin;
18
+ if (n !== "about:blank")
19
+ return n;
20
+ } catch {
21
+ }
22
+ return null;
23
+ }, O = (e) => (Object.keys(e).forEach((n) => e[n] === void 0 && delete e[n]), e), m = (e) => {
24
+ const { height: n } = e.documentElement.getBoundingClientRect() ?? {};
25
+ return n ? Math.ceil(n) : void 0;
26
+ };
27
+ function z(e) {
28
+ w(
29
+ e,
30
+ () => {
31
+ var n;
32
+ return (n = e.contentWindow) == null ? void 0 : n.postMessage(
33
+ "[iFrameSizer]ID:0:false:false:32:true:true::auto:::0:false:child:auto:true:::true:::false",
34
+ g(e) ?? "*"
35
+ );
36
+ }
37
+ );
38
+ }
39
+ function I(e) {
40
+ if (typeof e.data == "string" && e.data.startsWith("[iFrameSizer]")) {
41
+ const [n, t] = e.data.split(":");
42
+ return +t;
43
+ }
44
+ return null;
45
+ }
46
+ const f = C(), d = [], W = (e, n) => {
47
+ const t = { ...y(), ...O(e ?? {}) }, r = v(n), i = S(t, r);
15
48
  return r.map((o) => {
16
- const s = a.push({ iframe: o, settings: t }), c = L(o, t, i);
49
+ const a = d.push({ iframe: o, settings: t }), s = R(o, t, i);
17
50
  return {
18
51
  unsubscribe: () => {
19
- c(), a.splice(s - 1, 1);
52
+ s(), d.splice(a - 1, 1);
20
53
  }
21
54
  };
22
55
  });
23
56
  };
24
- function O(e) {
57
+ function v(e) {
25
58
  return typeof e == "string" ? Array.from(document.querySelectorAll(e)).filter(l) : e ? l(e) ? [e] : [] : Array.from(document.getElementsByTagName("iframe"));
26
59
  }
27
- function v(e, n) {
60
+ function S(e, n) {
28
61
  if (Array.isArray(e.checkOrigin))
29
62
  return e.checkOrigin;
30
63
  if (!e.checkOrigin)
31
64
  return [];
32
65
  const t = [];
33
66
  for (const r of n) {
34
- const i = z(r);
67
+ const i = g(r);
35
68
  i && t.push(i);
36
69
  }
37
70
  return t;
38
71
  }
39
- function z(e) {
40
- try {
41
- const n = new URL(e.src).origin;
42
- if (n !== "about:blank")
43
- return n;
44
- } catch {
45
- }
46
- return null;
47
- }
48
- function L(e, n, t) {
49
- return y(e) ? I(e) : p(e, n, t);
72
+ function R(e, n, t) {
73
+ return p(e) ? k(e) : E(e, n, t);
50
74
  }
51
- function p(e, n, t) {
75
+ function E(e, n, t) {
52
76
  const r = (i) => {
53
77
  var s;
54
- if ((!n.checkOrigin || t.includes(i.origin)) && ((s = i.data) == null ? void 0 : s.type) === "iframe-resized" && e.contentWindow === i.source) {
55
- const { height: c } = i.data;
56
- f({ height: c, iframe: e, settings: n });
78
+ const o = !n.checkOrigin || t.includes(i.origin);
79
+ if (!(!(e.contentWindow === i.source) || !o)) {
80
+ if (((s = i.data) == null ? void 0 : s.type) === "iframe-resized") {
81
+ const { height: c } = i.data;
82
+ c && u({ height: c, iframe: e, settings: n });
83
+ return;
84
+ }
85
+ if (n.enableLegacyLibSupport) {
86
+ const c = I(i);
87
+ c !== null && u({ height: c, iframe: e, settings: n });
88
+ return;
89
+ }
57
90
  }
58
91
  };
59
- return window.addEventListener("message", r, !1), () => window.removeEventListener("message", r, !1);
92
+ return window.addEventListener("message", r), n.enableLegacyLibSupport && z(e), () => window.removeEventListener("message", r);
60
93
  }
61
- function I(e) {
94
+ function k(e) {
62
95
  const n = () => {
63
96
  var r;
64
97
  const t = (r = e.contentDocument) == null ? void 0 : r.body;
@@ -66,38 +99,40 @@ function I(e) {
66
99
  console.error("Unable to observe the iframe content document body");
67
100
  return;
68
101
  }
69
- u.observe(t);
102
+ f.observe(t);
70
103
  };
71
- return w(e, n), () => {
104
+ return L(e, n), () => {
72
105
  var t;
73
- (t = e.contentDocument) != null && t.body && u.unobserve(e.contentDocument.body), e.removeEventListener("load", n);
106
+ (t = e.contentDocument) != null && t.body && f.unobserve(e.contentDocument.body), e.removeEventListener("load", n);
74
107
  };
75
108
  }
76
- function E() {
109
+ function C() {
77
110
  const e = ({ target: n }) => {
78
- var c;
79
- const t = a.find((m) => {
80
- var d;
81
- return ((d = m.iframe.contentDocument) == null ? void 0 : d.body) === n;
111
+ const t = d.find((a) => {
112
+ var s;
113
+ return ((s = a.iframe.contentDocument) == null ? void 0 : s.body) === n;
82
114
  });
83
115
  if (!t)
84
116
  return;
85
- const { iframe: r, settings: i } = t, { scrollHeight: o, scrollWidth: s } = ((c = r.contentDocument) == null ? void 0 : c.documentElement) ?? {};
86
- o !== void 0 && s !== void 0 && f({ height: o, iframe: r, settings: i });
117
+ const { iframe: r, settings: i } = t;
118
+ if (!r.contentDocument)
119
+ return;
120
+ const o = m(r.contentDocument);
121
+ o && u({ height: o, iframe: r, settings: i });
87
122
  };
88
123
  return new ResizeObserver((n) => n.forEach(e));
89
124
  }
90
- function f({ height: e, iframe: n, settings: t }) {
125
+ function u({ height: e, iframe: n, settings: t }) {
91
126
  n.style.height = `${e + t.offsetSize}px`;
92
127
  }
93
- g() && R();
94
- function R() {
95
- h(window, () => {
128
+ h() && D();
129
+ function D() {
130
+ b(window, () => {
96
131
  const e = () => {
97
132
  const t = {
98
133
  type: "iframe-resized",
99
134
  width: document.documentElement.scrollWidth,
100
- height: document.documentElement.scrollHeight
135
+ height: m(document) ?? void 0
101
136
  };
102
137
  window.parent.postMessage(t, "*");
103
138
  };
@@ -105,6 +140,6 @@ function R() {
105
140
  });
106
141
  }
107
142
  export {
108
- k as initialize,
109
- R as initializeChildListener
143
+ W as initialize,
144
+ D as initializeChildListener
110
145
  };
@@ -1 +1 @@
1
- (function(s,a){typeof exports=="object"&&typeof module<"u"?a(exports):typeof define=="function"&&define.amd?define(["exports"],a):(s=typeof globalThis<"u"?globalThis:s||self,a(s.iframeResizer={}))})(this,function(s){"use strict";const a=()=>window&&window.self!==window.top,l=e=>e instanceof HTMLIFrameElement,b=(e,n)=>{e.document.readyState==="complete"?n():e.addEventListener("load",n)},w=(e,n)=>{var r,o;const t=((r=e.contentWindow)==null?void 0:r.document.readyState)==="complete";return e.src!=="about:blank"&&((o=e.contentWindow)==null?void 0:o.location.href)!=="about:blank"&&t?n():e.addEventListener("load",n)},y=()=>({offsetSize:0,checkOrigin:!0}),O=e=>{try{return new URL(e.src,window.location.origin).origin===window.location.origin}catch{return!1}},f=S(),u=[],p=(e,n)=>{const t={...y(),...e},i=z(n),r=v(t,i);return i.map(o=>{const c=u.push({iframe:o,settings:t}),d=I(o,t,r);return{unsubscribe:()=>{d(),u.splice(c-1,1)}}})};function z(e){return typeof e=="string"?Array.from(document.querySelectorAll(e)).filter(l):e?l(e)?[e]:[]:Array.from(document.getElementsByTagName("iframe"))}function v(e,n){if(Array.isArray(e.checkOrigin))return e.checkOrigin;if(!e.checkOrigin)return[];const t=[];for(const i of n){const r=L(i);r&&t.push(r)}return t}function L(e){try{const n=new URL(e.src).origin;if(n!=="about:blank")return n}catch{}return null}function I(e,n,t){return O(e)?R(e):E(e,n,t)}function E(e,n,t){const i=r=>{var c;if((!n.checkOrigin||t.includes(r.origin))&&((c=r.data)==null?void 0:c.type)==="iframe-resized"&&e.contentWindow===r.source){const{height:d}=r.data;m({height:d,iframe:e,settings:n})}};return window.addEventListener("message",i,!1),()=>window.removeEventListener("message",i,!1)}function R(e){const n=()=>{var i;const t=(i=e.contentDocument)==null?void 0:i.body;if(!t){console.error("Unable to observe the iframe content document body");return}f.observe(t)};return w(e,n),()=>{var t;(t=e.contentDocument)!=null&&t.body&&f.unobserve(e.contentDocument.body),e.removeEventListener("load",n)}}function S(){const e=({target:n})=>{var d;const t=u.find(k=>{var h;return((h=k.iframe.contentDocument)==null?void 0:h.body)===n});if(!t)return;const{iframe:i,settings:r}=t,{scrollHeight:o,scrollWidth:c}=((d=i.contentDocument)==null?void 0:d.documentElement)??{};o!==void 0&&c!==void 0&&m({height:o,iframe:i,settings:r})};return new ResizeObserver(n=>n.forEach(e))}function m({height:e,iframe:n,settings:t}){n.style.height=`${e+t.offsetSize}px`}a()&&g();function g(){b(window,()=>{const e=()=>{const t={type:"iframe-resized",width:document.documentElement.scrollWidth,height:document.documentElement.scrollHeight};window.parent.postMessage(t,"*")};new ResizeObserver(e).observe(document.body)})}s.initialize=p,s.initializeChildListener=g,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"})});
1
+ (function(s,a){typeof exports=="object"&&typeof module<"u"?a(exports):typeof define=="function"&&define.amd?define(["exports"],a):(s=typeof globalThis<"u"?globalThis:s||self,a(s.iframeResizer={}))})(this,function(s){"use strict";const a=()=>window&&window.self!==window.top,g=e=>e instanceof HTMLIFrameElement,p=(e,n)=>{e.document.readyState==="complete"?n():e.addEventListener("load",n)},L=(e,n)=>{n(),e.addEventListener("load",n)},w=(e,n)=>{var r,o;const t=((r=e.contentWindow)==null?void 0:r.document.readyState)==="complete";return e.src!=="about:blank"&&((o=e.contentWindow)==null?void 0:o.location.href)!=="about:blank"&&t?n():e.addEventListener("load",n)},O=()=>({offsetSize:0,checkOrigin:!0,enableLegacyLibSupport:!1}),z=e=>{try{return new URL(e.src,window.location.origin).origin===window.location.origin}catch{return!1}},m=e=>{try{const n=new URL(e.src).origin;if(n!=="about:blank")return n}catch{}return null},I=e=>(Object.keys(e).forEach(n=>e[n]===void 0&&delete e[n]),e),h=e=>{const{height:n}=e.documentElement.getBoundingClientRect()??{};return n?Math.ceil(n):void 0};function v(e){L(e,()=>{var n;return(n=e.contentWindow)==null?void 0:n.postMessage("[iFrameSizer]ID:0:false:false:32:true:true::auto:::0:false:child:auto:true:::true:::false",m(e)??"*")})}function S(e){if(typeof e.data=="string"&&e.data.startsWith("[iFrameSizer]")){const[n,t]=e.data.split(":");return+t}return null}const b=T(),f=[],R=(e,n)=>{const t={...O(),...I(e??{})},i=E(n),r=k(t,i);return i.map(o=>{const u=f.push({iframe:o,settings:t}),c=C(o,t,r);return{unsubscribe:()=>{c(),f.splice(u-1,1)}}})};function E(e){return typeof e=="string"?Array.from(document.querySelectorAll(e)).filter(g):e?g(e)?[e]:[]:Array.from(document.getElementsByTagName("iframe"))}function k(e,n){if(Array.isArray(e.checkOrigin))return e.checkOrigin;if(!e.checkOrigin)return[];const t=[];for(const i of n){const r=m(i);r&&t.push(r)}return t}function C(e,n,t){return z(e)?M(e):D(e,n,t)}function D(e,n,t){const i=r=>{var c;const o=!n.checkOrigin||t.includes(r.origin);if(!(!(e.contentWindow===r.source)||!o)){if(((c=r.data)==null?void 0:c.type)==="iframe-resized"){const{height:d}=r.data;d&&l({height:d,iframe:e,settings:n});return}if(n.enableLegacyLibSupport){const d=S(r);d!==null&&l({height:d,iframe:e,settings:n});return}}};return window.addEventListener("message",i),n.enableLegacyLibSupport&&v(e),()=>window.removeEventListener("message",i)}function M(e){const n=()=>{var i;const t=(i=e.contentDocument)==null?void 0:i.body;if(!t){console.error("Unable to observe the iframe content document body");return}b.observe(t)};return w(e,n),()=>{var t;(t=e.contentDocument)!=null&&t.body&&b.unobserve(e.contentDocument.body),e.removeEventListener("load",n)}}function T(){const e=({target:n})=>{const t=f.find(u=>{var c;return((c=u.iframe.contentDocument)==null?void 0:c.body)===n});if(!t)return;const{iframe:i,settings:r}=t;if(!i.contentDocument)return;const o=h(i.contentDocument);o&&l({height:o,iframe:i,settings:r})};return new ResizeObserver(n=>n.forEach(e))}function l({height:e,iframe:n,settings:t}){n.style.height=`${e+t.offsetSize}px`}a()&&y();function y(){p(window,()=>{const e=()=>{const t={type:"iframe-resized",width:document.documentElement.scrollWidth,height:h(document)??void 0};window.parent.postMessage(t,"*")};new ResizeObserver(e).observe(document.body)})}s.initialize=R,s.initializeChildListener=y,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"})});
package/dist/type.d.ts CHANGED
@@ -14,14 +14,34 @@ export type InitializeFunction = (settings?: Partial<Settings>, selector?: strin
14
14
  export type InitializeResult = { unsubscribe: () => void };
15
15
 
16
16
  export type Settings = {
17
+ /**
18
+ * Offset added to the resize size (in pixels).
19
+ *
20
+ * Default: 0
21
+ */
17
22
  offsetSize: number;
23
+ /**
24
+ * Specifies whether to check the origin of incoming messages.
25
+ * Accepts an array of allowed origins or a boolean.
26
+ * If true, incoming messages are allowed from the origins of the registered iframes.
27
+ *
28
+ * Default: true
29
+ */
18
30
  checkOrigin: string[] | boolean;
31
+ /**
32
+ * Allows the library to communicate with a cross-origin child iframe
33
+ * containing the original "iframe-resizer" script.
34
+ * Useful if you do not control the child domain.
35
+ *
36
+ * Default: false
37
+ */
38
+ enableLegacyLibSupport: boolean;
19
39
  };
20
40
 
21
41
  export type IframeResizeEventData = {
22
42
  type: "iframe-resized";
23
43
  width: number;
24
- height: number;
44
+ height?: number;
25
45
  };
26
46
 
27
47
  export type IframeResizeEvent = MessageEvent<IframeResizeEventData>;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@open-iframe-resizer/core",
3
3
  "private": false,
4
- "version": "1.0.0",
4
+ "version": "1.1.1",
5
5
  "description": "Open-source modern iframe resizer",
6
6
  "license": "MIT",
7
7
  "repository": {
@@ -43,7 +43,6 @@
43
43
  "lint": "biome lint ./src",
44
44
  "build": "tsc && npm run lint && vite build && typedoc",
45
45
  "build:watch": "vite build --watch && typedoc",
46
- "build:usecases-html": "node scripts/generate-use-cases-html.js",
47
46
  "dev": "concurrently \"vite build --watch\" \"npm run serve\"",
48
47
  "serve": "concurrently \"vite --port 5550\" \"vite --port 5551\"",
49
48
  "test": "npm run test:unit && npm run test:e2e",