@open-iframe-resizer/core 1.6.1 → 2.1.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/README.md CHANGED
@@ -17,7 +17,7 @@ If you found this project helpful, please consider starring the repository!
17
17
  ```html
18
18
 
19
19
  <script type="module">
20
- import { initialize } from "https://cdn.jsdelivr.net/npm/@open-iframe-resizer/core@latest/dist/index.min.js";
20
+ import { initialize } from "https://cdn.jsdelivr.net/npm/@open-iframe-resizer/core@v2.1.0/dist/index.min.js";
21
21
 
22
22
  initialize({}, "#my-iframe");
23
23
  </script>
package/dist/index.d.ts CHANGED
@@ -2,6 +2,7 @@ export declare type BeforeResizeContext = {
2
2
  iframe: HTMLIFrameElement;
3
3
  settings: Settings;
4
4
  observedHeight: number;
5
+ interactionState: InteractionState;
5
6
  };
6
7
 
7
8
  export declare type IframeChildInitEventData = {
@@ -11,6 +12,10 @@ export declare type IframeChildInitEventData = {
11
12
  bodyMargin?: string;
12
13
  };
13
14
 
15
+ export declare type IframeGetChildDimensionsEventData = {
16
+ type: "iframe-get-child-dimensions";
17
+ };
18
+
14
19
  export declare type IframeResizeEvent = MessageEvent<IframeResizeEventData>;
15
20
 
16
21
  export declare type IframeResizeEventData = {
@@ -27,11 +32,11 @@ export declare function initializeChildListener(): void;
27
32
  * Automatically resize the selected iframes when their inner content grows.
28
33
  * @param settings The settings for the selected iframes. The default settings properties are picked if empty.
29
34
  * @param selector The selector for the iframe(s) or the HTMLIFrameElement to be resized. If empty, all document iframe elements will be selected.
30
- * @returns A result array, which can be used to clean up the listeners if you often remove iframes from the document.
35
+ * @returns A result array Promise, which can be used to clean up the listeners if you remove iframes from the document and want to clean all associated listeners.
31
36
  */
32
- export declare type InitializeFunction = (settings?: Partial<Settings>, selector?: string | HTMLIFrameElement) => InitializeResult[];
37
+ export declare type InitializeFunction = (settings?: Partial<Settings>, selector?: string | HTMLIFrameElement) => Promise<InitializeResult[]>;
33
38
 
34
- export declare type InitializeResult = { unsubscribe: () => void };
39
+ export declare type InitializeResult = { unsubscribe: () => void; resize: () => void };
35
40
 
36
41
  export declare type InteractionState = {
37
42
  isHovered: boolean;
package/dist/index.js CHANGED
@@ -1,17 +1,27 @@
1
- const v = () => typeof window < "u", S = () => window.self !== window.top, y = (e) => e instanceof HTMLIFrameElement, E = (e) => {
2
- window.document.readyState === "complete" ? e() : window.addEventListener("load", e);
3
- }, M = (e, t) => {
4
- t(), e.addEventListener("load", t);
5
- }, T = (e, t) => {
1
+ const v = () => typeof window < "u", C = () => {
2
+ try {
3
+ return window.self !== window.top;
4
+ } catch {
5
+ return !0;
6
+ }
7
+ }, w = (e) => e instanceof HTMLIFrameElement, z = (e) => {
8
+ window.document.readyState === "complete" ? e() : window.addEventListener("load", e, { once: !0 });
9
+ }, B = (e, t) => {
10
+ t(), e.addEventListener("load", t, { once: !0 });
11
+ }, k = (e, t) => {
6
12
  const n = e.contentWindow?.document.readyState === "complete";
7
- return e.src !== "about:blank" && e.contentWindow?.location.href !== "about:blank" && n ? t() : e.addEventListener("load", t);
8
- }, C = () => ({ offsetSize: 0, checkOrigin: !0, enableLegacyLibSupport: !1 }), B = (e) => {
13
+ return e.src !== "about:blank" && e.contentWindow?.location.href !== "about:blank" && n ? t() : e.addEventListener("load", t, { once: !0 });
14
+ }, W = () => ({ offsetSize: 0, checkOrigin: !0, enableLegacyLibSupport: !1 });
15
+ async function A(e) {
9
16
  try {
10
- return new URL(e.src).origin === window.location.origin;
17
+ return e.contentDocument?.URL === "about:blank" ? new Promise((n) => {
18
+ e.addEventListener("load", () => n(e.contentDocument !== null), { once: !0 });
19
+ }) : e.contentDocument !== null;
11
20
  } catch {
12
21
  return !1;
13
22
  }
14
- }, k = (e) => {
23
+ }
24
+ const P = (e) => {
15
25
  try {
16
26
  const t = new URL(e.src).origin;
17
27
  if (t !== "about:blank")
@@ -19,182 +29,208 @@ const v = () => typeof window < "u", S = () => window.self !== window.top, y = (
19
29
  } catch {
20
30
  }
21
31
  return null;
22
- }, D = (e) => (Object.keys(e).forEach((t) => {
32
+ }, H = (e) => (Object.keys(e).forEach((t) => {
23
33
  e[t] === void 0 && delete e[t];
24
- }), e), p = (e) => {
34
+ }), e), I = (e) => {
25
35
  const { height: t, width: n } = e.getBoundingClientRect();
26
36
  return { height: Math.ceil(t), width: Math.ceil(n) };
27
- }, f = (e, t) => e ? t ? e.querySelector(t) : e.documentElement : null, L = (e, t) => {
37
+ }, l = (e, t) => e ? t ? e.querySelector(t) : e.documentElement : null, O = (e, t) => {
28
38
  e && (t.bodyPadding && (e.body.style.padding = t.bodyPadding), t.bodyMargin && (e.body.style.margin = t.bodyMargin));
29
- }, m = (e) => e <= 100 ? 100 : e <= 120 ? 1e3 : 1e4, W = () => "[iFrameSizer]ID:0:false:false:32:true:true::auto:::0:false:child:auto:true:::true:::false";
30
- function A(e) {
39
+ }, b = (e) => e <= 100 ? 100 : e <= 120 ? 1e3 : 1e4, x = () => "[iFrameSizer]ID:0:false:false:32:true:true::auto:::0:false:child:auto:true:::true:::false";
40
+ function F(e) {
31
41
  if (typeof e.data != "string" || !e.data.startsWith("[iFrameSizer]") || !e.data.endsWith("mutationObserver") && !e.data.endsWith("resizeObserver"))
32
42
  return null;
33
43
  const [t, n] = e.data.split(":"), i = +n;
34
44
  return i > 0 ? i : null;
35
45
  }
36
- const b = _();
37
- let l = [];
38
- const J = (e, t) => {
46
+ const p = V();
47
+ let m = [];
48
+ const Z = async (e, t) => {
39
49
  if (!v())
40
50
  return [];
41
- const n = { ...C(), ...D(e ?? {}) }, i = H(t), s = P(n, i);
42
- return i.map((r) => {
43
- const o = {
44
- iframe: r,
45
- settings: n,
46
- interactionState: { isHovered: !1 },
47
- initContext: { isInitialized: !1, retryAttempts: 0 }
48
- }, a = x(o, s);
49
- return l.push(o), {
50
- unsubscribe: () => {
51
- a(), l = l.filter((c) => c.iframe !== r);
52
- }
53
- };
54
- });
51
+ const n = { ...W(), ...H(e ?? {}) }, i = N(t), r = U(n, i);
52
+ return Promise.all(
53
+ i.map(async (s) => {
54
+ const o = {
55
+ iframe: s,
56
+ settings: n,
57
+ interactionState: { isHovered: !1 },
58
+ initContext: { isInitialized: !1, retryAttempts: 0 }
59
+ }, { unsubscribe: c, resize: u } = await $(o, r);
60
+ return m.push(o), {
61
+ unsubscribe: () => {
62
+ c(), m = m.filter((f) => f.iframe !== s);
63
+ },
64
+ resize: u
65
+ };
66
+ })
67
+ );
55
68
  };
56
- function H(e) {
57
- return typeof e == "string" ? Array.from(document.querySelectorAll(e)).filter(y) : e ? y(e) ? [e] : [] : Array.from(document.getElementsByTagName("iframe"));
69
+ function N(e) {
70
+ return typeof e == "string" ? Array.from(document.querySelectorAll(e)).filter(w) : e ? w(e) ? [e] : [] : Array.from(document.getElementsByTagName("iframe"));
58
71
  }
59
- function P(e, t) {
72
+ function U(e, t) {
60
73
  if (Array.isArray(e.checkOrigin))
61
74
  return e.checkOrigin;
62
75
  if (!e.checkOrigin)
63
76
  return [];
64
77
  const n = [];
65
78
  for (const i of t) {
66
- const s = k(i);
67
- s && n.push(s);
79
+ const r = P(i);
80
+ r && n.push(r);
68
81
  }
69
82
  return n;
70
83
  }
71
- function x(e, t) {
72
- const n = B(e.iframe) ? N(e) : F(e, t), i = U(e);
73
- return () => {
74
- n(), i();
84
+ async function $(e, t) {
85
+ const n = await A(e.iframe), { unsubscribe: i, resize: r } = n ? _(e) : q(e, t), s = G(e);
86
+ return {
87
+ unsubscribe: () => {
88
+ i(), s();
89
+ },
90
+ resize: r
75
91
  };
76
92
  }
77
- function F(e, t) {
93
+ function q(e, t) {
78
94
  const {
79
95
  iframe: n,
80
96
  initContext: i,
81
- settings: { checkOrigin: s, enableLegacyLibSupport: r, targetElementSelector: o, bodyPadding: a, bodyMargin: c }
82
- } = e, h = (d) => {
83
- const R = !s || t.includes(d.origin);
84
- if (!(!(n.contentWindow === d.source) || !R)) {
85
- if (d.data?.type === "iframe-resized") {
86
- const { height: u } = d.data;
87
- u && g({ newHeight: u, registeredElement: e });
97
+ settings: { checkOrigin: r, enableLegacyLibSupport: s, targetElementSelector: o, bodyPadding: c, bodyMargin: u }
98
+ } = e, f = (a) => {
99
+ const T = a.origin === "null", D = !r || T || t.includes(a.origin);
100
+ if (!(!(n.contentWindow === a.source) || !D)) {
101
+ if (a.data?.type === "iframe-resized") {
102
+ const { height: d } = a.data;
103
+ d && g({ newHeight: d, registeredElement: e });
88
104
  return;
89
105
  }
90
- if (r) {
91
- const u = A(d);
92
- u !== null && g({ newHeight: u, registeredElement: e });
106
+ if (s) {
107
+ const d = F(a);
108
+ d !== null && g({ newHeight: d, registeredElement: e });
93
109
  return;
94
110
  }
95
111
  }
96
112
  };
97
- window.addEventListener("message", h);
98
- const O = r ? W() : { type: "iframe-child-init", targetElementSelector: o, bodyPadding: a, bodyMargin: c }, w = () => {
99
- M(n, () => n.contentWindow?.postMessage(O, "*")), i.retryAttempts++, i.retryTimeoutId = window.setTimeout(w, m(i.retryAttempts));
113
+ window.addEventListener("message", f);
114
+ const M = s ? x() : { type: "iframe-child-init", targetElementSelector: o, bodyPadding: c, bodyMargin: u }, y = () => {
115
+ B(n, () => n.contentWindow?.postMessage(M, "*")), i.retryAttempts++, i.retryTimeoutId = window.setTimeout(y, b(i.retryAttempts));
116
+ };
117
+ return y(), {
118
+ unsubscribe: () => window.removeEventListener("message", f),
119
+ resize: () => {
120
+ const a = { type: "iframe-get-child-dimensions" };
121
+ n.contentWindow?.postMessage(a, "*");
122
+ }
100
123
  };
101
- return w(), () => window.removeEventListener("message", h);
102
124
  }
103
- function N(e) {
125
+ function _(e) {
104
126
  const { iframe: t, settings: n } = e, { targetElementSelector: i } = n;
105
- let s = 0;
106
- const r = () => {
107
- const o = f(t.contentDocument, i);
127
+ let r = 0;
128
+ const s = () => {
129
+ const o = l(t.contentDocument, i);
108
130
  if (!t.contentDocument || !o)
109
- return s++, setTimeout(r, m(s));
110
- L(t.contentDocument, n), b().observe(o);
131
+ return r++, setTimeout(s, b(r));
132
+ O(t.contentDocument, n), p().observe(o);
111
133
  };
112
- return T(t, r), () => {
113
- const o = f(t.contentDocument, i);
114
- o && b().unobserve(o), t.removeEventListener("load", r);
134
+ return k(t, s), {
135
+ unsubscribe: () => {
136
+ const o = l(t.contentDocument, i);
137
+ o && p().unobserve(o);
138
+ },
139
+ resize: () => L(e)
115
140
  };
116
141
  }
117
- function U({ iframe: e, interactionState: t }) {
118
- const n = () => {
142
+ function G({ iframe: e, interactionState: t, settings: n }) {
143
+ if (!n.onBeforeIframeResize && !n.onIframeResize)
144
+ return () => {
145
+ };
146
+ const i = () => {
119
147
  t.isHovered = !0;
120
- }, i = () => {
148
+ }, r = () => {
121
149
  t.isHovered = !1;
122
150
  };
123
- return e.addEventListener("mouseenter", n), e.addEventListener("mouseleave", i), () => {
124
- e.removeEventListener("mouseenter", n), e.removeEventListener("mouseleave", i);
151
+ return e.addEventListener("mouseenter", i), e.addEventListener("mouseleave", r), () => {
152
+ e.removeEventListener("mouseenter", i), e.removeEventListener("mouseleave", r);
125
153
  };
126
154
  }
127
- function _() {
155
+ function V() {
128
156
  let e = null;
129
157
  return () => {
130
158
  if (!e) {
131
159
  const t = ({ target: n }) => {
132
- const i = l.find(({ iframe: c }) => c.contentDocument === n.ownerDocument);
133
- if (!i)
134
- return;
135
- const { iframe: s, settings: r } = i, o = f(s.contentDocument, r.targetElementSelector);
136
- if (!o)
137
- return;
138
- const { height: a } = p(o);
139
- a && g({ newHeight: a, registeredElement: i });
160
+ const i = m.find(({ iframe: r }) => r.contentDocument === n.ownerDocument);
161
+ i && L(i);
140
162
  };
141
163
  e = new ResizeObserver((n) => n.forEach(t));
142
164
  }
143
165
  return e;
144
166
  };
145
167
  }
168
+ function L(e) {
169
+ const { iframe: t, settings: n } = e, i = l(t.contentDocument, n.targetElementSelector);
170
+ if (!i)
171
+ return;
172
+ const { height: r } = I(i);
173
+ r && g({ newHeight: r, registeredElement: e });
174
+ }
146
175
  function g({ registeredElement: e, newHeight: t }) {
147
- const { iframe: n, settings: i, interactionState: s, initContext: r } = e;
148
- if (r.isInitialized || (r.isInitialized = !0, clearTimeout(r.retryTimeoutId)), i.onBeforeIframeResize?.({ iframe: n, settings: { ...i }, observedHeight: t }) === !1)
176
+ const { iframe: n, settings: i, interactionState: r, initContext: s } = e;
177
+ if (s.isInitialized || (s.isInitialized = !0, clearTimeout(s.retryTimeoutId)), i.onBeforeIframeResize?.({ iframe: n, interactionState: { ...r }, settings: { ...i }, observedHeight: t }) === !1)
149
178
  return;
150
- const o = n.getBoundingClientRect(), a = t + i.offsetSize;
151
- if (n.style.height = `${a}px`, !i.onIframeResize)
179
+ const o = n.getBoundingClientRect(), c = t + i.offsetSize;
180
+ if (n.style.height = `${c}px`, !i.onIframeResize)
152
181
  return;
153
- const c = {
182
+ const u = {
154
183
  iframe: n,
155
184
  settings: { ...i },
156
- interactionState: { ...s },
185
+ interactionState: { ...r },
157
186
  previousRenderState: { rect: o },
158
187
  nextRenderState: { rect: n.getBoundingClientRect() }
159
188
  };
160
- i.onIframeResize(c);
189
+ i.onIframeResize(u);
161
190
  }
162
- const $ = V();
163
- let z = !1;
164
- q();
165
- function q() {
166
- !v() || !S() || window.addEventListener("message", (e) => {
167
- e.data?.type === "iframe-child-init" && E(() => I(e));
191
+ const J = X();
192
+ let h = !1, R;
193
+ K();
194
+ function K() {
195
+ !v() || !C() || window.addEventListener("message", (e) => {
196
+ if (e.data?.type === "iframe-child-init")
197
+ return z(() => S(e));
198
+ if (e.data?.type === "iframe-get-child-dimensions")
199
+ return z(() => Q(e));
168
200
  });
169
201
  }
170
- function I(e, t = 0) {
171
- const { targetElementSelector: n, bodyPadding: i, bodyMargin: s } = e.data, r = f(document, n);
172
- if (z || window.parent !== e.source)
202
+ function S(e, t = 0) {
203
+ const { targetElementSelector: n, bodyPadding: i, bodyMargin: r } = e.data, s = l(document, n);
204
+ if (h || window.parent !== e.source)
173
205
  return;
174
- if (!r)
175
- return setTimeout(() => I(e, t + 1), m(t));
176
- L(document, { bodyMargin: s, bodyPadding: i });
177
- const o = $();
178
- o.disconnect(), o.observe(r), z = !0;
206
+ if (!s)
207
+ return setTimeout(() => S(e, t + 1), b(t));
208
+ O(document, { bodyMargin: r, bodyPadding: i }), R = n;
209
+ const o = J();
210
+ o.disconnect(), o.observe(s), h = !0;
179
211
  }
180
- function V() {
212
+ function Q(e) {
213
+ const t = l(document, R);
214
+ !h || window.parent !== e.source || !t || E(t);
215
+ }
216
+ function X() {
181
217
  let e = null;
182
218
  return () => (e || (e = new ResizeObserver((t) => {
183
- if (!t[0].target)
184
- return;
185
- const { height: n, width: i } = p(t[0].target), s = {
186
- type: "iframe-resized",
187
- width: i,
188
- height: n
189
- };
190
- window.parent.postMessage(s, "*");
219
+ t[0].target && E(t[0].target);
191
220
  })), e);
192
221
  }
193
- const K = ({ previousRenderState: e, nextRenderState: t, iframe: n }) => {
222
+ const E = (e) => {
223
+ const { width: t, height: n } = I(e), i = {
224
+ type: "iframe-resized",
225
+ width: t,
226
+ height: n
227
+ };
228
+ window.parent.postMessage(i, "*");
229
+ }, j = ({ previousRenderState: e, nextRenderState: t, iframe: n }) => {
194
230
  document.activeElement === n && window.scrollBy(0, t.rect.bottom - e.rect.bottom);
195
231
  };
196
232
  export {
197
- J as initialize,
198
- q as initializeChildListener,
199
- K as updateParentScrollOnResize
233
+ Z as initialize,
234
+ K as initializeChildListener,
235
+ j as updateParentScrollOnResize
200
236
  };
@@ -1 +1 @@
1
- (function(a,u){typeof exports=="object"&&typeof module<"u"?u(exports):typeof define=="function"&&define.amd?define(["exports"],u):(a=typeof globalThis<"u"?globalThis:a||self,u(a.iframeResizer={}))})(this,function(a){"use strict";const u=()=>typeof window<"u",R=()=>window.self!==window.top,b=e=>e instanceof HTMLIFrameElement,E=e=>{window.document.readyState==="complete"?e():window.addEventListener("load",e)},T=(e,t)=>{t(),e.addEventListener("load",t)},M=(e,t)=>{const n=e.contentWindow?.document.readyState==="complete";return e.src!=="about:blank"&&e.contentWindow?.location.href!=="about:blank"&&n?t():e.addEventListener("load",t)},C=()=>({offsetSize:0,checkOrigin:!0,enableLegacyLibSupport:!1}),B=e=>{try{return new URL(e.src).origin===window.location.origin}catch{return!1}},k=e=>{try{const t=new URL(e.src).origin;if(t!=="about:blank")return t}catch{}return null},D=e=>(Object.keys(e).forEach(t=>{e[t]===void 0&&delete e[t]}),e),w=e=>{const{height:t,width:n}=e.getBoundingClientRect();return{height:Math.ceil(t),width:Math.ceil(n)}},g=(e,t)=>e?t?e.querySelector(t):e.documentElement:null,z=(e,t)=>{e&&(t.bodyPadding&&(e.body.style.padding=t.bodyPadding),t.bodyMargin&&(e.body.style.margin=t.bodyMargin))},h=e=>e<=100?100:e<=120?1e3:1e4,P=()=>"[iFrameSizer]ID:0:false:false:32:true:true::auto:::0:false:child:auto:true:::true:::false";function W(e){if(typeof e.data!="string"||!e.data.startsWith("[iFrameSizer]")||!e.data.endsWith("mutationObserver")&&!e.data.endsWith("resizeObserver"))return null;const[t,n]=e.data.split(":"),i=+n;return i>0?i:null}const p=$();let m=[];const A=(e,t)=>{if(!u())return[];const n={...C(),...D(e??{})},i=H(t),s=x(n,i);return i.map(r=>{const o={iframe:r,settings:n,interactionState:{isHovered:!1},initContext:{isInitialized:!1,retryAttempts:0}},c=F(o,s);return m.push(o),{unsubscribe:()=>{c(),m=m.filter(d=>d.iframe!==r)}}})};function H(e){return typeof e=="string"?Array.from(document.querySelectorAll(e)).filter(b):e?b(e)?[e]:[]:Array.from(document.getElementsByTagName("iframe"))}function x(e,t){if(Array.isArray(e.checkOrigin))return e.checkOrigin;if(!e.checkOrigin)return[];const n=[];for(const i of t){const s=k(i);s&&n.push(s)}return n}function F(e,t){const n=B(e.iframe)?U(e):N(e,t),i=_(e);return()=>{n(),i()}}function N(e,t){const{iframe:n,initContext:i,settings:{checkOrigin:s,enableLegacyLibSupport:r,targetElementSelector:o,bodyPadding:c,bodyMargin:d}}=e,I=l=>{const J=!s||t.includes(l.origin);if(!(!(n.contentWindow===l.source)||!J)){if(l.data?.type==="iframe-resized"){const{height:f}=l.data;f&&y({newHeight:f,registeredElement:e});return}if(r){const f=W(l);f!==null&&y({newHeight:f,registeredElement:e});return}}};window.addEventListener("message",I);const G=r?P():{type:"iframe-child-init",targetElementSelector:o,bodyPadding:c,bodyMargin:d},S=()=>{T(n,()=>n.contentWindow?.postMessage(G,"*")),i.retryAttempts++,i.retryTimeoutId=window.setTimeout(S,h(i.retryAttempts))};return S(),()=>window.removeEventListener("message",I)}function U(e){const{iframe:t,settings:n}=e,{targetElementSelector:i}=n;let s=0;const r=()=>{const o=g(t.contentDocument,i);if(!t.contentDocument||!o)return s++,setTimeout(r,h(s));z(t.contentDocument,n),p().observe(o)};return M(t,r),()=>{const o=g(t.contentDocument,i);o&&p().unobserve(o),t.removeEventListener("load",r)}}function _({iframe:e,interactionState:t}){const n=()=>{t.isHovered=!0},i=()=>{t.isHovered=!1};return e.addEventListener("mouseenter",n),e.addEventListener("mouseleave",i),()=>{e.removeEventListener("mouseenter",n),e.removeEventListener("mouseleave",i)}}function $(){let e=null;return()=>{if(!e){const t=({target:n})=>{const i=m.find(({iframe:d})=>d.contentDocument===n.ownerDocument);if(!i)return;const{iframe:s,settings:r}=i,o=g(s.contentDocument,r.targetElementSelector);if(!o)return;const{height:c}=w(o);c&&y({newHeight:c,registeredElement:i})};e=new ResizeObserver(n=>n.forEach(t))}return e}}function y({registeredElement:e,newHeight:t}){const{iframe:n,settings:i,interactionState:s,initContext:r}=e;if(r.isInitialized||(r.isInitialized=!0,clearTimeout(r.retryTimeoutId)),i.onBeforeIframeResize?.({iframe:n,settings:{...i},observedHeight:t})===!1)return;const o=n.getBoundingClientRect(),c=t+i.offsetSize;if(n.style.height=`${c}px`,!i.onIframeResize)return;const d={iframe:n,settings:{...i},interactionState:{...s},previousRenderState:{rect:o},nextRenderState:{rect:n.getBoundingClientRect()}};i.onIframeResize(d)}const q=V();let v=!1;L();function L(){!u()||!R()||window.addEventListener("message",e=>{e.data?.type==="iframe-child-init"&&E(()=>O(e))})}function O(e,t=0){const{targetElementSelector:n,bodyPadding:i,bodyMargin:s}=e.data,r=g(document,n);if(v||window.parent!==e.source)return;if(!r)return setTimeout(()=>O(e,t+1),h(t));z(document,{bodyMargin:s,bodyPadding:i});const o=q();o.disconnect(),o.observe(r),v=!0}function V(){let e=null;return()=>(e||(e=new ResizeObserver(t=>{if(!t[0].target)return;const{height:n,width:i}=w(t[0].target),s={type:"iframe-resized",width:i,height:n};window.parent.postMessage(s,"*")})),e)}const j=({previousRenderState:e,nextRenderState:t,iframe:n})=>{document.activeElement===n&&window.scrollBy(0,t.rect.bottom-e.rect.bottom)};a.initialize=A,a.initializeChildListener=L,a.updateParentScrollOnResize=j,Object.defineProperty(a,Symbol.toStringTag,{value:"Module"})});
1
+ (function(a,u){typeof exports=="object"&&typeof module<"u"?u(exports):typeof define=="function"&&define.amd?define(["exports"],u):(a=typeof globalThis<"u"?globalThis:a||self,u(a.iframeResizer={}))})(this,function(a){"use strict";const u=()=>typeof window<"u",C=()=>{try{return window.self!==window.top}catch{return!0}},w=e=>e instanceof HTMLIFrameElement,p=e=>{window.document.readyState==="complete"?e():window.addEventListener("load",e,{once:!0})},D=(e,t)=>{t(),e.addEventListener("load",t,{once:!0})},B=(e,t)=>{const n=e.contentWindow?.document.readyState==="complete";return e.src!=="about:blank"&&e.contentWindow?.location.href!=="about:blank"&&n?t():e.addEventListener("load",t,{once:!0})},k=()=>({offsetSize:0,checkOrigin:!0,enableLegacyLibSupport:!1});async function P(e){try{return e.contentDocument?.URL==="about:blank"?new Promise(n=>{e.addEventListener("load",()=>n(e.contentDocument!==null),{once:!0})}):e.contentDocument!==null}catch{return!1}}const W=e=>{try{const t=new URL(e.src).origin;if(t!=="about:blank")return t}catch{}return null},A=e=>(Object.keys(e).forEach(t=>{e[t]===void 0&&delete e[t]}),e),O=e=>{const{height:t,width:n}=e.getBoundingClientRect();return{height:Math.ceil(t),width:Math.ceil(n)}},d=(e,t)=>e?t?e.querySelector(t):e.documentElement:null,v=(e,t)=>{e&&(t.bodyPadding&&(e.body.style.padding=t.bodyPadding),t.bodyMargin&&(e.body.style.margin=t.bodyMargin))},b=e=>e<=100?100:e<=120?1e3:1e4,H=()=>"[iFrameSizer]ID:0:false:false:32:true:true::auto:::0:false:child:auto:true:::true:::false";function x(e){if(typeof e.data!="string"||!e.data.startsWith("[iFrameSizer]")||!e.data.endsWith("mutationObserver")&&!e.data.endsWith("resizeObserver"))return null;const[t,n]=e.data.split(":"),i=+n;return i>0?i:null}const I=V();let g=[];const F=async(e,t)=>{if(!u())return[];const n={...k(),...A(e??{})},i=N(t),r=U(n,i);return Promise.all(i.map(async s=>{const o={iframe:s,settings:n,interactionState:{isHovered:!1},initContext:{isInitialized:!1,retryAttempts:0}},{unsubscribe:l,resize:f}=await $(o,r);return g.push(o),{unsubscribe:()=>{l(),g=g.filter(h=>h.iframe!==s)},resize:f}}))};function N(e){return typeof e=="string"?Array.from(document.querySelectorAll(e)).filter(w):e?w(e)?[e]:[]:Array.from(document.getElementsByTagName("iframe"))}function U(e,t){if(Array.isArray(e.checkOrigin))return e.checkOrigin;if(!e.checkOrigin)return[];const n=[];for(const i of t){const r=W(i);r&&n.push(r)}return n}async function $(e,t){const n=await P(e.iframe),{unsubscribe:i,resize:r}=n?_(e):q(e,t),s=G(e);return{unsubscribe:()=>{i(),s()},resize:r}}function q(e,t){const{iframe:n,initContext:i,settings:{checkOrigin:r,enableLegacyLibSupport:s,targetElementSelector:o,bodyPadding:l,bodyMargin:f}}=e,h=c=>{const Y=c.origin==="null",Z=!r||Y||t.includes(c.origin);if(!(!(n.contentWindow===c.source)||!Z)){if(c.data?.type==="iframe-resized"){const{height:m}=c.data;m&&y({newHeight:m,registeredElement:e});return}if(s){const m=x(c);m!==null&&y({newHeight:m,registeredElement:e});return}}};window.addEventListener("message",h);const X=s?H():{type:"iframe-child-init",targetElementSelector:o,bodyPadding:l,bodyMargin:f},T=()=>{D(n,()=>n.contentWindow?.postMessage(X,"*")),i.retryAttempts++,i.retryTimeoutId=window.setTimeout(T,b(i.retryAttempts))};return T(),{unsubscribe:()=>window.removeEventListener("message",h),resize:()=>{const c={type:"iframe-get-child-dimensions"};n.contentWindow?.postMessage(c,"*")}}}function _(e){const{iframe:t,settings:n}=e,{targetElementSelector:i}=n;let r=0;const s=()=>{const o=d(t.contentDocument,i);if(!t.contentDocument||!o)return r++,setTimeout(s,b(r));v(t.contentDocument,n),I().observe(o)};return B(t,s),{unsubscribe:()=>{const o=d(t.contentDocument,i);o&&I().unobserve(o)},resize:()=>L(e)}}function G({iframe:e,interactionState:t,settings:n}){if(!n.onBeforeIframeResize&&!n.onIframeResize)return()=>{};const i=()=>{t.isHovered=!0},r=()=>{t.isHovered=!1};return e.addEventListener("mouseenter",i),e.addEventListener("mouseleave",r),()=>{e.removeEventListener("mouseenter",i),e.removeEventListener("mouseleave",r)}}function V(){let e=null;return()=>{if(!e){const t=({target:n})=>{const i=g.find(({iframe:r})=>r.contentDocument===n.ownerDocument);i&&L(i)};e=new ResizeObserver(n=>n.forEach(t))}return e}}function L(e){const{iframe:t,settings:n}=e,i=d(t.contentDocument,n.targetElementSelector);if(!i)return;const{height:r}=O(i);r&&y({newHeight:r,registeredElement:e})}function y({registeredElement:e,newHeight:t}){const{iframe:n,settings:i,interactionState:r,initContext:s}=e;if(s.isInitialized||(s.isInitialized=!0,clearTimeout(s.retryTimeoutId)),i.onBeforeIframeResize?.({iframe:n,interactionState:{...r},settings:{...i},observedHeight:t})===!1)return;const o=n.getBoundingClientRect(),l=t+i.offsetSize;if(n.style.height=`${l}px`,!i.onIframeResize)return;const f={iframe:n,settings:{...i},interactionState:{...r},previousRenderState:{rect:o},nextRenderState:{rect:n.getBoundingClientRect()}};i.onIframeResize(f)}const j=K();let z=!1,R;S();function S(){!u()||!C()||window.addEventListener("message",e=>{if(e.data?.type==="iframe-child-init")return p(()=>E(e));if(e.data?.type==="iframe-get-child-dimensions")return p(()=>J(e))})}function E(e,t=0){const{targetElementSelector:n,bodyPadding:i,bodyMargin:r}=e.data,s=d(document,n);if(z||window.parent!==e.source)return;if(!s)return setTimeout(()=>E(e,t+1),b(t));v(document,{bodyMargin:r,bodyPadding:i}),R=n;const o=j();o.disconnect(),o.observe(s),z=!0}function J(e){const t=d(document,R);!z||window.parent!==e.source||!t||M(t)}function K(){let e=null;return()=>(e||(e=new ResizeObserver(t=>{t[0].target&&M(t[0].target)})),e)}const M=e=>{const{width:t,height:n}=O(e),i={type:"iframe-resized",width:t,height:n};window.parent.postMessage(i,"*")},Q=({previousRenderState:e,nextRenderState:t,iframe:n})=>{document.activeElement===n&&window.scrollBy(0,t.rect.bottom-e.rect.bottom)};a.initialize=F,a.initializeChildListener=S,a.updateParentScrollOnResize=Q,Object.defineProperty(a,Symbol.toStringTag,{value:"Module"})});
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@open-iframe-resizer/core",
3
3
  "private": false,
4
- "version": "1.6.1",
4
+ "version": "2.1.0",
5
5
  "description": "Open-source modern iframe resizer",
6
6
  "license": "MIT",
7
7
  "repository": {