diff --git a/demo/index.html b/demo/index.html index cc556ac..7b6906b 100644 --- a/demo/index.html +++ b/demo/index.html @@ -128,7 +128,7 @@

StreamBIM widget demo

} - StreamBIM.connectToParent(window, { + StreamBIM.connectToParent(window.parent, { pickedObject: function (result) { logEvent('Clicked object "' + result.guid + '" at (' + result.point[0].toFixed(2) + ', ' + result.point[1].toFixed(2) + ', ' + result.point[2].toFixed(2) + ')'); document.getElementById('guidInput').value = result.guid; diff --git a/dist/streambim-widget-api.min.js b/dist/streambim-widget-api.min.js index 5b00811..d14e7d6 100644 --- a/dist/streambim-widget-api.min.js +++ b/dist/streambim-widget-api.min.js @@ -1 +1 @@ -(()=>{"use strict";var e={691:(e,t,n)=>{var s,r,o,a=class extends Error{code;constructor(e,t){super(t),this.name="PenpalError",this.code=e}},i=e=>({name:e.name,message:e.message,stack:e.stack,penpalCode:e instanceof a?e.code:void 0}),d=Symbol("Reply"),l=class{value;transferables;#e=d;constructor(e,t){this.value=e,this.transferables=t?.transferables}},c="penpal",h=e=>"object"==typeof e&&null!==e,m=e=>"function"==typeof e,g=e=>"SYN"===e.type,p=e=>"ACK1"===e.type,u=e=>"ACK2"===e.type,f=e=>"CALL"===e.type,w=e=>"REPLY"===e.type,v=(e,t=[])=>{const n=[];for(const s of Object.keys(e)){const r=e[s];m(r)?n.push([...t,s]):h(r)&&n.push(...v(r,[...t,s]))}return n},y=e=>e.join("."),M=(e,t,n)=>({namespace:c,channel:e,type:"REPLY",callId:t,isError:!0,...n instanceof Error?{value:i(n),isSerializedErrorInstance:!0}:{value:n}}),E=(e,t,n,s)=>{let r=!1;const o=async o=>{if(r)return;if(!f(o))return;s?.(`Received ${y(o.methodPath)}() call`,o);const{methodPath:i,args:d,id:g}=o;let p,u;try{const e=((e,t)=>{const n=e.reduce(((e,t)=>h(e)?e[t]:void 0),t);return m(n)?n:void 0})(i,t);if(!e)throw new a("METHOD_NOT_FOUND",`Method \`${y(i)}\` is not found.`);let s=await e(...d);s instanceof l&&(u=s.transferables,s=await s.value),p={namespace:c,channel:n,type:"REPLY",callId:g,value:s}}catch(e){p=M(n,g,e)}if(!r)try{s?.(`Sending ${y(i)}() reply`,p),e.sendMessage(p,u)}catch(t){throw"DataCloneError"===t.name&&(p=M(n,g,t),s?.(`Sending ${y(i)}() reply`,p),e.sendMessage(p)),t}};return e.addMessageHandler(o),()=>{r=!0,e.removeMessageHandler(o)}},O=crypto.randomUUID?.bind(crypto)??(()=>new Array(4).fill(0).map((()=>Math.floor(Math.random()*Number.MAX_SAFE_INTEGER).toString(16))).join("-")),I=Symbol("CallOptions"),P=class{transferables;timeout;#e=I;constructor(e){this.transferables=e?.transferables,this.timeout=e?.timeout}},S=new Set(["apply","call","bind"]),R=(e,t,n=[])=>new Proxy(n.length?()=>{}:Object.create(null),{get(s,r){if("then"!==r)return n.length&&S.has(r)?Reflect.get(s,r):R(e,t,[...n,r])},apply:(t,s,r)=>e(n,r)}),A=e=>new a("CONNECTION_DESTROYED",`Method call ${y(e)}() failed due to destroyed connection`),C=(e,t,n)=>{let s=!1;const r=new Map,o=e=>{if(!w(e))return;const{callId:t,value:s,isError:o,isSerializedErrorInstance:i}=e,d=r.get(t);d&&(r.delete(t),n?.(`Received ${y(d.methodPath)}() call`,e),o?d.reject(i?(({name:e,message:t,stack:n,penpalCode:s})=>{const r=s?new a(s,t):new Error(t);return r.name=e,r.stack=n,r})(s):s):d.resolve(s))};return e.addMessageHandler(o),{remoteProxy:R(((o,i)=>{if(s)throw A(o);const d=O(),l=i[i.length-1],h=l instanceof P,{timeout:m,transferables:g}=h?l:{},p=h?i.slice(0,-1):i;return new Promise(((s,i)=>{const l=void 0!==m?window.setTimeout((()=>{r.delete(d),i(new a("METHOD_CALL_TIMEOUT",`Method call ${y(o)}() timed out after ${m}ms`))}),m):void 0;r.set(d,{methodPath:o,resolve:s,reject:i,timeoutId:l});try{const s={namespace:c,channel:t,type:"CALL",id:d,methodPath:o,args:p};n?.(`Sending ${y(o)}() call`,s),e.sendMessage(s,g)}catch(e){i(new a("TRANSMISSION_FAILED",e.message))}}))}),n),destroy:()=>{s=!0,e.removeMessageHandler(o);for(const{methodPath:e,reject:t,timeoutId:n}of r.values())clearTimeout(n),t(A(e));r.clear()}}},N=class extends Error{constructor(e){super(`You've hit a bug in Penpal. Please file an issue with the following information: ${e}`)}},b="deprecated-penpal",L=e=>e.join("."),T=e=>new N(`Unexpected message to translate: ${JSON.stringify(e)}`),D=({messenger:e,methods:t,timeout:n,channel:s,log:r})=>{const o=O();let i;const d=[];let l=!1;const h=v(t),{promise:m,resolve:f,reject:w}=(()=>{let e,t;return{promise:new Promise(((n,s)=>{e=n,t=s})),resolve:e,reject:t}})(),y=void 0!==n?setTimeout((()=>{w(new a("CONNECTION_TIMEOUT",`Connection timed out after ${n}ms`))}),n):void 0,M=()=>{for(const e of d)e()},I=()=>{if(l)return;d.push(E(e,t,s,r));const{remoteProxy:n,destroy:o}=C(e,s,r);d.push(o),clearTimeout(y),l=!0,f({remoteProxy:n,destroy:M})},P=()=>{const t={namespace:c,type:"SYN",channel:s,participantId:o};r?.("Sending handshake SYN",t);try{e.sendMessage(t)}catch(e){w(new a("TRANSMISSION_FAILED",e.message))}},S=t=>{g(t)&&(t=>{if(r?.("Received handshake SYN",t),t.participantId===i&&i!==b)return;if(i=t.participantId,P(),!(o>i||i===b))return;const n={namespace:c,channel:s,type:"ACK1",methodPaths:h};r?.("Sending handshake ACK1",n);try{e.sendMessage(n)}catch(e){return void w(new a("TRANSMISSION_FAILED",e.message))}})(t),p(t)&&(t=>{r?.("Received handshake ACK1",t);const n={namespace:c,channel:s,type:"ACK2"};r?.("Sending handshake ACK2",n);try{e.sendMessage(n)}catch(e){return void w(new a("TRANSMISSION_FAILED",e.message))}I()})(t),u(t)&&(e=>{r?.("Received handshake ACK2",e),I()})(t)};return e.addMessageHandler(S),d.push((()=>e.removeMessageHandler(S))),P(),m},_=new WeakSet,k=({messenger:e,methods:t={},timeout:n,channel:s,log:r})=>{if(!e)throw new a("INVALID_ARGUMENT","messenger must be defined");if(_.has(e))throw new a("INVALID_ARGUMENT","A messenger can only be used for a single connection");_.add(e);const o=[e.destroy],i=(e=>{let t,n=!1;return(...s)=>(n||(n=!0,t=e(...s)),t)})((t=>{if(t){const t={namespace:c,channel:s,type:"DESTROY"};try{e.sendMessage(t)}catch(e){}}for(const e of o)e();r?.("Connection destroyed")})),d=e=>(e=>h(e)&&e.namespace===c)(e)&&e.channel===s;return{promise:(async()=>{try{e.initialize({log:r,validateReceivedMessage:d}),e.addMessageHandler((e=>{(e=>"DESTROY"===e.type)(e)&&i(!1)}));const{remoteProxy:a,destroy:l}=await D({messenger:e,methods:t,timeout:n,channel:s,log:r});return o.push(l),a}catch(e){throw i(!0),e}})(),destroy:()=>{i(!0)}}},x=class{#t;#n;#s;#r;#o;#a=new Set;#i;#d=!1;constructor({remoteWindow:e,allowedOrigins:t}){if(!e)throw new a("INVALID_ARGUMENT","remoteWindow must be defined");this.#t=e,this.#n=t?.length?t:[window.origin]}initialize=({log:e,validateReceivedMessage:t})=>{this.#s=e,this.#r=t,window.addEventListener("message",this.#l)};sendMessage=(e,t)=>{if(g(e)){const n=this.#c(e);this.#t.postMessage(e,{targetOrigin:n,transfer:t})}else if(p(e)||this.#d){const n=this.#d?(e=>{if(p(e))return{penpal:"synAck",methodNames:e.methodPaths.map(L)};if(f(e))return{penpal:"call",id:e.id,methodName:L(e.methodPath),args:e.args};if(w(e))return e.isError?{penpal:"reply",id:e.callId,resolution:"rejected",...e.isSerializedErrorInstance?{returnValue:e.value,returnValueIsError:!0}:{returnValue:e.value}}:{penpal:"reply",id:e.callId,resolution:"fulfilled",returnValue:e.value};throw T(e)})(e):e,s=this.#c(e);this.#t.postMessage(n,{targetOrigin:s,transfer:t})}else if(u(e)){const{port1:n,port2:s}=new MessageChannel;this.#i=n,n.addEventListener("message",this.#h),n.start();const r=[s,...t||[]],o=this.#c(e);this.#t.postMessage(e,{targetOrigin:o,transfer:r})}else{if(!this.#i)throw new N("Port is undefined");this.#i.postMessage(e,{transfer:t})}};addMessageHandler=e=>{this.#a.add(e)};removeMessageHandler=e=>{this.#a.delete(e)};destroy=()=>{window.removeEventListener("message",this.#l),this.#m(),this.#a.clear()};#g=e=>this.#n.some((t=>t instanceof RegExp?t.test(e):t===e||"*"===t));#c=e=>{if(g(e))return"*";if(!this.#o)throw new N("Concrete remote origin not set");return"null"===this.#o&&this.#n.includes("*")?"*":this.#o};#m=()=>{this.#i?.removeEventListener("message",this.#h),this.#i?.close(),this.#i=void 0};#l=({source:e,origin:t,ports:n,data:s})=>{if(e===this.#t&&((e=>h(e)&&"penpal"in e)(s)&&(this.#s?.("Please upgrade the child window to the latest version of Penpal."),this.#d=!0,s=(e=>{if("syn"===e.penpal)return{namespace:c,channel:void 0,type:"SYN",participantId:b};if("ack"===e.penpal)return{namespace:c,channel:void 0,type:"ACK2"};if("call"===e.penpal)return{namespace:c,channel:void 0,type:"CALL",id:e.id,methodPath:(t=e.methodName,t.split(".")),args:e.args};var t;if("reply"===e.penpal)return"fulfilled"===e.resolution?{namespace:c,channel:void 0,type:"REPLY",callId:e.id,value:e.returnValue}:{namespace:c,channel:void 0,type:"REPLY",callId:e.id,isError:!0,...e.returnValueIsError?{value:e.returnValue,isSerializedErrorInstance:!0}:{value:e.returnValue}};throw T(e)})(s)),this.#r?.(s)))if(this.#g(t)){if(g(s)&&(this.#m(),this.#o=t),u(s)&&!this.#d){if(this.#i=n[0],!this.#i)throw new N("No port received on ACK2");this.#i.addEventListener("message",this.#h),this.#i.start()}for(const e of this.#a)e(s)}else this.#s?.(`Received a message from origin \`${t}\` which did not match allowed origins \`[${this.#n.join(", ")}]\``)};#h=({data:e})=>{if(this.#r?.(e))for(const t of this.#a)t(e)}};e=n.hmd(e),s=window,r="StreamBIM",o=function(){return{connectToParent(e,t={}){const n=e||window.parent,s=new x({remoteWindow:n,allowedOrigins:[n.origin,new URL(document.referrer).origin]});return this._connection=k({messenger:s,methods:t}),this._connection.promise.then((e=>{this.API=e}))},connectToChild(e,t={}){const n=new x({remoteWindow:e.contentWindow,allowedOrigins:[new URL(e.src).origin]});return this._connection=k({messenger:n,methods:t}),this._connection.promise.then((e=>{this.API=e}))},connectToWindow(e,t,n={}){const s=new x({remoteWindow:e,allowedOrigins:[new URL(t).origin]});return this._connection=k({messenger:s,methods:n}),this._connection.promise.then((e=>{this.API=e}))}}},e.exports?e.exports=o():"function"==typeof define&&n.amdO?define(r,o):s[r]=o()}},t={};function n(s){var r=t[s];if(void 0!==r)return r.exports;var o=t[s]={id:s,loaded:!1,exports:{}};return e[s](o,o.exports,n),o.loaded=!0,o.exports}n.amdO={},n.hmd=e=>((e=Object.create(e)).children||(e.children=[]),Object.defineProperty(e,"exports",{enumerable:!0,set:()=>{throw new Error("ES Modules may not assign module.exports or exports.*, Use ESM export syntax, instead: "+e.id)}}),e),n(691)})(); \ No newline at end of file +(()=>{"use strict";var e={691:(e,t,s)=>{var n,r,o,a=class extends Error{code;constructor(e,t){super(t),this.name="PenpalError",this.code=e}},i=e=>({name:e.name,message:e.message,stack:e.stack,penpalCode:e instanceof a?e.code:void 0}),d=Symbol("Reply"),l=class{value;transferables;#e=d;constructor(e,t){this.value=e,this.transferables=t?.transferables}},c="penpal",h=e=>"object"==typeof e&&null!==e,m=e=>"function"==typeof e,g=e=>"SYN"===e.type,p=e=>"ACK1"===e.type,u=e=>"ACK2"===e.type,f=e=>"CALL"===e.type,w=e=>"REPLY"===e.type,v=(e,t=[])=>{const s=[];for(const n of Object.keys(e)){const r=e[n];m(r)?s.push([...t,n]):h(r)&&s.push(...v(r,[...t,n]))}return s},y=e=>e.join("."),M=(e,t,s)=>({namespace:c,channel:e,type:"REPLY",callId:t,isError:!0,...s instanceof Error?{value:i(s),isSerializedErrorInstance:!0}:{value:s}}),E=(e,t,s,n)=>{let r=!1;const o=async o=>{if(r)return;if(!f(o))return;n?.(`Received ${y(o.methodPath)}() call`,o);const{methodPath:i,args:d,id:g}=o;let p,u;try{const e=((e,t)=>{const s=e.reduce(((e,t)=>h(e)?e[t]:void 0),t);return m(s)?s:void 0})(i,t);if(!e)throw new a("METHOD_NOT_FOUND",`Method \`${y(i)}\` is not found.`);let n=await e(...d);n instanceof l&&(u=n.transferables,n=await n.value),p={namespace:c,channel:s,type:"REPLY",callId:g,value:n}}catch(e){p=M(s,g,e)}if(!r)try{n?.(`Sending ${y(i)}() reply`,p),e.sendMessage(p,u)}catch(t){throw"DataCloneError"===t.name&&(p=M(s,g,t),n?.(`Sending ${y(i)}() reply`,p),e.sendMessage(p)),t}};return e.addMessageHandler(o),()=>{r=!0,e.removeMessageHandler(o)}},O=crypto.randomUUID?.bind(crypto)??(()=>new Array(4).fill(0).map((()=>Math.floor(Math.random()*Number.MAX_SAFE_INTEGER).toString(16))).join("-")),I=Symbol("CallOptions"),P=class{transferables;timeout;#e=I;constructor(e){this.transferables=e?.transferables,this.timeout=e?.timeout}},S=new Set(["apply","call","bind"]),R=(e,t,s=[])=>new Proxy(s.length?()=>{}:Object.create(null),{get(n,r){if("then"!==r)return s.length&&S.has(r)?Reflect.get(n,r):R(e,t,[...s,r])},apply:(t,n,r)=>e(s,r)}),A=e=>new a("CONNECTION_DESTROYED",`Method call ${y(e)}() failed due to destroyed connection`),C=(e,t,s)=>{let n=!1;const r=new Map,o=e=>{if(!w(e))return;const{callId:t,value:n,isError:o,isSerializedErrorInstance:i}=e,d=r.get(t);d&&(r.delete(t),s?.(`Received ${y(d.methodPath)}() call`,e),o?d.reject(i?(({name:e,message:t,stack:s,penpalCode:n})=>{const r=n?new a(n,t):new Error(t);return r.name=e,r.stack=s,r})(n):n):d.resolve(n))};return e.addMessageHandler(o),{remoteProxy:R(((o,i)=>{if(n)throw A(o);const d=O(),l=i[i.length-1],h=l instanceof P,{timeout:m,transferables:g}=h?l:{},p=h?i.slice(0,-1):i;return new Promise(((n,i)=>{const l=void 0!==m?window.setTimeout((()=>{r.delete(d),i(new a("METHOD_CALL_TIMEOUT",`Method call ${y(o)}() timed out after ${m}ms`))}),m):void 0;r.set(d,{methodPath:o,resolve:n,reject:i,timeoutId:l});try{const n={namespace:c,channel:t,type:"CALL",id:d,methodPath:o,args:p};s?.(`Sending ${y(o)}() call`,n),e.sendMessage(n,g)}catch(e){i(new a("TRANSMISSION_FAILED",e.message))}}))}),s),destroy:()=>{n=!0,e.removeMessageHandler(o);for(const{methodPath:e,reject:t,timeoutId:s}of r.values())clearTimeout(s),t(A(e));r.clear()}}},N=class extends Error{constructor(e){super(`You've hit a bug in Penpal. Please file an issue with the following information: ${e}`)}},b="deprecated-penpal",L=e=>e.join("."),T=e=>new N(`Unexpected message to translate: ${JSON.stringify(e)}`),D=({messenger:e,methods:t,timeout:s,channel:n,log:r})=>{const o=O();let i;const d=[];let l=!1;const h=v(t),{promise:m,resolve:f,reject:w}=(()=>{let e,t;return{promise:new Promise(((s,n)=>{e=s,t=n})),resolve:e,reject:t}})(),y=void 0!==s?setTimeout((()=>{w(new a("CONNECTION_TIMEOUT",`Connection timed out after ${s}ms`))}),s):void 0,M=()=>{for(const e of d)e()},I=()=>{if(l)return;d.push(E(e,t,n,r));const{remoteProxy:s,destroy:o}=C(e,n,r);d.push(o),clearTimeout(y),l=!0,f({remoteProxy:s,destroy:M})},P=()=>{const t={namespace:c,type:"SYN",channel:n,participantId:o};r?.("Sending handshake SYN",t);try{e.sendMessage(t)}catch(e){w(new a("TRANSMISSION_FAILED",e.message))}},S=t=>{g(t)&&(t=>{if(r?.("Received handshake SYN",t),t.participantId===i&&i!==b)return;if(i=t.participantId,P(),!(o>i||i===b))return;const s={namespace:c,channel:n,type:"ACK1",methodPaths:h};r?.("Sending handshake ACK1",s);try{e.sendMessage(s)}catch(e){return void w(new a("TRANSMISSION_FAILED",e.message))}})(t),p(t)&&(t=>{r?.("Received handshake ACK1",t);const s={namespace:c,channel:n,type:"ACK2"};r?.("Sending handshake ACK2",s);try{e.sendMessage(s)}catch(e){return void w(new a("TRANSMISSION_FAILED",e.message))}I()})(t),u(t)&&(e=>{r?.("Received handshake ACK2",e),I()})(t)};return e.addMessageHandler(S),d.push((()=>e.removeMessageHandler(S))),P(),m},_=new WeakSet,k=({messenger:e,methods:t={},timeout:s,channel:n,log:r})=>{if(!e)throw new a("INVALID_ARGUMENT","messenger must be defined");if(_.has(e))throw new a("INVALID_ARGUMENT","A messenger can only be used for a single connection");_.add(e);const o=[e.destroy],i=(e=>{let t,s=!1;return(...n)=>(s||(s=!0,t=e(...n)),t)})((t=>{if(t){const t={namespace:c,channel:n,type:"DESTROY"};try{e.sendMessage(t)}catch(e){}}for(const e of o)e();r?.("Connection destroyed")})),d=e=>(e=>h(e)&&e.namespace===c)(e)&&e.channel===n;return{promise:(async()=>{try{e.initialize({log:r,validateReceivedMessage:d}),e.addMessageHandler((e=>{(e=>"DESTROY"===e.type)(e)&&i(!1)}));const{remoteProxy:a,destroy:l}=await D({messenger:e,methods:t,timeout:s,channel:n,log:r});return o.push(l),a}catch(e){throw i(!0),e}})(),destroy:()=>{i(!0)}}},x=class{#t;#s;#n;#r;#o;#a=new Set;#i;#d=!1;constructor({remoteWindow:e,allowedOrigins:t}){if(!e)throw new a("INVALID_ARGUMENT","remoteWindow must be defined");this.#t=e,this.#s=t?.length?t:[window.origin]}initialize=({log:e,validateReceivedMessage:t})=>{this.#n=e,this.#r=t,window.addEventListener("message",this.#l)};sendMessage=(e,t)=>{if(g(e)){const s=this.#c(e);this.#t.postMessage(e,{targetOrigin:s,transfer:t})}else if(p(e)||this.#d){const s=this.#d?(e=>{if(p(e))return{penpal:"synAck",methodNames:e.methodPaths.map(L)};if(f(e))return{penpal:"call",id:e.id,methodName:L(e.methodPath),args:e.args};if(w(e))return e.isError?{penpal:"reply",id:e.callId,resolution:"rejected",...e.isSerializedErrorInstance?{returnValue:e.value,returnValueIsError:!0}:{returnValue:e.value}}:{penpal:"reply",id:e.callId,resolution:"fulfilled",returnValue:e.value};throw T(e)})(e):e,n=this.#c(e);this.#t.postMessage(s,{targetOrigin:n,transfer:t})}else if(u(e)){const{port1:s,port2:n}=new MessageChannel;this.#i=s,s.addEventListener("message",this.#h),s.start();const r=[n,...t||[]],o=this.#c(e);this.#t.postMessage(e,{targetOrigin:o,transfer:r})}else{if(!this.#i)throw new N("Port is undefined");this.#i.postMessage(e,{transfer:t})}};addMessageHandler=e=>{this.#a.add(e)};removeMessageHandler=e=>{this.#a.delete(e)};destroy=()=>{window.removeEventListener("message",this.#l),this.#m(),this.#a.clear()};#g=e=>this.#s.some((t=>t instanceof RegExp?t.test(e):t===e||"*"===t));#c=e=>{if(g(e))return"*";if(!this.#o)throw new N("Concrete remote origin not set");return"null"===this.#o&&this.#s.includes("*")?"*":this.#o};#m=()=>{this.#i?.removeEventListener("message",this.#h),this.#i?.close(),this.#i=void 0};#l=({source:e,origin:t,ports:s,data:n})=>{if(e===this.#t&&((e=>h(e)&&"penpal"in e)(n)&&(this.#n?.("Please upgrade the child window to the latest version of Penpal."),this.#d=!0,n=(e=>{if("syn"===e.penpal)return{namespace:c,channel:void 0,type:"SYN",participantId:b};if("ack"===e.penpal)return{namespace:c,channel:void 0,type:"ACK2"};if("call"===e.penpal)return{namespace:c,channel:void 0,type:"CALL",id:e.id,methodPath:(t=e.methodName,t.split(".")),args:e.args};var t;if("reply"===e.penpal)return"fulfilled"===e.resolution?{namespace:c,channel:void 0,type:"REPLY",callId:e.id,value:e.returnValue}:{namespace:c,channel:void 0,type:"REPLY",callId:e.id,isError:!0,...e.returnValueIsError?{value:e.returnValue,isSerializedErrorInstance:!0}:{value:e.returnValue}};throw T(e)})(n)),this.#r?.(n)))if(this.#g(t)){if(g(n)&&(this.#m(),this.#o=t),u(n)&&!this.#d){if(this.#i=s[0],!this.#i)throw new N("No port received on ACK2");this.#i.addEventListener("message",this.#h),this.#i.start()}for(const e of this.#a)e(n)}else this.#n?.(`Received a message from origin \`${t}\` which did not match allowed origins \`[${this.#s.join(", ")}]\``)};#h=({data:e})=>{if(this.#r?.(e))for(const t of this.#a)t(e)}};e=s.hmd(e),n=window,r="StreamBIM",o=function(){return{connectToParent(e,t={}){const s=[];try{document.referrer&&s.push(new URL(document.referrer).origin)}catch(e){console.error(e)}const n=new x({remoteWindow:e,allowedOrigins:s});return this._connection=k({messenger:n,methods:t}),this._connection.promise.then((e=>{this.API=e}))},connectToChild(e,t={}){const s=new x({remoteWindow:e.contentWindow,allowedOrigins:[new URL(e.src).origin]});return this._connection=k({messenger:s,methods:t}),this._connection.promise.then((e=>{this.API=e}))},connectToWindow(e,t,s={}){const n=new x({remoteWindow:e,allowedOrigins:[new URL(t).origin]});return this._connection=k({messenger:n,methods:s}),this._connection.promise.then((e=>{this.API=e}))}}},e.exports?e.exports=o():"function"==typeof define&&s.amdO?define(r,o):n[r]=o()}},t={};function s(n){var r=t[n];if(void 0!==r)return r.exports;var o=t[n]={id:n,loaded:!1,exports:{}};return e[n](o,o.exports,s),o.loaded=!0,o.exports}s.amdO={},s.hmd=e=>((e=Object.create(e)).children||(e.children=[]),Object.defineProperty(e,"exports",{enumerable:!0,set:()=>{throw new Error("ES Modules may not assign module.exports or exports.*, Use ESM export syntax, instead: "+e.id)}}),e),s(691)})(); \ No newline at end of file diff --git a/src/index.js b/src/index.js index 1a04981..a67564e 100644 --- a/src/index.js +++ b/src/index.js @@ -17,11 +17,15 @@ import { WindowMessenger, connect } from 'penpal'; }(window, 'StreamBIM', function () { return { connectToParent(parentWindow, methods = {}) { - const parent = parentWindow || window.parent; + const allowedOrigins = []; + + if (document.referrer) { + allowedOrigins.push(new URL(document.referrer).origin); + } const messenger = new WindowMessenger({ - remoteWindow: parent, - allowedOrigins: [parent.origin, new URL(document.referrer).origin] + remoteWindow: parentWindow, + allowedOrigins: allowedOrigins }); this._connection = connect({