@open-iframe-resizer/core 2.0.0 → 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@v2.0.0/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 = {
@@ -31,7 +36,7 @@ export declare function initializeChildListener(): void;
31
36
  */
32
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,18 +1,18 @@
1
- const z = () => typeof window < "u", R = () => {
1
+ const v = () => typeof window < "u", C = () => {
2
2
  try {
3
3
  return window.self !== window.top;
4
4
  } catch {
5
5
  return !0;
6
6
  }
7
- }, b = (e) => e instanceof HTMLIFrameElement, M = (e) => {
7
+ }, w = (e) => e instanceof HTMLIFrameElement, z = (e) => {
8
8
  window.document.readyState === "complete" ? e() : window.addEventListener("load", e, { once: !0 });
9
- }, T = (e, t) => {
9
+ }, B = (e, t) => {
10
10
  t(), e.addEventListener("load", t, { once: !0 });
11
- }, C = (e, t) => {
11
+ }, k = (e, t) => {
12
12
  const n = e.contentWindow?.document.readyState === "complete";
13
13
  return e.src !== "about:blank" && e.contentWindow?.location.href !== "about:blank" && n ? t() : e.addEventListener("load", t, { once: !0 });
14
- }, D = () => ({ offsetSize: 0, checkOrigin: !0, enableLegacyLibSupport: !1 });
15
- async function k(e) {
14
+ }, W = () => ({ offsetSize: 0, checkOrigin: !0, enableLegacyLibSupport: !1 });
15
+ async function A(e) {
16
16
  try {
17
17
  return e.contentDocument?.URL === "about:blank" ? new Promise((n) => {
18
18
  e.addEventListener("load", () => n(e.contentDocument !== null), { once: !0 });
@@ -21,7 +21,7 @@ async function k(e) {
21
21
  return !1;
22
22
  }
23
23
  }
24
- const B = (e) => {
24
+ const P = (e) => {
25
25
  try {
26
26
  const t = new URL(e.src).origin;
27
27
  if (t !== "about:blank")
@@ -29,184 +29,208 @@ const B = (e) => {
29
29
  } catch {
30
30
  }
31
31
  return null;
32
- }, P = (e) => (Object.keys(e).forEach((t) => {
32
+ }, H = (e) => (Object.keys(e).forEach((t) => {
33
33
  e[t] === void 0 && delete e[t];
34
- }), e), p = (e) => {
34
+ }), e), I = (e) => {
35
35
  const { height: t, width: n } = e.getBoundingClientRect();
36
36
  return { height: Math.ceil(t), width: Math.ceil(n) };
37
- }, 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) => {
38
38
  e && (t.bodyPadding && (e.body.style.padding = t.bodyPadding), t.bodyMargin && (e.body.style.margin = t.bodyMargin));
39
- }, 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";
40
- 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) {
41
41
  if (typeof e.data != "string" || !e.data.startsWith("[iFrameSizer]") || !e.data.endsWith("mutationObserver") && !e.data.endsWith("resizeObserver"))
42
42
  return null;
43
43
  const [t, n] = e.data.split(":"), i = +n;
44
44
  return i > 0 ? i : null;
45
45
  }
46
- const w = q();
47
- let l = [];
48
- const K = async (e, t) => {
49
- if (!z())
46
+ const p = V();
47
+ let m = [];
48
+ const Z = async (e, t) => {
49
+ if (!v())
50
50
  return [];
51
- const n = { ...D(), ...P(e ?? {}) }, i = H(t), r = x(n, i);
51
+ const n = { ...W(), ...H(e ?? {}) }, i = N(t), r = U(n, i);
52
52
  return Promise.all(
53
- i.map(async (o) => {
54
- const s = {
55
- iframe: o,
53
+ i.map(async (s) => {
54
+ const o = {
55
+ iframe: s,
56
56
  settings: n,
57
57
  interactionState: { isHovered: !1 },
58
58
  initContext: { isInitialized: !1, retryAttempts: 0 }
59
- }, a = await F(s, r);
60
- return l.push(s), {
59
+ }, { unsubscribe: c, resize: u } = await $(o, r);
60
+ return m.push(o), {
61
61
  unsubscribe: () => {
62
- a(), l = l.filter((c) => c.iframe !== o);
63
- }
62
+ c(), m = m.filter((f) => f.iframe !== s);
63
+ },
64
+ resize: u
64
65
  };
65
66
  })
66
67
  );
67
68
  };
68
- function H(e) {
69
- return typeof e == "string" ? Array.from(document.querySelectorAll(e)).filter(b) : e ? b(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"));
70
71
  }
71
- function x(e, t) {
72
+ function U(e, t) {
72
73
  if (Array.isArray(e.checkOrigin))
73
74
  return e.checkOrigin;
74
75
  if (!e.checkOrigin)
75
76
  return [];
76
77
  const n = [];
77
78
  for (const i of t) {
78
- const r = B(i);
79
+ const r = P(i);
79
80
  r && n.push(r);
80
81
  }
81
82
  return n;
82
83
  }
83
- async function F(e, t) {
84
- const i = await k(e.iframe) ? U(e) : N(e, t), r = $(e);
85
- return () => {
86
- i(), r();
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
87
91
  };
88
92
  }
89
- function N(e, t) {
93
+ function q(e, t) {
90
94
  const {
91
95
  iframe: n,
92
96
  initContext: i,
93
- settings: { checkOrigin: r, enableLegacyLibSupport: o, targetElementSelector: s, bodyPadding: a, bodyMargin: c }
94
- } = e, h = (u) => {
95
- const S = u.origin === "null", E = !r || S || t.includes(u.origin);
96
- if (!(!(n.contentWindow === u.source) || !E)) {
97
- if (u.data?.type === "iframe-resized") {
98
- const { height: d } = u.data;
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;
99
103
  d && g({ newHeight: d, registeredElement: e });
100
104
  return;
101
105
  }
102
- if (o) {
103
- const d = A(u);
106
+ if (s) {
107
+ const d = F(a);
104
108
  d !== null && g({ newHeight: d, registeredElement: e });
105
109
  return;
106
110
  }
107
111
  }
108
112
  };
109
- window.addEventListener("message", h);
110
- const I = o ? W() : { type: "iframe-child-init", targetElementSelector: s, bodyPadding: a, bodyMargin: c }, y = () => {
111
- T(n, () => n.contentWindow?.postMessage(I, "*")), i.retryAttempts++, i.retryTimeoutId = window.setTimeout(y, 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
+ }
112
123
  };
113
- return y(), () => window.removeEventListener("message", h);
114
124
  }
115
- function U(e) {
125
+ function _(e) {
116
126
  const { iframe: t, settings: n } = e, { targetElementSelector: i } = n;
117
127
  let r = 0;
118
- const o = () => {
119
- const s = f(t.contentDocument, i);
120
- if (!t.contentDocument || !s)
121
- return r++, setTimeout(o, m(r));
122
- L(t.contentDocument, n), w().observe(s);
128
+ const s = () => {
129
+ const o = l(t.contentDocument, i);
130
+ if (!t.contentDocument || !o)
131
+ return r++, setTimeout(s, b(r));
132
+ O(t.contentDocument, n), p().observe(o);
123
133
  };
124
- return C(t, o), () => {
125
- const s = f(t.contentDocument, i);
126
- s && w().unobserve(s), t.removeEventListener("load", o);
134
+ return k(t, s), {
135
+ unsubscribe: () => {
136
+ const o = l(t.contentDocument, i);
137
+ o && p().unobserve(o);
138
+ },
139
+ resize: () => L(e)
127
140
  };
128
141
  }
129
- function $({ iframe: e, interactionState: t }) {
130
- const n = () => {
142
+ function G({ iframe: e, interactionState: t, settings: n }) {
143
+ if (!n.onBeforeIframeResize && !n.onIframeResize)
144
+ return () => {
145
+ };
146
+ const i = () => {
131
147
  t.isHovered = !0;
132
- }, i = () => {
148
+ }, r = () => {
133
149
  t.isHovered = !1;
134
150
  };
135
- return e.addEventListener("mouseenter", n), e.addEventListener("mouseleave", i), () => {
136
- 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);
137
153
  };
138
154
  }
139
- function q() {
155
+ function V() {
140
156
  let e = null;
141
157
  return () => {
142
158
  if (!e) {
143
159
  const t = ({ target: n }) => {
144
- const i = l.find(({ iframe: c }) => c.contentDocument === n.ownerDocument);
145
- if (!i)
146
- return;
147
- const { iframe: r, settings: o } = i, s = f(r.contentDocument, o.targetElementSelector);
148
- if (!s)
149
- return;
150
- const { height: a } = p(s);
151
- a && g({ newHeight: a, registeredElement: i });
160
+ const i = m.find(({ iframe: r }) => r.contentDocument === n.ownerDocument);
161
+ i && L(i);
152
162
  };
153
163
  e = new ResizeObserver((n) => n.forEach(t));
154
164
  }
155
165
  return e;
156
166
  };
157
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
+ }
158
175
  function g({ registeredElement: e, newHeight: t }) {
159
- const { iframe: n, settings: i, interactionState: r, initContext: o } = e;
160
- if (o.isInitialized || (o.isInitialized = !0, clearTimeout(o.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)
161
178
  return;
162
- const s = n.getBoundingClientRect(), a = t + i.offsetSize;
163
- 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)
164
181
  return;
165
- const c = {
182
+ const u = {
166
183
  iframe: n,
167
184
  settings: { ...i },
168
185
  interactionState: { ...r },
169
- previousRenderState: { rect: s },
186
+ previousRenderState: { rect: o },
170
187
  nextRenderState: { rect: n.getBoundingClientRect() }
171
188
  };
172
- i.onIframeResize(c);
189
+ i.onIframeResize(u);
173
190
  }
174
- const _ = G();
175
- let v = !1;
176
- V();
177
- function V() {
178
- !z() || !R() || window.addEventListener("message", (e) => {
179
- e.data?.type === "iframe-child-init" && M(() => O(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));
180
200
  });
181
201
  }
182
- function O(e, t = 0) {
183
- const { targetElementSelector: n, bodyPadding: i, bodyMargin: r } = e.data, o = f(document, n);
184
- if (v || 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)
185
205
  return;
186
- if (!o)
187
- return setTimeout(() => O(e, t + 1), m(t));
188
- L(document, { bodyMargin: r, bodyPadding: i });
189
- const s = _();
190
- s.disconnect(), s.observe(o), v = !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;
211
+ }
212
+ function Q(e) {
213
+ const t = l(document, R);
214
+ !h || window.parent !== e.source || !t || E(t);
191
215
  }
192
- function G() {
216
+ function X() {
193
217
  let e = null;
194
218
  return () => (e || (e = new ResizeObserver((t) => {
195
- if (!t[0].target)
196
- return;
197
- const { height: n, width: i } = p(t[0].target), r = {
198
- type: "iframe-resized",
199
- width: i,
200
- height: n
201
- };
202
- window.parent.postMessage(r, "*");
219
+ t[0].target && E(t[0].target);
203
220
  })), e);
204
221
  }
205
- const Q = ({ 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 }) => {
206
230
  document.activeElement === n && window.scrollBy(0, t.rect.bottom - e.rect.bottom);
207
231
  };
208
232
  export {
209
- K as initialize,
210
- V as initializeChildListener,
211
- Q as updateParentScrollOnResize
233
+ Z as initialize,
234
+ K as initializeChildListener,
235
+ j as updateParentScrollOnResize
212
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=()=>{try{return window.self!==window.top}catch{return!0}},b=e=>e instanceof HTMLIFrameElement,E=e=>{window.document.readyState==="complete"?e():window.addEventListener("load",e,{once:!0})},T=(e,t)=>{t(),e.addEventListener("load",t,{once:!0})},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,{once:!0})},C=()=>({offsetSize:0,checkOrigin:!0,enableLegacyLibSupport:!1});async function D(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 k=e=>{try{const t=new URL(e.src).origin;if(t!=="about:blank")return t}catch{}return null},B=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=q();let m=[];const A=async(e,t)=>{if(!u())return[];const n={...C(),...B(e??{})},i=H(t),r=x(n,i);return Promise.all(i.map(async o=>{const s={iframe:o,settings:n,interactionState:{isHovered:!1},initContext:{isInitialized:!1,retryAttempts:0}},c=await F(s,r);return m.push(s),{unsubscribe:()=>{c(),m=m.filter(d=>d.iframe!==o)}}}))};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 r=k(i);r&&n.push(r)}return n}async function F(e,t){const i=await D(e.iframe)?U(e):N(e,t),r=$(e);return()=>{i(),r()}}function N(e,t){const{iframe:n,initContext:i,settings:{checkOrigin:r,enableLegacyLibSupport:o,targetElementSelector:s,bodyPadding:c,bodyMargin:d}}=e,I=l=>{const J=l.origin==="null",K=!r||J||t.includes(l.origin);if(!(!(n.contentWindow===l.source)||!K)){if(l.data?.type==="iframe-resized"){const{height:f}=l.data;f&&y({newHeight:f,registeredElement:e});return}if(o){const f=W(l);f!==null&&y({newHeight:f,registeredElement:e});return}}};window.addEventListener("message",I);const G=o?P():{type:"iframe-child-init",targetElementSelector:s,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 r=0;const o=()=>{const s=g(t.contentDocument,i);if(!t.contentDocument||!s)return r++,setTimeout(o,h(r));z(t.contentDocument,n),p().observe(s)};return M(t,o),()=>{const s=g(t.contentDocument,i);s&&p().unobserve(s),t.removeEventListener("load",o)}}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 q(){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:r,settings:o}=i,s=g(r.contentDocument,o.targetElementSelector);if(!s)return;const{height:c}=w(s);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:r,initContext:o}=e;if(o.isInitialized||(o.isInitialized=!0,clearTimeout(o.retryTimeoutId)),i.onBeforeIframeResize?.({iframe:n,settings:{...i},observedHeight:t})===!1)return;const s=n.getBoundingClientRect(),c=t+i.offsetSize;if(n.style.height=`${c}px`,!i.onIframeResize)return;const d={iframe:n,settings:{...i},interactionState:{...r},previousRenderState:{rect:s},nextRenderState:{rect:n.getBoundingClientRect()}};i.onIframeResize(d)}const _=V();let v=!1;O();function O(){!u()||!R()||window.addEventListener("message",e=>{e.data?.type==="iframe-child-init"&&E(()=>L(e))})}function L(e,t=0){const{targetElementSelector:n,bodyPadding:i,bodyMargin:r}=e.data,o=g(document,n);if(v||window.parent!==e.source)return;if(!o)return setTimeout(()=>L(e,t+1),h(t));z(document,{bodyMargin:r,bodyPadding:i});const s=_();s.disconnect(),s.observe(o),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),r={type:"iframe-resized",width:i,height:n};window.parent.postMessage(r,"*")})),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=O,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": "2.0.0",
4
+ "version": "2.1.0",
5
5
  "description": "Open-source modern iframe resizer",
6
6
  "license": "MIT",
7
7
  "repository": {