cors-window
Advanced tools
Comparing version 0.0.0-alpha.4 to 0.0.0-alpha.5
@@ -1,13 +0,3 @@ | ||
interface DialogOptions { | ||
width: number; | ||
height: number; | ||
target?: string; | ||
centered?: boolean; | ||
xMove?: number; | ||
yMove?: number; | ||
options?: string; | ||
} | ||
declare const newDialog: (url: string, options?: DialogOptions) => Window | null; | ||
type Origin = string; | ||
declare class WindowHost<T extends object = object> { | ||
declare abstract class Host<T extends object = object> { | ||
private id; | ||
@@ -21,6 +11,6 @@ origin: Origin; | ||
isOpen(): boolean; | ||
constructor(remoteUrl: string, id?: string, options?: DialogOptions); | ||
constructor(remoteUrl: string, id?: string); | ||
post(payload: T): void; | ||
} | ||
declare class WindowDialog<T extends object = object> { | ||
declare class Listener<T extends object = object> { | ||
private id; | ||
@@ -33,8 +23,36 @@ origin: Origin; | ||
onParentClose: () => void; | ||
constructor(id: string, parent: Window | null); | ||
isOpen(): boolean; | ||
constructor(id?: string); | ||
private init; | ||
post(payload: T): void; | ||
} | ||
interface DialogOptions { | ||
width: number; | ||
height: number; | ||
target?: string; | ||
centered?: boolean; | ||
xMove?: number; | ||
yMove?: number; | ||
options?: string; | ||
} | ||
declare const newDialog: (url: string, options?: DialogOptions) => Window | null; | ||
declare class IframeHost<T extends object = object> extends Host<T> { | ||
constructor(iFrame: HTMLIFrameElement, id?: string); | ||
} | ||
declare class IframeWindow<T extends object = object> extends Listener<T> { | ||
constructor(id?: string); | ||
} | ||
/** | ||
* parent -> dialog/window | ||
*/ | ||
declare class DialogHost<T extends object = object> extends Host<T> { | ||
constructor(remoteUrl: string, id?: string, options?: DialogOptions); | ||
} | ||
/** | ||
* dialog/window -> parent | ||
*/ | ||
declare class DialogWindow<T extends object = object> extends Listener<T> { | ||
constructor(id?: string); | ||
} | ||
export { type DialogOptions, WindowDialog, WindowHost, newDialog }; | ||
export { DialogHost, type DialogOptions, DialogWindow, IframeHost, IframeWindow, newDialog }; |
@@ -1,1 +0,1 @@ | ||
var h=()=>{},p=JSON.stringify,u=JSON.parse,j=n=>{try{return u(n)}catch{return null}},r=window,O=document,w=(n,t)=>{let i=t??{width:600,height:400},s=r.screenLeft!=null?r.screenLeft:screen.left,e=r.screenTop!=null?r.screenTop:screen.top,o=r.innerWidth,l=r.innerHeight,c=O.documentElement,d=o||(c.clientWidth?c.clientWidth:screen.width),T=l||(c.clientHeight?c.clientHeight:screen.height),f=s+(i.xMove??0),y=e+(i.yMove??0);(i.centered??!0)&&(f+=d/2-i.width/2,y+=T/2-i.height/2);let C=i.target??"_blank",a=r.open(n,C,`width=${i.width}, height=${i.height} top=${y}, left=${f}, ${i.options??"resize=yes"}`);return a!=null&&a.focus&&a.focus(),a},m=n=>new URL(n).origin,x=(n,t)=>{if(n.origin!=t)throw new Error(`Invalid message origin: ${n.origin}, allowed: ${t}`)},b=(n,t,i)=>{r.addEventListener("message",s=>{let e=j(s.data);e&&e.type&&e.i===n&&(x(s,t),i(s.source,e))},!1)},g=(n,t,i,s,e)=>{let o={i,j:e,type:s};n==null||n.postMessage(p(o),t)},v=class{constructor(t,i="1",s){this.id=i;this.origin=m(t),setTimeout(()=>{this.child||(this.child=w(t,s))},250),b(this.id,this.origin,(e,o)=>{var l,c,d;o.type==="msg"&&((l=this.onMessage)==null||l.call(this,u(o.j))),o.type==="child-open"&&((c=this.onChildOpen)==null||c.call(this)),e&&this.child==null&&o.type==="ping"&&(this.child=e,(d=this.onChildAttach)==null||d.call(this),g(this.child,this.origin,i,"host-attach",""))}),setInterval(()=>{var e;if(this.child&&this.child.closed){(e=this.onChildClose)==null||e.call(this),this.child=null;return}},100)}origin;child;onMessage=h;onChildOpen=h;onChildAttach=h;onChildClose=h;isOpen(){return!!this.child&&this.child.closed==!1}post(t){g(this.child,this.origin,this.id,"msg",p(t))}},M=class{constructor(t="1"){this.id=t;setTimeout(()=>this.init(),0)}origin;parent=r.opener;onMessage=h;onParentOpen=h;onParentAttach=h;onParentClose=h;isOpen(){return!!this.parent&&this.parent.closed==!1}init(){var t;this.parent&&(this.origin=m(O.referrer),b(this.id,this.origin,(i,s)=>{var e,o;s.type==="msg"?(e=this.onMessage)==null||e.call(this,u(s.j)):s.type==="host-attach"&&((o=this.onParentAttach)==null||o.call(this))}),setInterval(()=>{var i;if(this.parent&&this.parent.closed){(i=this.onParentClose)==null||i.call(this),this.parent=null;return}g(this.parent,this.origin,this.id,"ping","")},100),(t=this.onParentOpen)==null||t.call(this),g(this.parent,this.origin,this.id,"child-open",""))}post(t){g(this.parent,this.origin,this.id,"msg",p(t))}};export{M as WindowDialog,v as WindowHost,w as newDialog}; | ||
var c=()=>{},u=JSON.stringify,f=JSON.parse,E=i=>{try{return f(i)}catch{return null}},r=window,j=document,O=i=>new URL(i).origin,$=(i,t)=>{if(i.origin!=t)throw new Error(`Invalid message origin: ${i.origin}, allowed: ${t}`)},m=(i,t,e)=>{r.addEventListener("message",n=>{let s=E(n.data);s&&s.type&&s.i===i&&($(n,t),e(n.source,s))},!1)},d=(i,t,e,n,s)=>{let o={i:e,j:s,type:n};i==null||i.postMessage(u(o),t)},g=class{constructor(t,e="1"){this.id=e;this.origin=O(t),m(this.id,this.origin,(n,s)=>{var o,l,h;s.type==="msg"&&((o=this.onMessage)==null||o.call(this,f(s.j))),s.type==="child-open"&&((l=this.onChildOpen)==null||l.call(this)),n&&this.child==null&&s.type==="ping"&&(this.child=n,(h=this.onChildAttach)==null||h.call(this),d(this.child,this.origin,e,"host-attach",""))}),setInterval(()=>{var n;if(this.child&&this.child.closed){(n=this.onChildClose)==null||n.call(this),this.child=null;return}},100)}origin;child;onMessage=c;onChildOpen=c;onChildAttach=c;onChildClose=c;isOpen(){return!!this.child&&this.child.closed==!1}post(t){d(this.child,this.origin,this.id,"msg",u(t))}},p=class{constructor(t="1",e){this.id=t;this.parent=e,setTimeout(()=>this.init(),0)}origin;parent;onMessage=c;onParentOpen=c;onParentAttach=c;onParentClose=c;isOpen(){return!!this.parent&&this.parent.closed==!1}init(){var t;this.parent&&(this.origin=O(j.referrer),m(this.id,this.origin,(e,n)=>{var s,o;n.type==="msg"?(s=this.onMessage)==null||s.call(this,f(n.j)):n.type==="host-attach"&&((o=this.onParentAttach)==null||o.call(this))}),setInterval(()=>{var e;if(this.parent&&this.parent.closed){(e=this.onParentClose)==null||e.call(this),this.parent=null;return}d(this.parent,this.origin,this.id,"ping","")},100),(t=this.onParentOpen)==null||t.call(this),d(this.parent,this.origin,this.id,"child-open",""))}post(t){d(this.parent,this.origin,this.id,"msg",u(t))}},L=(i,t)=>{let e=t??{width:600,height:400},n=r.screenLeft!=null?r.screenLeft:screen.left,s=r.screenTop!=null?r.screenTop:screen.top,o=r.innerWidth,l=r.innerHeight,h=j.documentElement,w=o||(h.clientWidth?h.clientWidth:screen.width),C=l||(h.clientHeight?h.clientHeight:screen.height),y=n+(e.xMove??0),b=s+(e.yMove??0);(e.centered??!0)&&(y+=w/2-e.width/2,b+=C/2-e.height/2);let W=e.target??"_blank",a=r.open(i,W,`width=${e.width}, height=${e.height} top=${b}, left=${y}, ${e.options??"resize=yes"}`);return a!=null&&a.focus&&a.focus(),a},T=class extends g{constructor(t,e="1"){super(t.src,e+"_i"),!this.child&&(this.child=t.contentWindow)}},v=class extends p{constructor(t="1"){super(t+"_i",r.top)}},x=class extends g{constructor(t,e="1",n){super(t,e+"_d"),setTimeout(()=>{this.child||(this.child=L(t,n))},250)}},M=class extends p{constructor(t="1"){super(t+"_d",r.opener)}};export{x as DialogHost,M as DialogWindow,T as IframeHost,v as IframeWindow,L as newDialog}; |
{ | ||
"name": "cors-window", | ||
"description": "CORS-Origin window.open() comunication", | ||
"version": "0.0.0-alpha.4", | ||
"version": "0.0.0-alpha.5", | ||
"type": "module", | ||
@@ -19,4 +19,4 @@ "author": "Paweł Jastrzębski", | ||
"dialog", | ||
"popup" | ||
"iframe" | ||
] | ||
} |
# cors-window | ||
Cross-origin window communication based on `window.postMessage()` | ||
Cross-origin window/iframe communication | ||
@@ -7,3 +7,3 @@ # Example | ||
```ts | ||
const host = new WindowHost("http://localhost:7702") | ||
const host = new DialogHost("http://localhost:7702") | ||
host.onMessage = (data) => { | ||
@@ -28,3 +28,3 @@ console.log("child sent msg", data) | ||
```ts | ||
const dialog = new WindowDialog(); | ||
const dialog = new DialogWindow(); | ||
dialog.onMessage = (data) => { | ||
@@ -45,2 +45,5 @@ console.log("parent sent message", data) | ||
}, 1500) | ||
``` | ||
``` | ||
## Examples | ||
For full examples go to [`example`](https://vscode.dev/github/PawelJastrzebski/cors-window) directory |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
11056
79
46