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