@open-iframe-resizer/core 1.0.0 → 1.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 +21 -6
- package/dist/common.d.ts +15 -8
- package/dist/compat.d.ts +2 -0
- package/dist/index.js +82 -52
- package/dist/index.umd.cjs +1 -1
- package/dist/type.d.ts +20 -0
- package/package.json +1 -2
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
## Overview
|
|
4
4
|
|
|
5
|
-
A modern, lightweight
|
|
5
|
+
A modern, lightweight alternative for resizing iframes dynamically. It is shipped under the MIT license, making it usable in commercial projects.
|
|
6
6
|
|
|
7
7
|
If you found this plugin helpful, please consider starring the repository!
|
|
8
8
|
|
|
@@ -21,18 +21,33 @@ If you found this plugin helpful, please consider starring the repository!
|
|
|
21
21
|
You can found a working example [here](https://codesandbox.io/p/sandbox/open-iframe-resize-browser-m655zt)
|
|
22
22
|
|
|
23
23
|
### Package
|
|
24
|
-
Note you can also install the package through npm:
|
|
24
|
+
Note you can also install the core package through [npm](https://www.npmjs.com/package/@open-iframe-resizer/core):
|
|
25
25
|
```bash
|
|
26
26
|
npm install @open-iframe-resizer/core
|
|
27
27
|
```
|
|
28
28
|
|
|
29
|
+
### React
|
|
30
|
+
A React component is also available:
|
|
31
|
+
```bash
|
|
32
|
+
npm install @open-iframe-resizer/react
|
|
33
|
+
```
|
|
34
|
+
|
|
29
35
|
## Notes
|
|
30
36
|
|
|
31
|
-
###
|
|
32
|
-
|
|
37
|
+
### Comparison with iframe-resizer
|
|
38
|
+
This library is very good, but it has changed its license, so it is no longer usable in closed-source projects for free.
|
|
39
|
+
I decided to replicate some parts of the API, as it may facilitate migration to this project.
|
|
40
|
+
|
|
41
|
+
Some features from this library are missing, but they could be implemented in future versions.
|
|
33
42
|
|
|
34
|
-
### Resize
|
|
35
|
-
|
|
43
|
+
### Resize iframes from a different origin
|
|
44
|
+
- If you have control over the embedded page, you 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).
|
|
45
|
+
|
|
46
|
+
- If you have no control over the child iframe domain, and, by chance, the child page loads the legacy *iframe-resizer* script, you can initialize the library with the compatibility mode; it will try to connect to the child iframe:
|
|
47
|
+
|
|
48
|
+
```javascript
|
|
49
|
+
initialize({ enableLegacyLibSupport: true }, "#my-iframe");
|
|
50
|
+
```
|
|
36
51
|
|
|
37
52
|
## Browser support
|
|
38
53
|
|
package/dist/common.d.ts
CHANGED
|
@@ -1,11 +1,18 @@
|
|
|
1
1
|
import { Settings } from './type';
|
|
2
|
-
declare const isInIframe: () => boolean;
|
|
3
|
-
declare const isSameOriginIframe: (iframe: HTMLIFrameElement) => iframe is HTMLIFrameElement & {
|
|
2
|
+
export declare const isInIframe: () => boolean;
|
|
3
|
+
export declare const isSameOriginIframe: (iframe: HTMLIFrameElement) => iframe is HTMLIFrameElement & {
|
|
4
4
|
contentDocument: Document;
|
|
5
5
|
};
|
|
6
|
-
declare const isHtmlIframeElement: (element: Element) => element is HTMLIFrameElement;
|
|
7
|
-
declare const deferWhenWindowIsLoaded: (_window: Window, executable: () => void) => void;
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
export
|
|
6
|
+
export declare const isHtmlIframeElement: (element: Element) => element is HTMLIFrameElement;
|
|
7
|
+
export declare const deferWhenWindowIsLoaded: (_window: Window, executable: () => void) => void;
|
|
8
|
+
/**
|
|
9
|
+
* Post the message twice, it assures the target to receive the message at least once
|
|
10
|
+
*/
|
|
11
|
+
export declare const safePostMessageToCrossOriginIframe: (iframe: HTMLIFrameElement, executable: () => void) => void;
|
|
12
|
+
export declare const deferWhenSameOriginIframeIsLoaded: (iframe: HTMLIFrameElement, executable: () => void) => void;
|
|
13
|
+
export declare const getDefaultSettings: () => Settings;
|
|
14
|
+
export declare const isIframeSameOrigin: (iframe: HTMLIFrameElement) => boolean;
|
|
15
|
+
export declare const extractIframeOrigin: (iframe: HTMLIFrameElement) => string | null;
|
|
16
|
+
export declare const removeUndefinedProperties: <T extends {
|
|
17
|
+
[key: string]: unknown;
|
|
18
|
+
}>(object: T) => T;
|
package/dist/compat.d.ts
ADDED
package/dist/index.js
CHANGED
|
@@ -1,98 +1,128 @@
|
|
|
1
|
-
const
|
|
1
|
+
const h = () => window && window.self !== window.top, f = (e) => e instanceof HTMLIFrameElement, b = (e, n) => {
|
|
2
2
|
e.document.readyState === "complete" ? n() : e.addEventListener("load", n);
|
|
3
3
|
}, w = (e, n) => {
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
n(), e.addEventListener("load", n);
|
|
5
|
+
}, L = (e, n) => {
|
|
6
|
+
var r, o;
|
|
7
|
+
const t = ((r = e.contentWindow) == null ? void 0 : r.document.readyState) === "complete";
|
|
6
8
|
return e.src !== "about:blank" && ((o = e.contentWindow) == null ? void 0 : o.location.href) !== "about:blank" && t ? n() : e.addEventListener("load", n);
|
|
7
|
-
},
|
|
9
|
+
}, y = () => ({ offsetSize: 0, checkOrigin: !0, enableLegacyLibSupport: !1 }), p = (e) => {
|
|
8
10
|
try {
|
|
9
11
|
return new URL(e.src, window.location.origin).origin === window.location.origin;
|
|
10
12
|
} catch {
|
|
11
13
|
return !1;
|
|
12
14
|
}
|
|
13
|
-
},
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
15
|
+
}, m = (e) => {
|
|
16
|
+
try {
|
|
17
|
+
const n = new URL(e.src).origin;
|
|
18
|
+
if (n !== "about:blank")
|
|
19
|
+
return n;
|
|
20
|
+
} catch {
|
|
21
|
+
}
|
|
22
|
+
return null;
|
|
23
|
+
}, O = (e) => (Object.keys(e).forEach((n) => e[n] === void 0 && delete e[n]), e);
|
|
24
|
+
function z(e) {
|
|
25
|
+
w(
|
|
26
|
+
e,
|
|
27
|
+
() => {
|
|
28
|
+
var n;
|
|
29
|
+
return (n = e.contentWindow) == null ? void 0 : n.postMessage(
|
|
30
|
+
"[iFrameSizer]ID:0:false:false:32:true:true::auto:::0:false:child:auto:true:::true:::false",
|
|
31
|
+
m(e) ?? "*"
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
function I(e) {
|
|
37
|
+
if (typeof e.data == "string" && e.data.startsWith("[iFrameSizer]")) {
|
|
38
|
+
const [n, t] = e.data.split(":");
|
|
39
|
+
return +t;
|
|
40
|
+
}
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
const g = W(), d = [], D = (e, n) => {
|
|
44
|
+
const t = { ...y(), ...O(e ?? {}) }, i = v(n), r = S(t, i);
|
|
45
|
+
return i.map((o) => {
|
|
46
|
+
const c = d.push({ iframe: o, settings: t }), s = E(o, t, r);
|
|
17
47
|
return {
|
|
18
48
|
unsubscribe: () => {
|
|
19
|
-
|
|
49
|
+
s(), d.splice(c - 1, 1);
|
|
20
50
|
}
|
|
21
51
|
};
|
|
22
52
|
});
|
|
23
53
|
};
|
|
24
|
-
function
|
|
25
|
-
return typeof e == "string" ? Array.from(document.querySelectorAll(e)).filter(
|
|
54
|
+
function v(e) {
|
|
55
|
+
return typeof e == "string" ? Array.from(document.querySelectorAll(e)).filter(f) : e ? f(e) ? [e] : [] : Array.from(document.getElementsByTagName("iframe"));
|
|
26
56
|
}
|
|
27
|
-
function
|
|
57
|
+
function S(e, n) {
|
|
28
58
|
if (Array.isArray(e.checkOrigin))
|
|
29
59
|
return e.checkOrigin;
|
|
30
60
|
if (!e.checkOrigin)
|
|
31
61
|
return [];
|
|
32
62
|
const t = [];
|
|
33
|
-
for (const
|
|
34
|
-
const
|
|
35
|
-
|
|
63
|
+
for (const i of n) {
|
|
64
|
+
const r = m(i);
|
|
65
|
+
r && t.push(r);
|
|
36
66
|
}
|
|
37
67
|
return t;
|
|
38
68
|
}
|
|
39
|
-
function
|
|
40
|
-
|
|
41
|
-
const n = new URL(e.src).origin;
|
|
42
|
-
if (n !== "about:blank")
|
|
43
|
-
return n;
|
|
44
|
-
} catch {
|
|
45
|
-
}
|
|
46
|
-
return null;
|
|
47
|
-
}
|
|
48
|
-
function L(e, n, t) {
|
|
49
|
-
return y(e) ? I(e) : p(e, n, t);
|
|
69
|
+
function E(e, n, t) {
|
|
70
|
+
return p(e) ? k(e) : R(e, n, t);
|
|
50
71
|
}
|
|
51
|
-
function
|
|
52
|
-
const
|
|
72
|
+
function R(e, n, t) {
|
|
73
|
+
const i = (r) => {
|
|
53
74
|
var s;
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
75
|
+
const o = !n.checkOrigin || t.includes(r.origin);
|
|
76
|
+
if (!(!(e.contentWindow === r.source) || !o)) {
|
|
77
|
+
if (((s = r.data) == null ? void 0 : s.type) === "iframe-resized") {
|
|
78
|
+
const { height: a } = r.data;
|
|
79
|
+
u({ height: a, iframe: e, settings: n });
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
if (n.enableLegacyLibSupport) {
|
|
83
|
+
const a = I(r);
|
|
84
|
+
a !== null && u({ height: a, iframe: e, settings: n });
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
57
87
|
}
|
|
58
88
|
};
|
|
59
|
-
return window.addEventListener("message",
|
|
89
|
+
return window.addEventListener("message", i), n.enableLegacyLibSupport && z(e), () => window.removeEventListener("message", i);
|
|
60
90
|
}
|
|
61
|
-
function
|
|
91
|
+
function k(e) {
|
|
62
92
|
const n = () => {
|
|
63
|
-
var
|
|
64
|
-
const t = (
|
|
93
|
+
var i;
|
|
94
|
+
const t = (i = e.contentDocument) == null ? void 0 : i.body;
|
|
65
95
|
if (!t) {
|
|
66
96
|
console.error("Unable to observe the iframe content document body");
|
|
67
97
|
return;
|
|
68
98
|
}
|
|
69
|
-
|
|
99
|
+
g.observe(t);
|
|
70
100
|
};
|
|
71
|
-
return
|
|
101
|
+
return L(e, n), () => {
|
|
72
102
|
var t;
|
|
73
|
-
(t = e.contentDocument) != null && t.body &&
|
|
103
|
+
(t = e.contentDocument) != null && t.body && g.unobserve(e.contentDocument.body), e.removeEventListener("load", n);
|
|
74
104
|
};
|
|
75
105
|
}
|
|
76
|
-
function
|
|
106
|
+
function W() {
|
|
77
107
|
const e = ({ target: n }) => {
|
|
78
|
-
var
|
|
79
|
-
const t =
|
|
80
|
-
var
|
|
81
|
-
return ((
|
|
108
|
+
var s;
|
|
109
|
+
const t = d.find((a) => {
|
|
110
|
+
var l;
|
|
111
|
+
return ((l = a.iframe.contentDocument) == null ? void 0 : l.body) === n;
|
|
82
112
|
});
|
|
83
113
|
if (!t)
|
|
84
114
|
return;
|
|
85
|
-
const { iframe:
|
|
86
|
-
o !== void 0 &&
|
|
115
|
+
const { iframe: i, settings: r } = t, { scrollHeight: o, scrollWidth: c } = ((s = i.contentDocument) == null ? void 0 : s.documentElement) ?? {};
|
|
116
|
+
o !== void 0 && c !== void 0 && u({ height: o, iframe: i, settings: r });
|
|
87
117
|
};
|
|
88
118
|
return new ResizeObserver((n) => n.forEach(e));
|
|
89
119
|
}
|
|
90
|
-
function
|
|
91
|
-
n.style.height = `${e + t.offsetSize}px`;
|
|
120
|
+
function u({ height: e, iframe: n, settings: t }) {
|
|
121
|
+
n.style.height = `${e + t.offsetSize + 1}px`;
|
|
92
122
|
}
|
|
93
|
-
|
|
94
|
-
function
|
|
95
|
-
|
|
123
|
+
h() && C();
|
|
124
|
+
function C() {
|
|
125
|
+
b(window, () => {
|
|
96
126
|
const e = () => {
|
|
97
127
|
const t = {
|
|
98
128
|
type: "iframe-resized",
|
|
@@ -105,6 +135,6 @@ function R() {
|
|
|
105
135
|
});
|
|
106
136
|
}
|
|
107
137
|
export {
|
|
108
|
-
|
|
109
|
-
|
|
138
|
+
D as initialize,
|
|
139
|
+
C as initializeChildListener
|
|
110
140
|
};
|
package/dist/index.umd.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(s,
|
|
1
|
+
(function(s,d){typeof exports=="object"&&typeof module<"u"?d(exports):typeof define=="function"&&define.amd?define(["exports"],d):(s=typeof globalThis<"u"?globalThis:s||self,d(s.iframeResizer={}))})(this,function(s){"use strict";const d=()=>window&&window.self!==window.top,g=e=>e instanceof HTMLIFrameElement,p=(e,n)=>{e.document.readyState==="complete"?n():e.addEventListener("load",n)},L=(e,n)=>{n(),e.addEventListener("load",n)},w=(e,n)=>{var i,o;const t=((i=e.contentWindow)==null?void 0:i.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)},O=()=>({offsetSize:0,checkOrigin:!0,enableLegacyLibSupport:!1}),z=e=>{try{return new URL(e.src,window.location.origin).origin===window.location.origin}catch{return!1}},m=e=>{try{const n=new URL(e.src).origin;if(n!=="about:blank")return n}catch{}return null},I=e=>(Object.keys(e).forEach(n=>e[n]===void 0&&delete e[n]),e);function v(e){L(e,()=>{var n;return(n=e.contentWindow)==null?void 0:n.postMessage("[iFrameSizer]ID:0:false:false:32:true:true::auto:::0:false:child:auto:true:::true:::false",m(e)??"*")})}function S(e){if(typeof e.data=="string"&&e.data.startsWith("[iFrameSizer]")){const[n,t]=e.data.split(":");return+t}return null}const h=D(),l=[],E=(e,n)=>{const t={...O(),...I(e??{})},r=R(n),i=k(t,r);return r.map(o=>{const u=l.push({iframe:o,settings:t}),a=W(o,t,i);return{unsubscribe:()=>{a(),l.splice(u-1,1)}}})};function R(e){return typeof e=="string"?Array.from(document.querySelectorAll(e)).filter(g):e?g(e)?[e]:[]:Array.from(document.getElementsByTagName("iframe"))}function k(e,n){if(Array.isArray(e.checkOrigin))return e.checkOrigin;if(!e.checkOrigin)return[];const t=[];for(const r of n){const i=m(r);i&&t.push(i)}return t}function W(e,n,t){return z(e)?T(e):C(e,n,t)}function C(e,n,t){const r=i=>{var a;const o=!n.checkOrigin||t.includes(i.origin);if(!(!(e.contentWindow===i.source)||!o)){if(((a=i.data)==null?void 0:a.type)==="iframe-resized"){const{height:c}=i.data;f({height:c,iframe:e,settings:n});return}if(n.enableLegacyLibSupport){const c=S(i);c!==null&&f({height:c,iframe:e,settings:n});return}}};return window.addEventListener("message",r),n.enableLegacyLibSupport&&v(e),()=>window.removeEventListener("message",r)}function T(e){const n=()=>{var r;const t=(r=e.contentDocument)==null?void 0:r.body;if(!t){console.error("Unable to observe the iframe content document body");return}h.observe(t)};return w(e,n),()=>{var t;(t=e.contentDocument)!=null&&t.body&&h.unobserve(e.contentDocument.body),e.removeEventListener("load",n)}}function D(){const e=({target:n})=>{var a;const t=l.find(c=>{var y;return((y=c.iframe.contentDocument)==null?void 0:y.body)===n});if(!t)return;const{iframe:r,settings:i}=t,{scrollHeight:o,scrollWidth:u}=((a=r.contentDocument)==null?void 0:a.documentElement)??{};o!==void 0&&u!==void 0&&f({height:o,iframe:r,settings:i})};return new ResizeObserver(n=>n.forEach(e))}function f({height:e,iframe:n,settings:t}){n.style.height=`${e+t.offsetSize+1}px`}d()&&b();function b(){p(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=E,s.initializeChildListener=b,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"})});
|
package/dist/type.d.ts
CHANGED
|
@@ -14,8 +14,28 @@ export type InitializeFunction = (settings?: Partial<Settings>, selector?: strin
|
|
|
14
14
|
export type InitializeResult = { unsubscribe: () => void };
|
|
15
15
|
|
|
16
16
|
export type Settings = {
|
|
17
|
+
/**
|
|
18
|
+
* Offset added to the resize size (in pixels).
|
|
19
|
+
*
|
|
20
|
+
* Default: 0
|
|
21
|
+
*/
|
|
17
22
|
offsetSize: number;
|
|
23
|
+
/**
|
|
24
|
+
* Specifies whether to check the origin of incoming messages.
|
|
25
|
+
* Accepts an array of allowed origins or a boolean.
|
|
26
|
+
* If true, incoming messages are allowed from the origins of the registered iframes.
|
|
27
|
+
*
|
|
28
|
+
* Default: true
|
|
29
|
+
*/
|
|
18
30
|
checkOrigin: string[] | boolean;
|
|
31
|
+
/**
|
|
32
|
+
* Allows the library to communicate with a cross-origin child iframe
|
|
33
|
+
* containing the original "iframe-resizer" script.
|
|
34
|
+
* Useful if you do not control the child domain.
|
|
35
|
+
*
|
|
36
|
+
* Default: false
|
|
37
|
+
*/
|
|
38
|
+
enableLegacyLibSupport: boolean;
|
|
19
39
|
};
|
|
20
40
|
|
|
21
41
|
export type 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.
|
|
4
|
+
"version": "1.1.0",
|
|
5
5
|
"description": "Open-source modern iframe resizer",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"repository": {
|
|
@@ -43,7 +43,6 @@
|
|
|
43
43
|
"lint": "biome lint ./src",
|
|
44
44
|
"build": "tsc && npm run lint && vite build && typedoc",
|
|
45
45
|
"build:watch": "vite build --watch && typedoc",
|
|
46
|
-
"build:usecases-html": "node scripts/generate-use-cases-html.js",
|
|
47
46
|
"dev": "concurrently \"vite build --watch\" \"npm run serve\"",
|
|
48
47
|
"serve": "concurrently \"vite --port 5550\" \"vite --port 5551\"",
|
|
49
48
|
"test": "npm run test:unit && npm run test:e2e",
|