-
Notifications
You must be signed in to change notification settings - Fork 0
/
xdomain.min.js
executable file
·3 lines (3 loc) · 7.7 KB
/
xdomain.min.js
1
2
3
// XDomain - v0.5.8 - https://github.com/jpillora/xdomain
// Jaime Pillora <[email protected]> - MIT Copyright 2014
!function(a){!function(a,b,c){var d,e,f,g,h,i,j,k,l,m,n=[].slice;e="before",d="after",h="readyState",g="Invalid number or parameters. Please see API documentation.",(m=Array.prototype).indexOf||(m.indexOf=function(a){var b,c,d,e;for(b=d=0,e=this.length;e>d;b=++d)if(c=this[b],c===a)return b;return-1}),f=function(a){var b,d,e;return d={},e=function(a){return d[a]||[]},b={listeners:function(a){return Array.prototype.slice.call(e(a))},on:function(a,b,f){d[a]=e(a),d[a].indexOf(b)>=0||(f=f===c?d[a].length:f,d[a].splice(f,0,b))},off:function(a,b){var c;c=e(a).indexOf(b),-1!==c&&e(a).splice(c,1)},fire:function(){var b,c,d,f,g,h;for(c=arguments[0],b=2<=arguments.length?n.call(arguments,1):[],h=e(c),f=0,g=h.length;g>f;f++)d=h[f],d.apply(a,b)}}},k=f(),l={},l[e]=function(a,b){return k.on(e,a,b)},l[d]=function(a,b){return k.on(d,a,b)},j=function(a,b){var c,d,e,f,g,h;switch(null==b&&(b={}),typeof a){case"object":d=[];for(e in a)f=a[e],d.push(""+e+": "+f);return d.join("\n");case"string":for(d=a.split("\n"),g=0,h=d.length;h>g;g++)c=d[g],/([^:]+):\s*(.+)/.test(c)&&(b[RegExp.$1]||(b[RegExp.$1]=RegExp.$2));return b}},l.headers=j,i=a.XMLHttpRequest,a.XMLHttpRequest=function(){var b,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C;if(z=new i,0===k.listeners(e).length&&0===k.listeners(d).length)return z;for(y=!1,v={headers:{}},w=null,r=f(),u=function(){q.status=w.status,q.statusText=w.statusText,w.headers||(w.headers={})},t=function(){q.responseType=w.type||"",q.response=w.data||null,q.responseText=w.text||w.data||"",q.responseXML=w.xml||null},m=function(){var a,b,c,d;w.status=z.status,w.statusText=z.statusText,c=j(z.getAllResponseHeaders()),d=[];for(a in c)b=c[a],w.headers[a]?d.push(void 0):d.push(w.headers[a]=b);return d},l=function(){return w.type=z.responseType,w.text=z.responseText,w.data=z.response||w.text,w.xml=z.responseXML},n=0,x=function(a){var b,c,e;return p(),b=function(){for(;a>n&&4>n;)q[h]=++n,2===n&&u(),4===n&&t(),r.fire("readystatechange",s("readystatechange")),4===n&&(r.fire("load",s("load")),r.fire("loadend",s("loadend")))},4>a?b():(c=k.listeners(d),e=function(){var a;if(!c.length)return b();if(a=c.shift(),2===a.length)return a(v,w),e();if(3===a.length)return a(v,w,e);throw g},e(),void 0)},s=function(b){var c;if(null!=a.document.createEventObject)return c=a.document.createEventObject(),c.type=b,c;try{return new Event(b)}catch(d){return{type:b}}},b=function(a){var b,c,d;b={};for(c in a)d=a[c],b[c]=d===z?q:d;return b},p=function(){var a,b,d,e,f;for(f=["timeout"],d=0,e=f.length;e>d;d++)b=f[d],z[b]&&v[b]===c&&(v[b]=z[b]);for(b in q)a=q[b],"function"==typeof a&&/^on(\w+)/.test(b)&&r.on(RegExp.$1,a)},z.onreadystatechange=function(){try{2===z[h]&&(m(),x(2))}catch(a){}4===z[h]&&(y=!1,m(),l(),x(4))},C=["abort","progress"],A=0,B=C.length;B>A;A++)o=C[A],z["on"+o]=function(a){return r.fire(o,b(a))};return q={withCredentials:!1,response:null,status:0},q.addEventListener=function(a,b){return r.on(a,b)},q.removeEventListener=r.off,q.dispatchEvent=function(){},q.open=function(a,b,c){v.method=a,v.url=b,v.async=c,x(1)},q.send=function(a){var b,c,d;v.body=a,d=function(){var a,b,c;w={headers:{}},y=!0,z.open(v.method,v.url,v.async),v.timeout&&(z.timeout=v.timeout),c=v.headers;for(a in c)b=c[a],z.setRequestHeader(a,b);z.send(v.body)},b=k.listeners(e),c=function(){var a,e;if(!b.length)return d();if(a=function(a){return"object"!=typeof a||"number"!=typeof a.status?c():(w=a,x(4),void 0)},e=b.shift(),1===e.length)return a(e(v));if(2===e.length)return e(v,a);throw g},c()},q.abort=function(){y&&z.abort(),r.fire("abort",arguments)},q.setRequestHeader=function(a,b){v.headers[a]=b},q.getResponseHeader=function(a){return w.headers[a]},q.getAllResponseHeaders=function(){return j(w.headers)},q},a.xhook=l}(a,document);var b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I;for(h=location.protocol+"//"+location.host,y=function(b){var c;return b="xdomain ("+h+"): "+b,c=a.console=a.console||{},c.warn?c.warn(b):alert(b)},G=["postMessage","JSON"],A=0,D=G.length;D>A;A++)if(i=G[A],!a[i])return y("requires '"+i+"' and this browser does not support it"),void 0;for(c="V0",k=function(){return(Math.random()*Math.pow(2,32)).toString(16)},p=function(a){return/(https?:\/\/[^\/\?]+)(\/.*)?/.test(a)?{origin:RegExp.$1,path:RegExp.$2}:null},x=function(a){var b;return a instanceof RegExp?a:(b=a.toString().replace(/\W/g,function(a){return"\\"+a}).replace(/\\\*/g,".+"),new RegExp("^"+b+"$"))},n=function(b){return document.addEventListener?a.addEventListener("message",b):a.attachEvent("onmessage",b)},t=function(a){return JSON.stringify(a)},j=function(a){return JSON.parse(a)},m=null,e=function(a){var b,c;null===m&&(m={},u());for(b in a)c=a[b],m[b]=c},u=function(){return n(function(a){var b,c,d,e,f,g,h,i,k,l,n,o,q;g="null"===a.origin?"*":a.origin,i=null;for(d in m){l=m[d];try{if(e=x(d),e.test(g)){i=x(l);break}}catch(r){}}if(!i)return y("blocked request from: '"+g+"'"),void 0;if(b=a.source,f=j(a.data),n=f.msg,h=p(n.url),!h||!i.test(h.path))return y("blocked request to path: '"+h.path+"' by regex: "+l),void 0;k=new XMLHttpRequest,k.open(n.method,n.url),k.onreadystatechange=function(){var a;if(4===k.readyState)return a={status:k.status,statusText:k.statusText,type:"",text:k.responseText,headers:xhook.headers(k.getAllResponseHeaders())},b.postMessage(t({id:f.id,msg:a}),g)},n.timeout&&(k.timeout=n.timeout),q=n.headers;for(c in q)o=q[c],k.setRequestHeader(c,o);return k.send(n.body||null)}),a===a.parent?y("slaves must be in an iframe"):a.parent.postMessage("XPING_"+c,"*")},w=null,f=function(a){var b,c;null===w&&(w={},v());for(b in a)c=a[b],w[b]=c},v=function(){return n(function(a){var b;return null!=(b=d.prototype.frames[a.origin])?b.recieve(a):void 0}),xhook.before(function(a,b){var c,e;return e=p(a.url),e&&w[e.origin]?(a.async===!1&&y("sync not supported"),c=new d(e.origin,w[e.origin]),c.send(a,b),void 0):b()})},d=function(){function a(a,b){return this.origin=a,this.proxyPath=b,this.frames[this.origin]?this.frames[this.origin]:(this.frames[this.origin]=this,this.listeners={},this.frame=document.createElement("iframe"),this.frame.id=this.frame.name="xdomain-"+k(),this.frame.src=this.origin+this.proxyPath,this.frame.setAttribute("style","display:none;"),document.body.appendChild(this.frame),this.waits=0,this.waiters=[],this.ready=!1,void 0)}return a.prototype.frames={},a.prototype.post=function(a){this.frame.contentWindow.postMessage(a,this.origin)},a.prototype.listen=function(a,b){if(this.listeners[a])throw"already listening for: "+a;this.listeners[a]=b},a.prototype.unlisten=function(a){delete this.listeners[a]},a.prototype.recieve=function(a){var b,d;return/^XPING(_(V\d+))?$/.test(a.data)?RegExp.$2!==c?(y("your master is not compatible with your slave, check your xdomain.js verison"),void 0):(this.ready=!0,void 0):(d=j(a.data),(b=this.listeners[d.id])?(this.unlisten(d.id),b(d.msg),void 0):(y("unkown message ("+d.id+")"),void 0))},a.prototype.send=function(a,b){var c=this;this.readyCheck(function(){var d;return d=k(),c.listen(d,function(a){return b(a)}),c.post(t({id:d,msg:a}))})},a.prototype.readyCheck=function(a){var c,d=this;return this.ready?a():(this.waiters.push(a),1===this.waiters.length&&(c=function(){if(!d.ready){if(d.waits++>=z.timeout/b)throw"Timeout connecting to iframe: "+d.origin;return setTimeout(c,b)}for(;d.waiters.length;)d.waiters.shift()()},c()),void 0)},a}(),z=function(a){a&&(a.masters&&e(a.masters),a.slaves&&f(a.slaves))},z.parseUrl=p,z.origin=h,z.timeout=15e3,b=100,a.xdomain=z,H=document.getElementsByTagName("script"),B=0,E=H.length;E>B;B++)if(s=H[B],/xdomain/.test(s.src))for(I=["","data-"],C=0,F=I.length;F>C;C++){if(q=I[C],g=s.getAttribute(q+"slave")){if(o=p(g),!o)return;r={},r[o.origin]=o.path,f(r);break}g=s.getAttribute(q+"master"),g&&(l={},l[g]=/./,e(l))}}(this);