@open-iframe-resizer/core 1.0.0-rc4 → 1.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/LICENSE.md +20 -20
- package/README.md +29 -6
- package/dist/common.d.ts +3 -4
- package/dist/index.js +58 -59
- package/dist/index.umd.cjs +1 -1
- package/dist/type.d.ts +14 -8
- package/package.json +20 -6
package/LICENSE.md
CHANGED
|
@@ -1,20 +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.
|
|
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
CHANGED
|
@@ -1,18 +1,41 @@
|
|
|
1
|
-
# Open Iframe Resizer
|
|
1
|
+
# Open Iframe Resizer
|
|
2
2
|
|
|
3
3
|
## Overview
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
A modern, lightweight library for resizing iframes dynamically. It is shipped under the MIT license, making it usable in commercial projects.
|
|
6
|
+
|
|
7
|
+
If you found this plugin helpful, please consider starring the repository!
|
|
6
8
|
|
|
7
9
|
## Getting Started
|
|
8
10
|
|
|
9
|
-
### Browser
|
|
10
|
-
#### ES6 Modules
|
|
11
|
+
### Browser (ES6 modules)
|
|
11
12
|
|
|
12
13
|
```html
|
|
13
14
|
<script type="module">
|
|
14
|
-
import { initialize } from "/dist/index.js";
|
|
15
|
+
import { initialize } from "https://cdn.jsdelivr.net/npm/@open-iframe-resizer/core@latest/dist/index.js";
|
|
15
16
|
|
|
16
|
-
initialize({}, "#iframe
|
|
17
|
+
initialize({}, "#my-iframe");
|
|
17
18
|
</script>
|
|
18
19
|
```
|
|
20
|
+
|
|
21
|
+
You can found a working example [here](https://codesandbox.io/p/sandbox/open-iframe-resize-browser-m655zt)
|
|
22
|
+
|
|
23
|
+
### Package
|
|
24
|
+
Note you can also install the package through npm:
|
|
25
|
+
```bash
|
|
26
|
+
npm install @open-iframe-resizer/core
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Notes
|
|
30
|
+
|
|
31
|
+
### Retro-compatibility
|
|
32
|
+
I decided to replicate the API from the well-known iframe-resizer library, as it may facilitate migration to this project (you only need to change the script).
|
|
33
|
+
|
|
34
|
+
### Resize cross-origin iframes
|
|
35
|
+
To resize iframes from a different origin, you also need to load the script on your child page to enable messaging between the two windows (you do not need to call the initialize function in the child; loading the module is sufficient).
|
|
36
|
+
|
|
37
|
+
## Browser support
|
|
38
|
+
|
|
39
|
+
| Chrome | Safari | Firefox | Opera | IE |
|
|
40
|
+
|--------|--------|---------|-------|-----------|
|
|
41
|
+
| 64+ | 13.1+ | 69+ | 51+ | 🙅♂️ |
|
package/dist/common.d.ts
CHANGED
|
@@ -4,9 +4,8 @@ declare const isSameOriginIframe: (iframe: HTMLIFrameElement) => iframe is HTMLI
|
|
|
4
4
|
contentDocument: Document;
|
|
5
5
|
};
|
|
6
6
|
declare const isHtmlIframeElement: (element: Element) => element is HTMLIFrameElement;
|
|
7
|
-
declare const
|
|
8
|
-
declare const
|
|
7
|
+
declare const deferWhenWindowIsLoaded: (_window: Window, executable: () => void) => void;
|
|
8
|
+
declare const deferWhenIframeIsLoaded: (iframe: HTMLIFrameElement, executable: () => void) => void;
|
|
9
9
|
declare const getDefaultSettings: () => Settings;
|
|
10
10
|
declare const isIframeSameOrigin: (iframe: HTMLIFrameElement) => boolean;
|
|
11
|
-
|
|
12
|
-
export { isInIframe, isSameOriginIframe, deferWhenDomContentIsLoaded, isHtmlIframeElement, deferWhenPageIsLoaded, isIframeSameOrigin, debounce, getDefaultSettings, };
|
|
11
|
+
export { isInIframe, isSameOriginIframe, isHtmlIframeElement, deferWhenIframeIsLoaded, isIframeSameOrigin, deferWhenWindowIsLoaded, getDefaultSettings };
|
package/dist/index.js
CHANGED
|
@@ -1,23 +1,22 @@
|
|
|
1
|
-
const
|
|
1
|
+
const g = () => window && window.self !== window.top, l = (e) => e instanceof HTMLIFrameElement, h = (e, n) => {
|
|
2
|
+
e.document.readyState === "complete" ? n() : e.addEventListener("load", n);
|
|
3
|
+
}, w = (e, n) => {
|
|
4
|
+
var i, o;
|
|
5
|
+
const t = ((i = e.contentWindow) == null ? void 0 : i.document.readyState) === "complete";
|
|
6
|
+
return e.src !== "about:blank" && ((o = e.contentWindow) == null ? void 0 : o.location.href) !== "about:blank" && t ? n() : e.addEventListener("load", n);
|
|
7
|
+
}, b = () => ({ offsetSize: 0, checkOrigin: !0 }), y = (e) => {
|
|
2
8
|
try {
|
|
3
9
|
return new URL(e.src, window.location.origin).origin === window.location.origin;
|
|
4
10
|
} catch {
|
|
5
11
|
return !1;
|
|
6
12
|
}
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
clearTimeout(n), n = setTimeout(() => e.apply(void 0, i), t);
|
|
12
|
-
};
|
|
13
|
-
}
|
|
14
|
-
const f = I(), d = [], S = (e, t) => {
|
|
15
|
-
const n = { ...b(), ...e }, i = O(t), r = v(n, i);
|
|
16
|
-
return i.map((c) => {
|
|
17
|
-
const o = d.push({ iframe: c, settings: n }), s = p(c, n, r);
|
|
13
|
+
}, u = E(), a = [], k = (e, n) => {
|
|
14
|
+
const t = { ...b(), ...e }, r = O(n), i = v(t, r);
|
|
15
|
+
return r.map((o) => {
|
|
16
|
+
const s = a.push({ iframe: o, settings: t }), c = L(o, t, i);
|
|
18
17
|
return {
|
|
19
18
|
unsubscribe: () => {
|
|
20
|
-
|
|
19
|
+
c(), a.splice(s - 1, 1);
|
|
21
20
|
}
|
|
22
21
|
};
|
|
23
22
|
});
|
|
@@ -25,87 +24,87 @@ const f = I(), d = [], S = (e, t) => {
|
|
|
25
24
|
function O(e) {
|
|
26
25
|
return typeof e == "string" ? Array.from(document.querySelectorAll(e)).filter(l) : e ? l(e) ? [e] : [] : Array.from(document.getElementsByTagName("iframe"));
|
|
27
26
|
}
|
|
28
|
-
function v(e,
|
|
27
|
+
function v(e, n) {
|
|
29
28
|
if (Array.isArray(e.checkOrigin))
|
|
30
29
|
return e.checkOrigin;
|
|
31
30
|
if (!e.checkOrigin)
|
|
32
31
|
return [];
|
|
33
|
-
const
|
|
34
|
-
for (const
|
|
35
|
-
const
|
|
36
|
-
|
|
32
|
+
const t = [];
|
|
33
|
+
for (const r of n) {
|
|
34
|
+
const i = z(r);
|
|
35
|
+
i && t.push(i);
|
|
37
36
|
}
|
|
38
|
-
return
|
|
37
|
+
return t;
|
|
39
38
|
}
|
|
40
39
|
function z(e) {
|
|
41
40
|
try {
|
|
42
|
-
const
|
|
43
|
-
if (
|
|
44
|
-
return
|
|
41
|
+
const n = new URL(e.src).origin;
|
|
42
|
+
if (n !== "about:blank")
|
|
43
|
+
return n;
|
|
45
44
|
} catch {
|
|
46
45
|
}
|
|
47
46
|
return null;
|
|
48
47
|
}
|
|
49
|
-
function
|
|
50
|
-
return y(e) ?
|
|
48
|
+
function L(e, n, t) {
|
|
49
|
+
return y(e) ? I(e) : p(e, n, t);
|
|
51
50
|
}
|
|
52
|
-
function
|
|
53
|
-
const
|
|
54
|
-
var
|
|
55
|
-
if ((!
|
|
56
|
-
const {
|
|
57
|
-
|
|
51
|
+
function p(e, n, t) {
|
|
52
|
+
const r = (i) => {
|
|
53
|
+
var s;
|
|
54
|
+
if ((!n.checkOrigin || t.includes(i.origin)) && ((s = i.data) == null ? void 0 : s.type) === "iframe-resized" && e.contentWindow === i.source) {
|
|
55
|
+
const { height: c } = i.data;
|
|
56
|
+
f({ height: c, iframe: e, settings: n });
|
|
58
57
|
}
|
|
59
58
|
};
|
|
60
|
-
return window.addEventListener("message",
|
|
59
|
+
return window.addEventListener("message", r, !1), () => window.removeEventListener("message", r, !1);
|
|
61
60
|
}
|
|
62
|
-
function
|
|
63
|
-
const
|
|
64
|
-
var
|
|
65
|
-
const
|
|
66
|
-
if (!
|
|
61
|
+
function I(e) {
|
|
62
|
+
const n = () => {
|
|
63
|
+
var r;
|
|
64
|
+
const t = (r = e.contentDocument) == null ? void 0 : r.body;
|
|
65
|
+
if (!t) {
|
|
67
66
|
console.error("Unable to observe the iframe content document body");
|
|
68
67
|
return;
|
|
69
68
|
}
|
|
70
|
-
|
|
69
|
+
u.observe(t);
|
|
71
70
|
};
|
|
72
|
-
return e
|
|
73
|
-
var
|
|
74
|
-
(
|
|
71
|
+
return w(e, n), () => {
|
|
72
|
+
var t;
|
|
73
|
+
(t = e.contentDocument) != null && t.body && u.unobserve(e.contentDocument.body), e.removeEventListener("load", n);
|
|
75
74
|
};
|
|
76
75
|
}
|
|
77
|
-
function
|
|
76
|
+
function E() {
|
|
78
77
|
const e = ({ target: n }) => {
|
|
79
|
-
var
|
|
80
|
-
const
|
|
81
|
-
var
|
|
82
|
-
return ((
|
|
78
|
+
var c;
|
|
79
|
+
const t = a.find((m) => {
|
|
80
|
+
var d;
|
|
81
|
+
return ((d = m.iframe.contentDocument) == null ? void 0 : d.body) === n;
|
|
83
82
|
});
|
|
84
|
-
if (!
|
|
83
|
+
if (!t)
|
|
85
84
|
return;
|
|
86
|
-
const { iframe: r, settings:
|
|
87
|
-
o !== void 0 && s !== void 0 &&
|
|
88
|
-
}
|
|
89
|
-
return new ResizeObserver(
|
|
85
|
+
const { iframe: r, settings: i } = t, { scrollHeight: o, scrollWidth: s } = ((c = r.contentDocument) == null ? void 0 : c.documentElement) ?? {};
|
|
86
|
+
o !== void 0 && s !== void 0 && f({ height: o, iframe: r, settings: i });
|
|
87
|
+
};
|
|
88
|
+
return new ResizeObserver((n) => n.forEach(e));
|
|
90
89
|
}
|
|
91
|
-
function
|
|
92
|
-
n.style.
|
|
90
|
+
function f({ height: e, iframe: n, settings: t }) {
|
|
91
|
+
n.style.height = `${e + t.offsetSize}px`;
|
|
93
92
|
}
|
|
94
|
-
|
|
93
|
+
g() && R();
|
|
95
94
|
function R() {
|
|
96
|
-
window
|
|
97
|
-
const e =
|
|
98
|
-
const
|
|
95
|
+
h(window, () => {
|
|
96
|
+
const e = () => {
|
|
97
|
+
const t = {
|
|
99
98
|
type: "iframe-resized",
|
|
100
99
|
width: document.documentElement.scrollWidth,
|
|
101
100
|
height: document.documentElement.scrollHeight
|
|
102
101
|
};
|
|
103
|
-
window.parent.postMessage(
|
|
104
|
-
}
|
|
102
|
+
window.parent.postMessage(t, "*");
|
|
103
|
+
};
|
|
105
104
|
new ResizeObserver(e).observe(document.body);
|
|
106
105
|
});
|
|
107
106
|
}
|
|
108
107
|
export {
|
|
109
|
-
|
|
108
|
+
k as initialize,
|
|
110
109
|
R as initializeChildListener
|
|
111
110
|
};
|
package/dist/index.umd.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(
|
|
1
|
+
(function(s,a){typeof exports=="object"&&typeof module<"u"?a(exports):typeof define=="function"&&define.amd?define(["exports"],a):(s=typeof globalThis<"u"?globalThis:s||self,a(s.iframeResizer={}))})(this,function(s){"use strict";const a=()=>window&&window.self!==window.top,l=e=>e instanceof HTMLIFrameElement,b=(e,n)=>{e.document.readyState==="complete"?n():e.addEventListener("load",n)},w=(e,n)=>{var r,o;const t=((r=e.contentWindow)==null?void 0:r.document.readyState)==="complete";return e.src!=="about:blank"&&((o=e.contentWindow)==null?void 0:o.location.href)!=="about:blank"&&t?n():e.addEventListener("load",n)},y=()=>({offsetSize:0,checkOrigin:!0}),O=e=>{try{return new URL(e.src,window.location.origin).origin===window.location.origin}catch{return!1}},f=S(),u=[],p=(e,n)=>{const t={...y(),...e},i=z(n),r=v(t,i);return i.map(o=>{const c=u.push({iframe:o,settings:t}),d=I(o,t,r);return{unsubscribe:()=>{d(),u.splice(c-1,1)}}})};function z(e){return typeof e=="string"?Array.from(document.querySelectorAll(e)).filter(l):e?l(e)?[e]:[]:Array.from(document.getElementsByTagName("iframe"))}function v(e,n){if(Array.isArray(e.checkOrigin))return e.checkOrigin;if(!e.checkOrigin)return[];const t=[];for(const i of n){const r=L(i);r&&t.push(r)}return t}function L(e){try{const n=new URL(e.src).origin;if(n!=="about:blank")return n}catch{}return null}function I(e,n,t){return O(e)?R(e):E(e,n,t)}function E(e,n,t){const i=r=>{var c;if((!n.checkOrigin||t.includes(r.origin))&&((c=r.data)==null?void 0:c.type)==="iframe-resized"&&e.contentWindow===r.source){const{height:d}=r.data;m({height:d,iframe:e,settings:n})}};return window.addEventListener("message",i,!1),()=>window.removeEventListener("message",i,!1)}function R(e){const n=()=>{var i;const t=(i=e.contentDocument)==null?void 0:i.body;if(!t){console.error("Unable to observe the iframe content document body");return}f.observe(t)};return w(e,n),()=>{var t;(t=e.contentDocument)!=null&&t.body&&f.unobserve(e.contentDocument.body),e.removeEventListener("load",n)}}function S(){const e=({target:n})=>{var d;const t=u.find(k=>{var h;return((h=k.iframe.contentDocument)==null?void 0:h.body)===n});if(!t)return;const{iframe:i,settings:r}=t,{scrollHeight:o,scrollWidth:c}=((d=i.contentDocument)==null?void 0:d.documentElement)??{};o!==void 0&&c!==void 0&&m({height:o,iframe:i,settings:r})};return new ResizeObserver(n=>n.forEach(e))}function m({height:e,iframe:n,settings:t}){n.style.height=`${e+t.offsetSize}px`}a()&&g();function g(){b(window,()=>{const e=()=>{const t={type:"iframe-resized",width:document.documentElement.scrollWidth,height:document.documentElement.scrollHeight};window.parent.postMessage(t,"*")};new ResizeObserver(e).observe(document.body)})}s.initialize=p,s.initializeChildListener=g,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"})});
|
package/dist/type.d.ts
CHANGED
|
@@ -1,21 +1,27 @@
|
|
|
1
1
|
interface Window {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
iframeResizer: {
|
|
3
|
+
initialize: InitializeFunction;
|
|
4
|
+
};
|
|
5
5
|
}
|
|
6
6
|
|
|
7
|
+
/**
|
|
8
|
+
* Automatically resizes the selected iframes.
|
|
9
|
+
* @param settings The settings for the selected iframes. The default settings properties are picked if empty.
|
|
10
|
+
* @param selector The selector for the iframe(s) or the HTMLIFrameElement to be resized. If empty, all document iframe elements will be selected.
|
|
11
|
+
* @returns A result array, which can be used to clean up the listeners if you often remove iframes from the document.
|
|
12
|
+
*/
|
|
7
13
|
export type InitializeFunction = (settings?: Partial<Settings>, selector?: string | HTMLIFrameElement) => InitializeResult[];
|
|
8
14
|
export type InitializeResult = { unsubscribe: () => void };
|
|
9
15
|
|
|
10
16
|
export type Settings = {
|
|
11
|
-
|
|
12
|
-
|
|
17
|
+
offsetSize: number;
|
|
18
|
+
checkOrigin: string[] | boolean;
|
|
13
19
|
};
|
|
14
20
|
|
|
15
21
|
export type IframeResizeEventData = {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
22
|
+
type: "iframe-resized";
|
|
23
|
+
width: number;
|
|
24
|
+
height: number;
|
|
19
25
|
};
|
|
20
26
|
|
|
21
27
|
export type IframeResizeEvent = MessageEvent<IframeResizeEventData>;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@open-iframe-resizer/core",
|
|
3
3
|
"private": false,
|
|
4
|
-
"version": "1.0.0
|
|
4
|
+
"version": "1.0.0",
|
|
5
5
|
"description": "Open-source modern iframe resizer",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"repository": {
|
|
@@ -11,7 +11,18 @@
|
|
|
11
11
|
"engines": {
|
|
12
12
|
"node": ">=20.0.0"
|
|
13
13
|
},
|
|
14
|
-
"keywords": [
|
|
14
|
+
"keywords": [
|
|
15
|
+
"iframe",
|
|
16
|
+
"Resizing",
|
|
17
|
+
"Resizer",
|
|
18
|
+
"resize",
|
|
19
|
+
"cross",
|
|
20
|
+
"same",
|
|
21
|
+
"domain",
|
|
22
|
+
"width",
|
|
23
|
+
"height",
|
|
24
|
+
"responsive"
|
|
25
|
+
],
|
|
15
26
|
"type": "module",
|
|
16
27
|
"main": "./dist/index.cjs",
|
|
17
28
|
"module": "./dist/index.js",
|
|
@@ -23,7 +34,10 @@
|
|
|
23
34
|
"require": "./dist/index.cjs"
|
|
24
35
|
}
|
|
25
36
|
},
|
|
26
|
-
"files": [
|
|
37
|
+
"files": [
|
|
38
|
+
"dist",
|
|
39
|
+
"README.md"
|
|
40
|
+
],
|
|
27
41
|
"scripts": {
|
|
28
42
|
"docs": "npm run typedoc",
|
|
29
43
|
"lint": "biome lint ./src",
|
|
@@ -34,10 +48,10 @@
|
|
|
34
48
|
"serve": "concurrently \"vite --port 5550\" \"vite --port 5551\"",
|
|
35
49
|
"test": "npm run test:unit && npm run test:e2e",
|
|
36
50
|
"test:e2e": "npx playwright install --with-deps && npx playwright test",
|
|
37
|
-
"test:unit": "vitest run"
|
|
51
|
+
"test:unit": "vitest run",
|
|
52
|
+
"prepare": "copyfiles -V ../../README.md packages/core"
|
|
38
53
|
},
|
|
39
54
|
"devDependencies": {
|
|
40
|
-
"@microsoft/tsdoc": "^0.
|
|
41
|
-
"picocolors": "^1.0.1"
|
|
55
|
+
"@microsoft/tsdoc": "^0.15.0"
|
|
42
56
|
}
|
|
43
57
|
}
|