Нет описания

postmessage.min.js 6.1KB

12345678910111213141516171819202122232425
  1. /* Do not modify this file directly. It is compiled from other files. */
  2. /**
  3. The MIT License
  4. Copyright (c) 2010 Daniel Park (http://metaweb.com, http://postmessage.freebaseapps.com)
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. **/
  21. var NO_JQUERY={};!function(e,a,s){if(!("console"in e)){var t=e.console={};t.log=t.warn=t.error=t.debug=function(){}}a===NO_JQUERY&&(a={fn:{},extend:function(){for(var e=arguments[0],a=1,s=arguments.length;a<s;a++){var t=arguments[a];for(var n in t)e[n]=t[n]}return e}}),a.fn.pm=function(){return console.log("usage: \nto send: $.pm(options)\nto receive: $.pm.bind(type, fn, [origin])"),this},a.pm=e.pm=function(e){n.send(e)},a.pm.bind=e.pm.bind=function(e,a,s,t,r){n.bind(e,a,s,t,!0===r)},a.pm.unbind=e.pm.unbind=function(e,a){n.unbind(e,a)},a.pm.origin=e.pm.origin=null,a.pm.poll=e.pm.poll=200;var n={send:function(e){var s=a.extend({},n.defaults,e),t=s.target;if(s.target)if(s.type){var r={data:s.data,type:s.type};s.success&&(r.callback=n._callback(s.success)),s.error&&(r.errback=n._callback(s.error)),"postMessage"in t&&!s.hash?(n._bind(),t.postMessage(JSON.stringify(r),s.origin||"*")):(n.hash._bind(),n.hash.send(s,r))}else console.warn("postmessage type required");else console.warn("postmessage target window required")},bind:function(e,a,s,t,r){n._replyBind(e,a,s,t,r)},_replyBind:function(s,t,r,i,o){"postMessage"in e&&!i?n._bind():n.hash._bind();var l=n.data("listeners.postmessage");l||(l={},n.data("listeners.postmessage",l));var c=l[s];c||(c=[],l[s]=c),c.push({fn:t,callback:o,origin:r||a.pm.origin})},unbind:function(e,a){var s=n.data("listeners.postmessage");if(s)if(e)if(a){var t=s[e];if(t){for(var r=[],i=0,o=t.length;i<o;i++){var l=t[i];l.fn!==a&&r.push(l)}s[e]=r}}else delete s[e];else for(var i in s)delete s[i]},data:function(e,a){return void 0===a?n._data[e]:(n._data[e]=a,a)},_data:{},_CHARS:"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".split(""),_random:function(){for(var e=[],a=0;a<32;a++)e[a]=n._CHARS[0|32*Math.random()];return e.join("")},_callback:function(e){var a=n.data("callbacks.postmessage");a||(a={},n.data("callbacks.postmessage",a));var s=n._random();return a[s]=e,s},_bind:function(){n.data("listening.postmessage")||(e.addEventListener?e.addEventListener("message",n._dispatch,!1):e.attachEvent&&e.attachEvent("onmessage",n._dispatch),n.data("listening.postmessage",1))},_dispatch:function(e){try{var a=JSON.parse(e.data)}catch(e){return}if(a.type){var s=(n.data("callbacks.postmessage")||{})[a.type];if(s)s(a.data);else for(var t=(n.data("listeners.postmessage")||{})[a.type]||[],r=0,i=t.length;r<i;r++){var o=t[r];if(o.origin&&"*"!==o.origin&&e.origin!==o.origin){if(console.warn("postmessage message origin mismatch",e.origin,o.origin),a.errback){var l={message:"postmessage origin mismatch",origin:[e.origin,o.origin]};n.send({target:e.source,data:l,type:a.errback})}}else try{o.callback?o.fn(a.data,c,e):c(o.fn(a.data,e))}catch(s){if(!a.errback)throw s;n.send({target:e.source,data:s,type:a.errback})}function c(s){a.callback&&n.send({target:e.source,data:s,type:a.callback})}}}}};n.hash={send:function(a,s){var t=a.target,r=a.url;if(r){r=n.hash._url(r);var i,o=n.hash._url(e.location.href);if(e==t.parent)i="parent";else try{for(var l=0,c=parent.frames.length;l<c;l++){if(parent.frames[l]==e){i=l;break}}}catch(a){i=e.name}if(null!=i){var d={"x-requested-with":"postmessage",source:{name:i,url:o},postmessage:s},g="#x-postmessage-id="+n._random();t.location=r+g+encodeURIComponent(JSON.stringify(d))}else console.warn("postmessage windows must be direct parent/child windows and the child must be available through the parent window.frames list")}else console.warn("postmessage target window url is required")},_regex:/^\#x\-postmessage\-id\=(\w{32})/,_regex_len:"#x-postmessage-id=".length+32,_bind:function(){n.data("polling.postmessage")||(setInterval(function(){var a=""+e.location.hash,s=n.hash._regex.exec(a);if(s){var t=s[1];n.hash._last!==t&&(n.hash._last=t,n.hash._dispatch(a.substring(n.hash._regex_len)))}},a.pm.poll||200),n.data("polling.postmessage",1))},_dispatch:function(a){if(a){try{if(!("postmessage"===(a=JSON.parse(decodeURIComponent(a)))["x-requested-with"]&&a.source&&null!=a.source.name&&a.source.url&&a.postmessage))return}catch(e){return}var s=a.postmessage,t=(n.data("callbacks.postmessage")||{})[s.type];if(t)t(s.data);else{var r;r="parent"===a.source.name?e.parent:e.frames[a.source.name];for(var i=(n.data("listeners.postmessage")||{})[s.type]||[],o=0,l=i.length;o<l;o++){var c=i[o];if(c.origin){var d=/https?\:\/\/[^\/]*/.exec(a.source.url)[0];if("*"!==c.origin&&d!==c.origin){if(console.warn("postmessage message origin mismatch",d,c.origin),s.errback){var g={message:"postmessage origin mismatch",origin:[d,c.origin]};n.send({target:r,data:g,type:s.errback,hash:!0,url:a.source.url})}continue}}function p(e){s.callback&&n.send({target:r,data:e,type:s.callback,hash:!0,url:a.source.url})}try{c.callback?c.fn(s.data,p):p(c.fn(s.data))}catch(e){if(!s.errback)throw e;n.send({target:r,data:e,type:s.errback,hash:!0,url:a.source.url})}}}}},_url:function(e){return(""+e).replace(/#.*$/,"")}},a.extend(n,{defaults:{target:null,url:null,type:null,data:null,success:null,error:null,origin:"*",hash:!1}})}(this,"undefined"==typeof jQuery?NO_JQUERY:jQuery);