Comparing version 1.0.2 to 1.0.3
@@ -201,3 +201,3 @@ // Derived from https://github.com/umdjs/umd/blob/master/templates/amdWebGlobal.js | ||
var handleMessage = function handleMessage(message) { | ||
if (message.origin === childOrigin && message.data && message.data.penpal === 'handshake-reply') { | ||
if (message.source === child && message.origin === childOrigin && message.data && message.data.penpal === 'handshake-reply') { | ||
(function () { | ||
@@ -204,0 +204,0 @@ log('Parent: Received handshake reply from Child'); |
@@ -1,1 +0,1 @@ | ||
!function(e,n){var a={};n(a),"function"==typeof define&&define.amd?define("PenPal",a.default):e.PenPal=a.default}(this,function(e){"use strict";function n(e){if(Array.isArray(e)){for(var n=0,a=Array(e.length);n<e.length;n++)a[n]=e[n];return a}return Array.from(e)}function a(){if(i.debug){var e;(e=console).log.apply(e,arguments)}}function r(e){var n=document.createElement("a");return n.href=e,n.origin||n.protocol+"//"+n.hostname}function t(e,n){var r=e.localName,t=e.local,o=e.remote,l=e.remoteOrigin;a(r+": Creating call sender");var s=function(e){return function(){for(var n=arguments.length,s=Array(n),c=0;c<n;c++)s[c]=arguments[c];return a(r+": Sending "+e+"() call"),new i.Promise(function(n){var i=d(),c=function o(d){d.origin===l&&d.data&&"reply"===d.data.penpal&&d.data.id===i&&(a(r+": Received "+e+"() reply"),t.removeEventListener("message",o),n(d.data.returnValue))};t.addEventListener("message",c),o.postMessage({penpal:"call",id:i,methodName:e,args:s},l)})}};return n.reduce(function(e,n){return e[n]=s(n),e},{})}function o(e,r){var t=e.localName,o=e.local,d=e.remote,l=e.remoteOrigin;a(t+": Connecting call receiver");var s=function(e){e.origin===l&&e.data&&"call"===e.data.penpal&&!function(){var o=e.data,s=o.methodName,c=o.args,u=o.id;a(t+": Received "+s+"() call"),s in r&&i.Promise.resolve(r[s].apply(r,n(c))).then(function(e){a(t+": Sending "+s+"() reply"),d.postMessage({penpal:"reply",id:u,returnValue:e},l)})}()};return o.addEventListener("message",s),a(t+": Awaiting calls..."),function(){o.removeEventListener("message",s)}}Object.defineProperty(e,"__esModule",{value:!0});var i={Promise:function(){try{return window?window.Promise:null}catch(e){return null}}(),debug:!1},d=function(){var e=0;return function(){return++e}}();i.connectToChild=function(e){var n=e.url,d=e.appendTo,l=e.methods,s=void 0===l?{}:l,c=window,u=document.createElement("iframe");(d||document.body).appendChild(u);var m=u.contentWindow||u.contentDocument.parentWindow,f=r(n);return new i.Promise(function(e){var r=function n(r){r.origin===f&&r.data&&"handshake-reply"===r.data.penpal&&!function(){a("Parent: Received handshake reply from Child"),c.removeEventListener("message",n);var i={localName:"Parent",local:c,remote:m,remoteOrigin:r.origin},d=o(i,s),l=t(i,r.data.methodNames);l.frame=u,l.destroy=function(){d(),u.parentNode.removeChild(u)},e(l)}()};c.addEventListener("message",r),u.addEventListener("load",function(){a("Parent: Sending handshake"),setTimeout(function(){m.postMessage({penpal:"handshake",methodNames:Object.keys(s)},f)})}),a("Parent: Loading frame"),u.src=n})},i.connectToParent=function(e){var n=e.parentOrigin,r=e.methods,d=void 0===r?{}:r,l=window,s=l.parent;return new i.Promise(function(e){var r=function r(i){if((!n||i.origin===n)&&i.data&&"handshake"===i.data.penpal){a("Child: Received handshake from Parent"),l.removeEventListener("message",r),a("Child: Sending handshake reply to Parent"),i.source.postMessage({penpal:"handshake-reply",methodNames:Object.keys(d)},i.origin);var c={localName:"Child",local:l,remote:s,remoteOrigin:i.origin};o(c,d),e(t(c,i.data.methodNames))}};l.addEventListener("message",r)})},e.default=i}); | ||
!function(e,n){var a={};n(a),"function"==typeof define&&define.amd?define("PenPal",a.default):e.PenPal=a.default}(this,function(e){"use strict";function n(e){if(Array.isArray(e)){for(var n=0,a=Array(e.length);n<e.length;n++)a[n]=e[n];return a}return Array.from(e)}function a(){if(i.debug){var e;(e=console).log.apply(e,arguments)}}function r(e){var n=document.createElement("a");return n.href=e,n.origin||n.protocol+"//"+n.hostname}function t(e,n){var r=e.localName,t=e.local,o=e.remote,l=e.remoteOrigin;a(r+": Creating call sender");var s=function(e){return function(){for(var n=arguments.length,s=Array(n),c=0;c<n;c++)s[c]=arguments[c];return a(r+": Sending "+e+"() call"),new i.Promise(function(n){var i=d(),c=function o(d){d.origin===l&&d.data&&"reply"===d.data.penpal&&d.data.id===i&&(a(r+": Received "+e+"() reply"),t.removeEventListener("message",o),n(d.data.returnValue))};t.addEventListener("message",c),o.postMessage({penpal:"call",id:i,methodName:e,args:s},l)})}};return n.reduce(function(e,n){return e[n]=s(n),e},{})}function o(e,r){var t=e.localName,o=e.local,d=e.remote,l=e.remoteOrigin;a(t+": Connecting call receiver");var s=function(e){e.origin===l&&e.data&&"call"===e.data.penpal&&!function(){var o=e.data,s=o.methodName,c=o.args,u=o.id;a(t+": Received "+s+"() call"),s in r&&i.Promise.resolve(r[s].apply(r,n(c))).then(function(e){a(t+": Sending "+s+"() reply"),d.postMessage({penpal:"reply",id:u,returnValue:e},l)})}()};return o.addEventListener("message",s),a(t+": Awaiting calls..."),function(){o.removeEventListener("message",s)}}Object.defineProperty(e,"__esModule",{value:!0});var i={Promise:function(){try{return window?window.Promise:null}catch(e){return null}}(),debug:!1},d=function(){var e=0;return function(){return++e}}();i.connectToChild=function(e){var n=e.url,d=e.appendTo,l=e.methods,s=void 0===l?{}:l,c=window,u=document.createElement("iframe");(d||document.body).appendChild(u);var m=u.contentWindow||u.contentDocument.parentWindow,f=r(n);return new i.Promise(function(e){var r=function n(r){r.source===m&&r.origin===f&&r.data&&"handshake-reply"===r.data.penpal&&!function(){a("Parent: Received handshake reply from Child"),c.removeEventListener("message",n);var i={localName:"Parent",local:c,remote:m,remoteOrigin:r.origin},d=o(i,s),l=t(i,r.data.methodNames);l.frame=u,l.destroy=function(){d(),u.parentNode.removeChild(u)},e(l)}()};c.addEventListener("message",r),u.addEventListener("load",function(){a("Parent: Sending handshake"),setTimeout(function(){m.postMessage({penpal:"handshake",methodNames:Object.keys(s)},f)})}),a("Parent: Loading frame"),u.src=n})},i.connectToParent=function(e){var n=e.parentOrigin,r=e.methods,d=void 0===r?{}:r,l=window,s=l.parent;return new i.Promise(function(e){var r=function r(i){if((!n||i.origin===n)&&i.data&&"handshake"===i.data.penpal){a("Child: Received handshake from Parent"),l.removeEventListener("message",r),a("Child: Sending handshake reply to Parent"),i.source.postMessage({penpal:"handshake-reply",methodNames:Object.keys(d)},i.origin);var c={localName:"Child",local:l,remote:s,remoteOrigin:i.origin};o(c,d),e(t(c,i.data.methodNames))}};l.addEventListener("message",r)})},e.default=i}); |
@@ -187,3 +187,3 @@ 'use strict'; | ||
var handleMessage = function handleMessage(message) { | ||
if (message.origin === childOrigin && message.data && message.data.penpal === 'handshake-reply') { | ||
if (message.source === child && message.origin === childOrigin && message.data && message.data.penpal === 'handshake-reply') { | ||
(function () { | ||
@@ -190,0 +190,0 @@ log('Parent: Received handshake reply from Child'); |
{ | ||
"name": "penpal", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "A promise-based library for communicating with iframes via postMessage.", | ||
@@ -5,0 +5,0 @@ "author": "Aaron Hardy <aaron@aaronhardy.com>", |
27951