cors-window
Advanced tools
Comparing version 0.0.0-alpha.1 to 0.0.0-alpha.2
@@ -6,29 +6,34 @@ interface DialogOptions { | ||
centered?: boolean; | ||
moveRight?: number; | ||
moveDown?: number; | ||
xMove?: number; | ||
yMove?: number; | ||
options?: string; | ||
} | ||
declare const newDialog: (url: string, options?: DialogOptions) => Window | null; | ||
type Origin = string; | ||
declare class WindowHost { | ||
private id; | ||
childOrigin: string; | ||
childWindow: Window | null; | ||
origin: Origin; | ||
child: Window | null; | ||
onMessage: <T extends object>(data: T) => void; | ||
onChildOpen: () => void; | ||
onChildAttach: () => void; | ||
onChildClose: () => void; | ||
isConnected(): boolean; | ||
isOpen(): boolean; | ||
constructor(remoteUrl: string, id?: string, options?: DialogOptions); | ||
postMessage<T extends object>(data: T): void; | ||
post<T extends object>(message: T): void; | ||
} | ||
declare class WindowDialog { | ||
private id; | ||
parentOrigin: string; | ||
opener: Window | null; | ||
origin: Origin; | ||
parent: Window | null; | ||
onMessage: <T extends object>(data: T) => void; | ||
onAttache: () => void; | ||
onParentOpen: () => void; | ||
onParentAttach: () => void; | ||
onParentClose: () => void; | ||
isConnected(): boolean; | ||
isOpen(): boolean; | ||
constructor(id?: string); | ||
postMessage<T extends object>(data: T): void; | ||
private init; | ||
post<T extends object>(message: T): void; | ||
} | ||
export { type DialogOptions, WindowDialog, WindowHost, newDialog }; |
@@ -1,1 +0,1 @@ | ||
var f=(t,e)=>{let n=e??{width:600,height:400},s=window.screenLeft!=null?window.screenLeft:screen.left,i=window.screenTop!=null?window.screenTop:screen.top,r=window.innerWidth?window.innerWidth:document.documentElement.clientWidth?document.documentElement.clientWidth:screen.width,h=window.innerHeight?window.innerHeight:document.documentElement.clientHeight?document.documentElement.clientHeight:screen.height,a=0,c=0;n.centered??!0?(a=r/2-n.width/2+s,c=h/2-n.height/2+i):(a=s+(n.moveRight??0),c=i+(n.moveDown??0));let u=n.target??"_blank",d=window.open(t,u,`width=${n.width}, height=${n.height} top=${c}, left=${a}, ${n.options??"resize=yes"}`);return d!=null&&d.focus&&d.focus(),d},o=class t{constructor(e,n,s){this.id=e;this.json=n;this.type=s}parseJson(){return JSON.parse(this.json)}post(e,n){e&&e.postMessage(JSON.stringify(this),n)}static newPing(e){return new t(e,"","ping")}static newAttache(e){return new t(e,"","attached")}static newMsg(e,n){return new t(e,JSON.stringify(n),"msg")}static unwrap(e){let n=JSON.parse(e);return"type"in n&&"json"in n?Object.assign(new t("null","","msg"),n):null}},p=t=>new URL(t).origin,w=(t,e)=>{if(t.origin!=e)throw new Error(`Invalid message origin: ${t.origin}, allowed: ${e}`)},l=class{constructor(e,n="single",s){this.id=n;this.childOrigin=p(e),setTimeout(()=>{this.childWindow||(this.childWindow=f(e,s))},250),window.addEventListener("message",i=>{var h;w(i,this.childOrigin);let r=o.unwrap(i.data);r&&r.id==this.id&&(r.type==="msg"&&((h=this.onMessage)==null||h.call(this,r.parseJson())),this.childWindow==null&&r.type==="ping"&&(this.childWindow=i.source,o.newAttache(n).post(this.childWindow,this.childOrigin)))},!1),setInterval(()=>{var i;if(this.childWindow&&this.childWindow.closed){(i=this.onChildClose)==null||i.call(this),this.childWindow=null;return}},100)}childOrigin;childWindow;onMessage=e=>{};onChildClose=()=>{};isConnected(){return!!this.childWindow&&this.childWindow.closed==!1}postMessage(e){o.newMsg(this.id,e).post(this.childWindow,this.childOrigin)}},g=class{constructor(e="single"){this.id=e;this.opener&&(this.parentOrigin=p(window.document.referrer),window.addEventListener("message",n=>{var i,r;w(n,this.parentOrigin);let s=o.unwrap(n.data);s&&s.id==this.id&&(s.type==="msg"?(i=this.onMessage)==null||i.call(this,s.parseJson()):s.type==="attached"&&((r=this.onAttache)==null||r.call(this)))},!1),setInterval(()=>{var n;if(this.opener&&this.opener.closed){(n=this.onParentClose)==null||n.call(this),this.opener=null;return}o.newPing(this.id).post(this.opener,this.parentOrigin)},100))}parentOrigin=window.document.referrer;opener=window.opener;onMessage=e=>{};onAttache=()=>{};onParentClose=()=>{};isConnected(){return!!this.opener&&this.opener.closed==!1}postMessage(e){o.newMsg(this.id,e).post(this.opener,this.parentOrigin)}};export{g as WindowDialog,l as WindowHost,f as newDialog}; | ||
var h=()=>{},p=JSON.stringify,u=JSON.parse,r=window,O=document,C=(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,a=r.innerHeight,c=O.documentElement,d=o||(c.clientWidth?c.clientWidth:screen.width),b=a||(c.clientHeight?c.clientHeight:screen.height),f=s+(i.xMove??0),v=e+(i.yMove??0);(i.centered??!0)&&(f+=d/2-i.width/2,v+=b/2-i.height/2);let x=i.target??"_blank",l=r.open(n,x,`width=${i.width}, height=${i.height} top=${v}, left=${f}, ${i.options??"resize=yes"}`);return l!=null&&l.focus&&l.focus(),l},T=n=>new URL(n).origin,j=(n,t)=>{if(n.origin!=t)throw new Error(`Invalid message origin: ${n.origin}, allowed: ${t}`)},m=(n,t,i)=>{r.addEventListener("message",s=>{j(s,t);let e=u(s.data);e&&e.i===n&&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)},y=class{constructor(t,i="1",s){this.id=i;this.origin=T(t),setTimeout(()=>{this.child||(this.child=C(t,s))},250),m(this.id,this.origin,(e,o)=>{var a,c,d;o.type==="msg"&&((a=this.onMessage)==null||a.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=T(O.referrer),m(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,y as WindowHost,C as newDialog}; |
{ | ||
"name": "cors-window", | ||
"description": "CORS-Origin window.open() comunication", | ||
"version": "0.0.0-alpha.1", | ||
"version": "0.0.0-alpha.2", | ||
"type": "module", | ||
@@ -6,0 +6,0 @@ "author": "Paweł Jastrzębski", |
@@ -7,11 +7,17 @@ # cors-window | ||
```ts | ||
const host = new WindowHost("http://localhost:4030"); | ||
const host = new WindowHost("http://localhost:7702") | ||
host.onMessage = (data) => { | ||
console.log("child sent msg", data) | ||
console.log("child sent msg", data) | ||
} | ||
host.onChildAttach = () => { | ||
console.log("host has attached to existing window") | ||
} | ||
host.onChildOpen = () => { | ||
console.log("child opened a new window") | ||
} | ||
host.onChildClose = () => { | ||
console.log("child window closed") | ||
console.log("child window closed") | ||
} | ||
setInterval(() => { | ||
host.postMessage({ type: "Ok", data: "data from host" }) | ||
host.post({ type: "ok", data: "data from host" }) | ||
}, 1500) | ||
@@ -21,11 +27,13 @@ ``` | ||
### Dialog | ||
```ts | ||
const dialog = new WindowDialog(); | ||
dialog.onAttache = () => { | ||
console.log("parent refreshed the window") | ||
} | ||
dialog.onMessage = (data) => { | ||
console.log("parent sent message", data) | ||
} | ||
dialog.onParentOpen = () => { | ||
console.log("parent opend this window") | ||
} | ||
dialog.onParentAttach = () => { | ||
console.log("parent refreshed the window") | ||
} | ||
dialog.onParentClose = () => { | ||
@@ -35,4 +43,4 @@ console.log("parent closed the window") | ||
setInterval(() => { | ||
dialog.postMessage({ type: "ok" }) | ||
dialog.post({ type: "ok", data: "data from dialog" }) | ||
}, 1500) | ||
``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
59
43
8918
6