Comparing version 1.0.2 to 1.0.3
/*! | ||
name: iframeBridge | ||
version: 1.0.1 | ||
version: 1.0.2 | ||
file: inframejs.min.js | ||
author: web.szy | ||
buildTime: 2022/2/15 下午4:31:46 | ||
buildTime: 2022/2/15 下午5:08:25 | ||
*/ | ||
const Inframe = class { | ||
constructor({ target, whiteList, libName, debug } = {}) { | ||
constructor({ target, whiteList, libName, debug, opener } = {}) { | ||
this.setTargetIframe(target); | ||
@@ -18,2 +18,3 @@ this.setLibName(libName); | ||
this.debug = !!debug; | ||
this.opener = opener; | ||
this.handlersMap = /* @__PURE__ */ new Map(); | ||
@@ -72,3 +73,7 @@ if (!window) { | ||
if (this.targetIframe) { | ||
this.targetIframe.contentWindow.postMessage(msg, "*"); | ||
if (this.opener) { | ||
this.targetIframe.opener.postMessage(msg, "*"); | ||
} else { | ||
this.targetIframe.contentWindow.postMessage(msg, "*"); | ||
} | ||
} else { | ||
@@ -75,0 +80,0 @@ window.parent.postMessage(msg, "*"); |
/*! | ||
name: iframeBridge | ||
version: 1.0.1 | ||
version: 1.0.2 | ||
file: inframejs.min.js | ||
author: web.szy | ||
buildTime: 2022/2/15 下午4:31:46 | ||
buildTime: 2022/2/15 下午5:08:25 | ||
*/ | ||
(function(n,e){typeof exports=="object"&&typeof module!="undefined"?module.exports=e():typeof define=="function"&&define.amd?define(e):(n=typeof globalThis!="undefined"?globalThis:n||self,n.inframejs=e())})(this,function(){"use strict";return class{constructor({target:e,whiteList:t,libName:s,debug:i}={}){return this.setTargetIframe(e),this.setLibName(s),typeof t!="undefined"&&!Array.isArray(t)?(console.warn("whiteList must be a Array"),!1):(this.whiteList=t||[],this.debug=!!i,this.handlersMap=new Map,window?(window.getInframeInstace=()=>this,window.addEventListener("message",this.handleEvent.bind(this)),this.targetIframe?this.on("connected",a=>console.log(a)):(console.log("sub instance",this),this.emit("connected","sub Page connected")),this):(console.warn("inframe bridge must init on the browser"),!1))}setTargetIframe(e){typeof e=="string"?(this.targetIframe=document.querySelector(e),this.pageName="main"):typeof e!="undefined"?(this.targetIframe=e,this.pageName="main"):(this.targetIframe=null,this.pageName="sub")}setLibName(e){this.libName=e||"inframe"}on(e,t){if(this.logger(`${e} has been watched`),this.handlersMap.has(e)){const s=this.handlersMap.get(e);s.push(t),this.handlersMap.set(e,s)}else this.handlersMap.set(e,[t])}off(e){this.handlersMap.has(e)&&this.handlersMap.delete(e)}emit(e,t){const s={event:e,params:t,lib:this.libName};this.logger(`${e} has been emitted::${JSON.stringify(s)}`),this.targetIframe?this.targetIframe.contentWindow.postMessage(s,"*"):window.parent.postMessage(s,"*")}handleEvent(e){if(!e||typeof e.data!="object"||!(e.data.lib&&e.data.lib===this.libName))return;if(this.whiteList.length&&!this.whiteList.some(i=>~i.indexOf(e.origin))){console.warn("you are not access to inframe");return}const{data:t}=e;this.logger(`received data: ${JSON.stringify(t)}`),this.handlersMap.has(t.event)?this.handlersMap.get(t.event).forEach(i=>i(t)):this.logger(`handlers not find: ${JSON.stringify(t)}`)}destroy(){this.handlersMap.clear(),window.getInframeInstace=null,window.removeEventListener("message",this.handleEvent)}logger(e){if(!this.debug)return;const t=new Date,s=`${t.getHours()}:${t.getMinutes()}:${t.getSeconds()}`;console.log(`${s}-${this.pageName}::${e}`)}}}); | ||
(function(n,e){typeof exports=="object"&&typeof module!="undefined"?module.exports=e():typeof define=="function"&&define.amd?define(e):(n=typeof globalThis!="undefined"?globalThis:n||self,n.inframejs=e())})(this,function(){"use strict";return class{constructor({target:e,whiteList:t,libName:s,debug:i,opener:a}={}){return this.setTargetIframe(e),this.setLibName(s),typeof t!="undefined"&&!Array.isArray(t)?(console.warn("whiteList must be a Array"),!1):(this.whiteList=t||[],this.debug=!!i,this.opener=a,this.handlersMap=new Map,window?(window.getInframeInstace=()=>this,window.addEventListener("message",this.handleEvent.bind(this)),this.targetIframe?this.on("connected",r=>console.log(r)):(console.log("sub instance",this),this.emit("connected","sub Page connected")),this):(console.warn("inframe bridge must init on the browser"),!1))}setTargetIframe(e){typeof e=="string"?(this.targetIframe=document.querySelector(e),this.pageName="main"):typeof e!="undefined"?(this.targetIframe=e,this.pageName="main"):(this.targetIframe=null,this.pageName="sub")}setLibName(e){this.libName=e||"inframe"}on(e,t){if(this.logger(`${e} has been watched`),this.handlersMap.has(e)){const s=this.handlersMap.get(e);s.push(t),this.handlersMap.set(e,s)}else this.handlersMap.set(e,[t])}off(e){this.handlersMap.has(e)&&this.handlersMap.delete(e)}emit(e,t){const s={event:e,params:t,lib:this.libName};this.logger(`${e} has been emitted::${JSON.stringify(s)}`),this.targetIframe?this.opener?this.targetIframe.opener.postMessage(s,"*"):this.targetIframe.contentWindow.postMessage(s,"*"):window.parent.postMessage(s,"*")}handleEvent(e){if(!e||typeof e.data!="object"||!(e.data.lib&&e.data.lib===this.libName))return;if(this.whiteList.length&&!this.whiteList.some(i=>~i.indexOf(e.origin))){console.warn("you are not access to inframe");return}const{data:t}=e;this.logger(`received data: ${JSON.stringify(t)}`),this.handlersMap.has(t.event)?this.handlersMap.get(t.event).forEach(i=>i(t)):this.logger(`handlers not find: ${JSON.stringify(t)}`)}destroy(){this.handlersMap.clear(),window.getInframeInstace=null,window.removeEventListener("message",this.handleEvent)}logger(e){if(!this.debug)return;const t=new Date,s=`${t.getHours()}:${t.getMinutes()}:${t.getSeconds()}`;console.log(`${s}-${this.pageName}::${e}`)}}}); |
{ | ||
"name": "inframejs", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "use pure Javascript to connect the iframe pages", | ||
@@ -5,0 +5,0 @@ "main": "dist/inframejs.es.min.js", |
const Inframe = class { | ||
constructor({target, whiteList, libName,debug} = {}) { | ||
constructor({target, whiteList, libName,debug,opener} = {}) { | ||
this.setTargetIframe(target) | ||
@@ -11,2 +11,3 @@ this.setLibName(libName) | ||
this.debug = !!debug | ||
this.opener = opener | ||
this.handlersMap = new Map() | ||
@@ -70,3 +71,7 @@ if (!window) { | ||
if (this.targetIframe) { | ||
this.targetIframe.contentWindow.postMessage(msg, '*') | ||
if(this.opener){ | ||
this.targetIframe.opener.postMessage(msg, '*') | ||
} else { | ||
this.targetIframe.contentWindow.postMessage(msg, '*') | ||
} | ||
} else { | ||
@@ -73,0 +78,0 @@ window.parent.postMessage(msg, '*') |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12227
286