-
Notifications
You must be signed in to change notification settings - Fork 0
/
simplerXDM.min.js
8 lines (8 loc) · 3.68 KB
/
simplerXDM.min.js
1
2
3
4
5
6
7
8
(function(j,J){function x(){}function E(a,b,e){j.addEventListener?a.postMessage(b,e):setTimeout(function(){a.postMessage(b,e)},0)}function y(a,b,e){a.addEventListener?a.addEventListener(b,e,!1):a.attachEvent("on"+b,e)}function z(a,b,e){a.removeEventListener?a.removeEventListener(b,e,!1):a.detachEvent("on"+b,e)}function t(a){if(/^file:/.test(a))throw"The file:// protocol is not supported";var b=a.toLowerCase().match(/^((http.?:)\/\/([^:\/\s]+)(:\d+)*)/);a=b[2];var e=b[3],b=b[4]||"";if("http:"==a&&
":80"==b||"https:"==a&&":443"==b)b="";return a+"//"+e+b}function r(a,b){for(var e in b)b.hasOwnProperty(e)&&(a[e]=b[e]);return a}var A=j.document,s=j.location,l=j.finamtrade;l||(j.finamtrade=l={});if(!l.XDM){var v=Array.isArray;v||(v=function(a){return"[object Array]"==Object.prototype.toString.call(a)});var B=Array.from;B||(B=function(a){return Array.prototype.slice.call(a)});l.XDM=function(a){function b(f){var a;if(f.origin)a=t(f.origin);else if(f.uri)a=t(f.uri);else if(f.domain)a=s.protocol+"//"+
f.domain;else throw"Unable to retrieve the origin of the event";if(a==u&&f.data.substring(0,k.length+1)==k+" ")if(f=f.data.substring(k.length+1),f=JSON.parse(f),f.method){var c=f.id;a=F[f.method];f=f.params;if(a){var b,d;c?(b=function(a){b=x;l({id:c,result:a})},d=function(a,f){d=x;var b={id:c,error:{code:-32099,message:a}};f&&(b.error.data=f);l(b)}):b=d=x;isArray(f)||(f=[f]);try{var e=a.apply(m,f.concat([b,d]));e!==J&&b(e)}catch(g){d(g.message)}}else c&&l({id:c,error:{code:-32601,message:"Procedure not found."}})}else e=
C[f.id],f.error?e.error&&e.error(f.error):e.success&&e.success(f.result),delete C[f.id]}function e(f){if(f.data==k+"-ready"&&(w="postMessage"in n.contentWindow?n.contentWindow:n.contentWindow.document,z(j,"message",e),y(j,"message",b),"function"==typeof a.onReady))a.onReady()}function l(a){E(w,k+" "+JSON.stringify(a),u)}function v(a){return function(){var b=B(arguments),c=b.length,e,d={method:a};0<c&&"function"==typeof b[c-1]?(1<c&&"function"==typeof b[c-2]?(e={success:b[c-2],error:b[c-1]},d.params=
b.slice(0,c-2)):(e={success:b[c-1]},d.params=b.slice(0,c-1)),C[""+ ++G]=e,d.id=G):d.params=b;l(d)}}function H(a,b){r(F,b);r(D,a);for(var c in a)a.hasOwnProperty(c)&&(m[c]=v(c))}function K(){for(var a=0;a<D.length;a++)delete m[D[a]];z(j,"message",e);z(j,"message",b);n&&(n.parentNode.removeChild(n),m.frame=n=null)}var m=this,G=0,C={},w,k,u,F={},D={};H(a.remote,a.local);if(a.url){var c=a.url,n;k=a.channel||"default_"+Math.floor(1E6*Math.random());u=t(c);y(j,"message",e);var h=/[\-\w]+\/\.\.\//,c=c.replace(/([^:])\/\//g,
"$1/");if(!c.match(/^(http||https):\/\//)){var d="/"===c.substring(0,1)?"":s.pathname;"/"!==d.substring(d.length-1)&&(d=d.substring(0,d.lastIndexOf("/")+1));c=s.protocol+"//"+s.host+d+c}for(;h.test(c);)c=c.replace(h,"");var h=r({xdm_e:t(s.href),xdm_c:k},a.params||{}),g,d="",p=c.indexOf("#");-1!=p&&(d=c.substring(p),c=c.substring(0,p));p=[];for(g in h)h.hasOwnProperty(g)&&p.push(g+"="+encodeURIComponent(h[g]));g=c+(-1==c.indexOf("?")?"?":"&")+p.join("&")+d;g=r({src:g,id:"ft_xdm_provider_"+k},a.props||
{});h=m;c=a.container;d=A.createElement("IFRAME");c?"string"==typeof c&&(c=A.getElementById(c)):(r(d.style,{position:"absolute",top:"-9999px",left:"0"}),c=A.body);var p=g.src,I=g.style;g.src="javascript:false";I&&(delete g.style,r(d.style,I));d.frameBorder=0;for(var q in g)g.hasOwnProperty(q)&&d.setAttribute(q,g[q]);c.appendChild(d);d.src=p;h.frame=n=d;m.extendRpcMethods=H;m.destroy=K}else{q=j.parent;g={};c=s.search.substring(1).split("&");for(d=c.length;d--;)h=c[d].split("="),g[h[0]]=decodeURIComponent(h[1]);
h=g.xdm_e.replace(/["'<>\\]/g,"");k=g.xdm_c.replace(/["'<>\\]/g,"");u=t(h);w="postMessage"in q?q:q.document;y(j,"message",b);E(w,k+"-ready",u)}return m}}})(window);