@open-iframe-resizer/core 1.0.0-rc1 → 1.0.0-rc4
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/LICENSE.md +20 -0
- package/README.md +18 -0
- package/dist/index.js +0 -1
- package/dist/index.umd.cjs +0 -1
- package/package.json +7 -10
- package/dist/index.js.map +0 -1
- package/dist/index.umd.cjs.map +0 -1
package/LICENSE.md
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Copyright (c) 2012-2024 Scott Chacon and others
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
4
|
+
a copy of this software and associated documentation files (the
|
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
9
|
+
the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be
|
|
12
|
+
included in all copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# Open Iframe Resizer
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
|
|
5
|
+
`iframe-resizer` is a modern, lightweight library for resizing iframes dynamically. It is shipped under the MIT license, making it usable in commercial projects.
|
|
6
|
+
|
|
7
|
+
## Getting Started
|
|
8
|
+
|
|
9
|
+
### Browser
|
|
10
|
+
#### ES6 Modules
|
|
11
|
+
|
|
12
|
+
```html
|
|
13
|
+
<script type="module">
|
|
14
|
+
import { initialize } from "/dist/index.js";
|
|
15
|
+
|
|
16
|
+
initialize({}, "#iframe-id");
|
|
17
|
+
</script>
|
|
18
|
+
```
|
package/dist/index.js
CHANGED
package/dist/index.umd.cjs
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
1
|
(function(o,d){typeof exports=="object"&&typeof module<"u"?d(exports):typeof define=="function"&&define.amd?define(["exports"],d):(o=typeof globalThis<"u"?globalThis:o||self,d(o.iframeResizer={}))})(this,function(o){"use strict";const d=()=>window&&window.self!==window.top,f=e=>e instanceof HTMLIFrameElement,y=()=>({offsetSize:0,checkOrigin:!0}),O=e=>{try{return new URL(e.src,window.location.origin).origin===window.location.origin}catch{return!1}};function m(e,i){let n;return(...t)=>{clearTimeout(n),n=setTimeout(()=>e.apply(void 0,t),i)}}const g=S(),l=[],z=(e,i)=>{const n={...y(),...e},t=p(i),r=v(n,t);return t.map(a=>{const s=l.push({iframe:a,settings:n}),c=E(a,n,r);return{unsubscribe:()=>{c(),l.splice(s-1,1)}}})};function p(e){return typeof e=="string"?Array.from(document.querySelectorAll(e)).filter(f):e?f(e)?[e]:[]:Array.from(document.getElementsByTagName("iframe"))}function v(e,i){if(Array.isArray(e.checkOrigin))return e.checkOrigin;if(!e.checkOrigin)return[];const n=[];for(const t of i){const r=L(t);r&&n.push(r)}return n}function L(e){try{const i=new URL(e.src).origin;if(i!=="about:blank")return i}catch{}return null}function E(e,i,n){return O(e)?I(e):R(e,i,n)}function R(e,i,n){const t=r=>{var s;if((!i.checkOrigin||n.includes(r.origin))&&((s=r.data)==null?void 0:s.type)==="iframe-resized"&&e.contentWindow===r.source){const{width:c,height:u}=r.data;h({width:c,height:u,iframe:e,settings:i})}};return window.addEventListener("message",t,!1),()=>window.removeEventListener("message",t,!1)}function I(e){const i=()=>{var t;const n=(t=e.contentDocument)==null?void 0:t.body;if(!n){console.error("Unable to observe the iframe content document body");return}g.observe(n)};return e.addEventListener("load",i),()=>{var n;(n=e.contentDocument)!=null&&n.body&&g.unobserve(e.contentDocument.body),e.removeEventListener("load",i)}}function S(){const e=({target:n})=>{var u;const t=l.find(k=>{var w;return((w=k.iframe.contentDocument)==null?void 0:w.body)===n});if(!t)return;const{iframe:r,settings:a}=t,{scrollHeight:s,scrollWidth:c}=((u=r.contentDocument)==null?void 0:u.documentElement)??{};s!==void 0&&c!==void 0&&h({width:c,height:s,iframe:r,settings:a})},i=m(n=>n.forEach(e),10);return new ResizeObserver(i)}function h({width:e,height:i,iframe:n,settings:t}){n.style.width=`${e}px`,n.style.height=`${i+t.offsetSize}px`}d()&&b();function b(){window.addEventListener("load",()=>{const e=m(()=>{const n={type:"iframe-resized",width:document.documentElement.scrollWidth,height:document.documentElement.scrollHeight};window.parent.postMessage(n,"*")},10);new ResizeObserver(e).observe(document.body)})}o.initialize=z,o.initializeChildListener=b,Object.defineProperty(o,Symbol.toStringTag,{value:"Module"})});
|
|
2
|
-
//# sourceMappingURL=index.umd.cjs.map
|
package/package.json
CHANGED
|
@@ -1,18 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@open-iframe-resizer/core",
|
|
3
3
|
"private": false,
|
|
4
|
-
"version": "1.0.0-
|
|
5
|
-
"description": "
|
|
4
|
+
"version": "1.0.0-rc4",
|
|
5
|
+
"description": "Open-source modern iframe resizer",
|
|
6
6
|
"license": "MIT",
|
|
7
|
-
"keywords": [
|
|
8
|
-
"iframe-resizer",
|
|
9
|
-
"iframe",
|
|
10
|
-
"resize"
|
|
11
|
-
],
|
|
12
7
|
"repository": {
|
|
13
8
|
"type": "git",
|
|
14
9
|
"url": "git+https://github.com/Lemick/open-iframe-resizer.git"
|
|
15
10
|
},
|
|
11
|
+
"engines": {
|
|
12
|
+
"node": ">=20.0.0"
|
|
13
|
+
},
|
|
14
|
+
"keywords": ["iframe", "Resizing", "Resizer", "resize", "cross", "same", "domain", "width", "height", "responsive"],
|
|
16
15
|
"type": "module",
|
|
17
16
|
"main": "./dist/index.cjs",
|
|
18
17
|
"module": "./dist/index.js",
|
|
@@ -24,9 +23,7 @@
|
|
|
24
23
|
"require": "./dist/index.cjs"
|
|
25
24
|
}
|
|
26
25
|
},
|
|
27
|
-
"files": [
|
|
28
|
-
"dist"
|
|
29
|
-
],
|
|
26
|
+
"files": ["dist", "README.md", "LICENSE.md"],
|
|
30
27
|
"scripts": {
|
|
31
28
|
"docs": "npm run typedoc",
|
|
32
29
|
"lint": "biome lint ./src",
|
package/dist/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../src/common.ts","../src/parent.ts","../src/child.ts"],"sourcesContent":["import type { Settings } from \"~/type\";\n\nconst isInIframe = () => window && window.self !== window.top;\n\nconst isSameOriginIframe = (iframe: HTMLIFrameElement): iframe is HTMLIFrameElement & { contentDocument: Document } => !!iframe.contentDocument;\n\nconst isHtmlIframeElement = (element: Element): element is HTMLIFrameElement => element instanceof HTMLIFrameElement;\n\nconst deferWhenDomContentIsLoaded = (document: Document, executable: () => void) => {\n\tdocument.readyState === \"loading\" ? document.addEventListener(\"DOMContentLoaded\", executable) : executable();\n};\n\nconst deferWhenPageIsLoaded = (document: Document, executable: () => void) => {\n\tdocument.readyState !== \"complete\" ? document.addEventListener(\"load\", executable) : executable();\n};\n\nconst getDefaultSettings: () => Settings = () => ({ offsetSize: 0, checkOrigin: true });\n\nconst isIframeSameOrigin = (iframe: HTMLIFrameElement) => {\n\ttry {\n\t\tconst iframeSrc = new URL(iframe.src, window.location.origin);\n\t\treturn iframeSrc.origin === window.location.origin;\n\t} catch (e) {\n\t\treturn false;\n\t}\n};\n\n// biome-ignore lint/suspicious/noExplicitAny:\nfunction debounce<T extends (...args: any[]) => any>(f: T, delay: number) {\n\tlet timer: NodeJS.Timeout;\n\treturn (...args: unknown[]) => {\n\t\tclearTimeout(timer);\n\t\ttimer = setTimeout(() => f.apply(undefined, args), delay);\n\t};\n}\n\nexport {\n\tisInIframe,\n\tisSameOriginIframe,\n\tdeferWhenDomContentIsLoaded,\n\tisHtmlIframeElement,\n\tdeferWhenPageIsLoaded,\n\tisIframeSameOrigin,\n\tdebounce,\n\tgetDefaultSettings,\n};\n","import { debounce, getDefaultSettings, isHtmlIframeElement, isIframeSameOrigin } from \"~/common\";\nimport type { IframeResizeEvent, InitializeFunction, Settings } from \"./type\";\n\nconst resizeObserver = createResizeObserver();\nconst registeredIframes: Array<{ iframe: HTMLIFrameElement; settings: Settings }> = [];\n\nconst initialize: InitializeFunction = (clientSettings, selector) => {\n\tconst finalSettings = { ...getDefaultSettings(), ...clientSettings };\n\tconst iframes = resolveIframesToRegister(selector);\n\tconst allowedOrigins = registerIframesAllowOrigins(finalSettings, iframes);\n\n\treturn iframes.map((iframe) => {\n\t\tconst length = registeredIframes.push({ iframe, settings: finalSettings });\n\t\tconst unsubscribeResizeListener = addChildResizeListener(iframe, finalSettings, allowedOrigins);\n\t\treturn {\n\t\t\tunsubscribe: () => {\n\t\t\t\tunsubscribeResizeListener();\n\t\t\t\tregisteredIframes.splice(length - 1, 1);\n\t\t\t},\n\t\t};\n\t});\n};\n\nfunction resolveIframesToRegister(selector?: string | HTMLIFrameElement): HTMLIFrameElement[] {\n\tif (typeof selector === \"string\") {\n\t\treturn Array.from(document.querySelectorAll<HTMLElement>(selector)).filter(isHtmlIframeElement);\n\t}\n\tif (selector) {\n\t\treturn isHtmlIframeElement(selector) ? [selector] : [];\n\t}\n\treturn Array.from(document.getElementsByTagName(\"iframe\"));\n}\n\nfunction registerIframesAllowOrigins(settings: Settings, iframes: HTMLIFrameElement[]) {\n\tif (Array.isArray(settings.checkOrigin)) {\n\t\treturn settings.checkOrigin;\n\t}\n\n\tif (!settings.checkOrigin) {\n\t\treturn [];\n\t}\n\n\tconst allowedOrigins: string[] = [];\n\tfor (const iframe of iframes) {\n\t\tconst origin = extractIframeOrigin(iframe);\n\t\tif (origin) {\n\t\t\tallowedOrigins.push(origin);\n\t\t}\n\t}\n\treturn allowedOrigins;\n}\n\nfunction extractIframeOrigin(iframe: HTMLIFrameElement): string | null {\n\ttry {\n\t\tconst origin = new URL(iframe.src).origin;\n\t\tif (origin !== \"about:blank\") {\n\t\t\treturn origin;\n\t\t}\n\t} catch (error) {}\n\treturn null;\n}\n\nfunction addChildResizeListener(iframe: HTMLIFrameElement, settings: Settings, allowedOrigins: string[]) {\n\tif (isIframeSameOrigin(iframe)) {\n\t\treturn addSameOriginChildResizeListener(iframe);\n\t}\n\treturn addCrossOriginChildResizeListener(iframe, settings, allowedOrigins);\n}\n\nfunction addCrossOriginChildResizeListener(iframe: HTMLIFrameElement, settings: Settings, allowedOrigins: string[]) {\n\tconst handleIframeResizedMessage = (event: MessageEvent) => {\n\t\tconst isOriginValid = !settings.checkOrigin || allowedOrigins.includes(event.origin);\n\n\t\tif (isOriginValid && event.data?.type === \"iframe-resized\" && iframe.contentWindow === event.source) {\n\t\t\tconst { width, height } = (event as IframeResizeEvent).data;\n\t\t\tupdateIframeDimensions({ width, height, iframe, settings });\n\t\t}\n\t};\n\n\twindow.addEventListener(\"message\", handleIframeResizedMessage, false);\n\n\treturn () => window.removeEventListener(\"message\", handleIframeResizedMessage, false);\n}\n\nfunction addSameOriginChildResizeListener(iframe: HTMLIFrameElement) {\n\tconst startListener = () => {\n\t\tconst contentBody = iframe.contentDocument?.body;\n\n\t\tif (!contentBody) {\n\t\t\tconsole.error(\"Unable to observe the iframe content document body\");\n\t\t\treturn;\n\t\t}\n\n\t\tresizeObserver.observe(contentBody);\n\t};\n\n\tiframe.addEventListener(\"load\", startListener);\n\n\treturn () => {\n\t\tif (iframe.contentDocument?.body) {\n\t\t\tresizeObserver.unobserve(iframe.contentDocument.body);\n\t\t}\n\t\tiframe.removeEventListener(\"load\", startListener);\n\t};\n}\n\nfunction createResizeObserver() {\n\tconst handleEntry = ({ target }: ResizeObserverEntry) => {\n\t\tconst matchingRegisteredIframe = registeredIframes.find((value) => value.iframe.contentDocument?.body === target);\n\t\tif (!matchingRegisteredIframe) {\n\t\t\treturn;\n\t\t}\n\t\tconst { iframe, settings } = matchingRegisteredIframe;\n\t\tconst { scrollHeight, scrollWidth } = iframe.contentDocument?.documentElement ?? {};\n\t\tif (scrollHeight !== undefined && scrollWidth !== undefined) {\n\t\t\tupdateIframeDimensions({ width: scrollWidth, height: scrollHeight, iframe, settings });\n\t\t}\n\t};\n\n\tconst resizeObserverCallback = debounce<ResizeObserverCallback>((entries) => entries.forEach(handleEntry), 10);\n\treturn new ResizeObserver(resizeObserverCallback);\n}\n\nfunction updateIframeDimensions({ width, height, iframe, settings }: { iframe: HTMLIFrameElement; width: number; height: number; settings: Settings }) {\n\tiframe.style.width = `${width}px`;\n\tiframe.style.height = `${height + settings.offsetSize}px`;\n}\n\nexport { initialize };\n","import { debounce, isInIframe } from \"~/common\";\nimport type { IframeResizeEventData } from \"./type\";\n\nif (isInIframe()) {\n\tinitializeChildListener();\n}\n\nfunction initializeChildListener() {\n\twindow.addEventListener(\"load\", () => {\n\t\tconst resizeObserverCallback = debounce<ResizeObserverCallback>(() => {\n\t\t\tconst data: IframeResizeEventData = {\n\t\t\t\ttype: \"iframe-resized\",\n\t\t\t\twidth: document.documentElement.scrollWidth,\n\t\t\t\theight: document.documentElement.scrollHeight,\n\t\t\t};\n\t\t\twindow.parent.postMessage(data, \"*\");\n\t\t}, 10);\n\n\t\tconst resizeObserver = new ResizeObserver(resizeObserverCallback);\n\t\tresizeObserver.observe(document.body);\n\t});\n}\n\nexport { initializeChildListener };\n"],"names":["isInIframe","isHtmlIframeElement","element","getDefaultSettings","isIframeSameOrigin","iframe","debounce","f","delay","timer","args","resizeObserver","createResizeObserver","registeredIframes","initialize","clientSettings","selector","finalSettings","iframes","resolveIframesToRegister","allowedOrigins","registerIframesAllowOrigins","length","unsubscribeResizeListener","addChildResizeListener","settings","origin","extractIframeOrigin","addSameOriginChildResizeListener","addCrossOriginChildResizeListener","handleIframeResizedMessage","event","_a","width","height","updateIframeDimensions","startListener","contentBody","handleEntry","target","matchingRegisteredIframe","value","scrollHeight","scrollWidth","resizeObserverCallback","entries","initializeChildListener","data"],"mappings":"AAEA,MAAMA,IAAa,MAAM,UAAU,OAAO,SAAS,OAAO,KAIpDC,IAAsB,CAACC,MAAmDA,aAAmB,mBAU7FC,IAAqC,OAAO,EAAE,YAAY,GAAG,aAAa,GAAK,IAE/EC,IAAqB,CAACC,MAA8B;AACrD,MAAA;AAEI,WADW,IAAI,IAAIA,EAAO,KAAK,OAAO,SAAS,MAAM,EAC3C,WAAW,OAAO,SAAS;AAAA,UACjC;AACJ,WAAA;AAAA,EACR;AACD;AAGA,SAASC,EAA4CC,GAAMC,GAAe;AACrE,MAAAC;AACJ,SAAO,IAAIC,MAAoB;AAC9B,iBAAaD,CAAK,GAClBA,IAAQ,WAAW,MAAMF,EAAE,MAAM,QAAWG,CAAI,GAAGF,CAAK;AAAA,EAAA;AAE1D;AC/BA,MAAMG,IAAiBC,EAAqB,GACtCC,IAA8E,CAAA,GAE9EC,IAAiC,CAACC,GAAgBC,MAAa;AACpE,QAAMC,IAAgB,EAAE,GAAGd,EAAmB,GAAG,GAAGY,EAAe,GAC7DG,IAAUC,EAAyBH,CAAQ,GAC3CI,IAAiBC,EAA4BJ,GAAeC,CAAO;AAElE,SAAAA,EAAQ,IAAI,CAACb,MAAW;AAC9B,UAAMiB,IAAST,EAAkB,KAAK,EAAE,QAAAR,GAAQ,UAAUY,GAAe,GACnEM,IAA4BC,EAAuBnB,GAAQY,GAAeG,CAAc;AACvF,WAAA;AAAA,MACN,aAAa,MAAM;AACQ,QAAAG,KACRV,EAAA,OAAOS,IAAS,GAAG,CAAC;AAAA,MACv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eM,CAAsB;AACjD;AAEA,SAAST,EAAuB,EAAE,OAAAF,GAAO,QAAAC,GAAQ,QAAA7B,GAAQ,UAAAoB,KAA8F;AAC/I,EAAApB,EAAA,MAAM,QAAQ,GAAG4B,CAAK,MAC7B5B,EAAO,MAAM,SAAS,GAAG6B,IAAST,EAAS,UAAU;AACtD;AC3HIzB,OACqB8C;AAGzB,SAASA,IAA0B;AAC3B,SAAA,iBAAiB,QAAQ,MAAM;AAC/B,UAAAF,IAAyBtC,EAAiC,MAAM;AACrE,YAAMyC,IAA8B;AAAA,QACnC,MAAM;AAAA,QACN,OAAO,SAAS,gBAAgB;AAAA,QAChC,QAAQ,SAAS,gBAAgB;AAAA,MAAA;AAE3B,aAAA,OAAO,YAAYA,GAAM,GAAG;AAAA,OACjC,EAAE;AAGU,IADQ,IAAI,eAAeH,CAAsB,EACjD,QAAQ,SAAS,IAAI;AAAA,EAAA,CACpC;AACF;"}
|
package/dist/index.umd.cjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.umd.cjs","sources":["../src/common.ts","../src/parent.ts","../src/child.ts"],"sourcesContent":["import type { Settings } from \"~/type\";\n\nconst isInIframe = () => window && window.self !== window.top;\n\nconst isSameOriginIframe = (iframe: HTMLIFrameElement): iframe is HTMLIFrameElement & { contentDocument: Document } => !!iframe.contentDocument;\n\nconst isHtmlIframeElement = (element: Element): element is HTMLIFrameElement => element instanceof HTMLIFrameElement;\n\nconst deferWhenDomContentIsLoaded = (document: Document, executable: () => void) => {\n\tdocument.readyState === \"loading\" ? document.addEventListener(\"DOMContentLoaded\", executable) : executable();\n};\n\nconst deferWhenPageIsLoaded = (document: Document, executable: () => void) => {\n\tdocument.readyState !== \"complete\" ? document.addEventListener(\"load\", executable) : executable();\n};\n\nconst getDefaultSettings: () => Settings = () => ({ offsetSize: 0, checkOrigin: true });\n\nconst isIframeSameOrigin = (iframe: HTMLIFrameElement) => {\n\ttry {\n\t\tconst iframeSrc = new URL(iframe.src, window.location.origin);\n\t\treturn iframeSrc.origin === window.location.origin;\n\t} catch (e) {\n\t\treturn false;\n\t}\n};\n\n// biome-ignore lint/suspicious/noExplicitAny:\nfunction debounce<T extends (...args: any[]) => any>(f: T, delay: number) {\n\tlet timer: NodeJS.Timeout;\n\treturn (...args: unknown[]) => {\n\t\tclearTimeout(timer);\n\t\ttimer = setTimeout(() => f.apply(undefined, args), delay);\n\t};\n}\n\nexport {\n\tisInIframe,\n\tisSameOriginIframe,\n\tdeferWhenDomContentIsLoaded,\n\tisHtmlIframeElement,\n\tdeferWhenPageIsLoaded,\n\tisIframeSameOrigin,\n\tdebounce,\n\tgetDefaultSettings,\n};\n","import { debounce, getDefaultSettings, isHtmlIframeElement, isIframeSameOrigin } from \"~/common\";\nimport type { IframeResizeEvent, InitializeFunction, Settings } from \"./type\";\n\nconst resizeObserver = createResizeObserver();\nconst registeredIframes: Array<{ iframe: HTMLIFrameElement; settings: Settings }> = [];\n\nconst initialize: InitializeFunction = (clientSettings, selector) => {\n\tconst finalSettings = { ...getDefaultSettings(), ...clientSettings };\n\tconst iframes = resolveIframesToRegister(selector);\n\tconst allowedOrigins = registerIframesAllowOrigins(finalSettings, iframes);\n\n\treturn iframes.map((iframe) => {\n\t\tconst length = registeredIframes.push({ iframe, settings: finalSettings });\n\t\tconst unsubscribeResizeListener = addChildResizeListener(iframe, finalSettings, allowedOrigins);\n\t\treturn {\n\t\t\tunsubscribe: () => {\n\t\t\t\tunsubscribeResizeListener();\n\t\t\t\tregisteredIframes.splice(length - 1, 1);\n\t\t\t},\n\t\t};\n\t});\n};\n\nfunction resolveIframesToRegister(selector?: string | HTMLIFrameElement): HTMLIFrameElement[] {\n\tif (typeof selector === \"string\") {\n\t\treturn Array.from(document.querySelectorAll<HTMLElement>(selector)).filter(isHtmlIframeElement);\n\t}\n\tif (selector) {\n\t\treturn isHtmlIframeElement(selector) ? [selector] : [];\n\t}\n\treturn Array.from(document.getElementsByTagName(\"iframe\"));\n}\n\nfunction registerIframesAllowOrigins(settings: Settings, iframes: HTMLIFrameElement[]) {\n\tif (Array.isArray(settings.checkOrigin)) {\n\t\treturn settings.checkOrigin;\n\t}\n\n\tif (!settings.checkOrigin) {\n\t\treturn [];\n\t}\n\n\tconst allowedOrigins: string[] = [];\n\tfor (const iframe of iframes) {\n\t\tconst origin = extractIframeOrigin(iframe);\n\t\tif (origin) {\n\t\t\tallowedOrigins.push(origin);\n\t\t}\n\t}\n\treturn allowedOrigins;\n}\n\nfunction extractIframeOrigin(iframe: HTMLIFrameElement): string | null {\n\ttry {\n\t\tconst origin = new URL(iframe.src).origin;\n\t\tif (origin !== \"about:blank\") {\n\t\t\treturn origin;\n\t\t}\n\t} catch (error) {}\n\treturn null;\n}\n\nfunction addChildResizeListener(iframe: HTMLIFrameElement, settings: Settings, allowedOrigins: string[]) {\n\tif (isIframeSameOrigin(iframe)) {\n\t\treturn addSameOriginChildResizeListener(iframe);\n\t}\n\treturn addCrossOriginChildResizeListener(iframe, settings, allowedOrigins);\n}\n\nfunction addCrossOriginChildResizeListener(iframe: HTMLIFrameElement, settings: Settings, allowedOrigins: string[]) {\n\tconst handleIframeResizedMessage = (event: MessageEvent) => {\n\t\tconst isOriginValid = !settings.checkOrigin || allowedOrigins.includes(event.origin);\n\n\t\tif (isOriginValid && event.data?.type === \"iframe-resized\" && iframe.contentWindow === event.source) {\n\t\t\tconst { width, height } = (event as IframeResizeEvent).data;\n\t\t\tupdateIframeDimensions({ width, height, iframe, settings });\n\t\t}\n\t};\n\n\twindow.addEventListener(\"message\", handleIframeResizedMessage, false);\n\n\treturn () => window.removeEventListener(\"message\", handleIframeResizedMessage, false);\n}\n\nfunction addSameOriginChildResizeListener(iframe: HTMLIFrameElement) {\n\tconst startListener = () => {\n\t\tconst contentBody = iframe.contentDocument?.body;\n\n\t\tif (!contentBody) {\n\t\t\tconsole.error(\"Unable to observe the iframe content document body\");\n\t\t\treturn;\n\t\t}\n\n\t\tresizeObserver.observe(contentBody);\n\t};\n\n\tiframe.addEventListener(\"load\", startListener);\n\n\treturn () => {\n\t\tif (iframe.contentDocument?.body) {\n\t\t\tresizeObserver.unobserve(iframe.contentDocument.body);\n\t\t}\n\t\tiframe.removeEventListener(\"load\", startListener);\n\t};\n}\n\nfunction createResizeObserver() {\n\tconst handleEntry = ({ target }: ResizeObserverEntry) => {\n\t\tconst matchingRegisteredIframe = registeredIframes.find((value) => value.iframe.contentDocument?.body === target);\n\t\tif (!matchingRegisteredIframe) {\n\t\t\treturn;\n\t\t}\n\t\tconst { iframe, settings } = matchingRegisteredIframe;\n\t\tconst { scrollHeight, scrollWidth } = iframe.contentDocument?.documentElement ?? {};\n\t\tif (scrollHeight !== undefined && scrollWidth !== undefined) {\n\t\t\tupdateIframeDimensions({ width: scrollWidth, height: scrollHeight, iframe, settings });\n\t\t}\n\t};\n\n\tconst resizeObserverCallback = debounce<ResizeObserverCallback>((entries) => entries.forEach(handleEntry), 10);\n\treturn new ResizeObserver(resizeObserverCallback);\n}\n\nfunction updateIframeDimensions({ width, height, iframe, settings }: { iframe: HTMLIFrameElement; width: number; height: number; settings: Settings }) {\n\tiframe.style.width = `${width}px`;\n\tiframe.style.height = `${height + settings.offsetSize}px`;\n}\n\nexport { initialize };\n","import { debounce, isInIframe } from \"~/common\";\nimport type { IframeResizeEventData } from \"./type\";\n\nif (isInIframe()) {\n\tinitializeChildListener();\n}\n\nfunction initializeChildListener() {\n\twindow.addEventListener(\"load\", () => {\n\t\tconst resizeObserverCallback = debounce<ResizeObserverCallback>(() => {\n\t\t\tconst data: IframeResizeEventData = {\n\t\t\t\ttype: \"iframe-resized\",\n\t\t\t\twidth: document.documentElement.scrollWidth,\n\t\t\t\theight: document.documentElement.scrollHeight,\n\t\t\t};\n\t\t\twindow.parent.postMessage(data, \"*\");\n\t\t}, 10);\n\n\t\tconst resizeObserver = new ResizeObserver(resizeObserverCallback);\n\t\tresizeObserver.observe(document.body);\n\t});\n}\n\nexport { initializeChildListener };\n"],"names":["isInIframe","isHtmlIframeElement","element","getDefaultSettings","isIframeSameOrigin","iframe","debounce","f","delay","timer","args","resizeObserver","createResizeObserver","registeredIframes","initialize","clientSettings","selector","finalSettings","iframes","resolveIframesToRegister","allowedOrigins","registerIframesAllowOrigins","length","unsubscribeResizeListener","addChildResizeListener","settings","origin","extractIframeOrigin","addSameOriginChildResizeListener","addCrossOriginChildResizeListener","handleIframeResizedMessage","event","_a","width","height","updateIframeDimensions","startListener","contentBody","handleEntry","target","matchingRegisteredIframe","value","scrollHeight","scrollWidth","resizeObserverCallback","entries","initializeChildListener","data"],"mappings":"qOAEA,MAAMA,EAAa,IAAM,QAAU,OAAO,OAAS,OAAO,IAIpDC,EAAuBC,GAAmDA,aAAmB,kBAU7FC,EAAqC,KAAO,CAAE,WAAY,EAAG,YAAa,EAAK,GAE/EC,EAAsBC,GAA8B,CACrD,GAAA,CAEI,OADW,IAAI,IAAIA,EAAO,IAAK,OAAO,SAAS,MAAM,EAC3C,SAAW,OAAO,SAAS,YACjC,CACJ,MAAA,EACR,CACD,EAGA,SAASC,EAA4CC,EAAMC,EAAe,CACrE,IAAAC,EACJ,MAAO,IAAIC,IAAoB,CAC9B,aAAaD,CAAK,EAClBA,EAAQ,WAAW,IAAMF,EAAE,MAAM,OAAWG,CAAI,EAAGF,CAAK,CAAA,CAE1D,CC/BA,MAAMG,EAAiBC,EAAqB,EACtCC,EAA8E,CAAA,EAE9EC,EAAiC,CAACC,EAAgBC,IAAa,CACpE,MAAMC,EAAgB,CAAE,GAAGd,EAAmB,EAAG,GAAGY,CAAe,EAC7DG,EAAUC,EAAyBH,CAAQ,EAC3CI,EAAiBC,EAA4BJ,EAAeC,CAAO,EAElE,OAAAA,EAAQ,IAAKb,GAAW,CAC9B,MAAMiB,EAAST,EAAkB,KAAK,CAAE,OAAAR,EAAQ,SAAUY,EAAe,EACnEM,EAA4BC,EAAuBnB,EAAQY,EAAeG,CAAc,EACvF,MAAA,CACN,YAAa,IAAM,CACQG,IACRV,EAAA,OAAOS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