tv-remote-logger
Version:
remote transport console.log print message
1 lines • 4.99 kB
JavaScript
!function(){function ajax(a){var v,w,x,y,b=new XMLHttpRequest,c=a.async!==!1,d=a.cache!==!1,e=a.url,f=a.type?a.type.toUpperCase():"GET",g=a.query?a.query:{},h=a.header?a.header:{},i=a.data||{},j=a.dataType?a.dataType:"text",k=a.success&&"function"==typeof a.success?a.success:null,l=a.error&&"function"==typeof a.error?a.error:null,m=a.complete&&"function"==typeof a.complete?a.complete:null,n=a.context?a.context:a,o=a.timeout&&"number"==typeof a.timeout?a.timeout:null,p=null,q="jsonp"===j,r=a.jsonp||"callback",s=a.jsonpCallback||"callback"+Math.ceil(1e4*Math.random()),t=function(a,b){var c=document.createElement("script");c.src=e,window[b]=function(a){k&&k.call(n,a),document.body.removeChild(c),window[b]=void 0},document.body.appendChild(c)},u="";if(q&&(g[r]=s),Object.keys(g).length){v=0;for(w in g)g.hasOwnProperty(w)&&(x=g[w],u+=v===Object.keys(g).length-1?w+"="+x:w+"="+x+"&",v++)}if(e=(u?e+"?"+u:e)+(d||"GET"!==f?"":(u?"&":"?")+("ajaxCache="+Date.now())),"GET"===f&&q)return t(r,s);if(b.open(f,e,c),b.onreadystatechange=function(){if(4===b.readyState){clearTimeout(p);var a="";if(b.status>=200&&b.status<300||304===b.status){switch(j){case"text":a=b.responseText;break;case"json":a=JSON.parse(b.responseText);break;case"xml":a=b.responseXML;break;default:a=b.responseText}k&&k.call(n,a)}else l&&l.call(n,b);m&&m.call(n,b)}},"POST"!==f||h["Content-Type"]&&h["content-type"]||(h["Content-Type"]="application/x-www-form-urlencoded"),Object.keys(h).length)for(w in h)h.hasOwnProperty(w)&&b.setRequestHeader(w,h[w]);if(y="",Object.keys(i).length){v=0;for(w in i)i.hasOwnProperty(w)&&(y+=v===Object.keys(i).length-1?w+"="+i[w]:w+"="+i[w]+"&",v++)}b.send(y?y:null),o&&!q&&(p=setTimeout(function(){b.abort(),b.error="timeout"},o))}var Logger={counter:0,url:"http://localhost:8083",page:"log",result:function(){},exports:null,methodAliasList:[],err:null,remote:!0,stringify:function(a){var d,b=[];try{b=JSON.stringify(a)}catch(c){d=this,a.forEach(function(a,b,c){"object"==typeof a&&null!==a&&(c[b]=d.decycle(a))}),b=JSON.stringify(a)}finally{b="object"==typeof b?JSON.stringify(["终态处理对象失败,自行检查错误"]):b}return b},methodNames:["info","warn","error","log","table","time","timeEnd","trace","assert","dir","dirxml","count","group","groupEnd"],send:function(a,b){b=Array.prototype.slice.call(b,0);var c=b.reduce(function(a,b,c,d){return/element/i.test(Object.prototype.toString.call(b))?(d[c]=b.outerHTML,a.push(c)):"error"===/\w*(?=\])/.exec(Object.prototype.toString.call(b))[0].toLocaleLowerCase()&&(d[c]=b.toString()),a},[]);ajax({url:this.url+"/console/"+this.page,type:"POST",dataType:"json",data:{type:a,props:encodeURIComponent(this.stringify(b)),time:Date.now(),count:this.counter,dom:c.length?c.join(","):"noop"},success:function(a){this.result&&this.result(a)}.bind(this)}),this.counter++},decycle:function(a,b){var c=new Array;return function d(a,e){var f,g,h;return void 0!==b&&(a=b(a)),"object"!=typeof a||null===a||a instanceof Boolean||a instanceof Date||a instanceof Number||a instanceof RegExp||a instanceof String?a:(h=c.filter(function(b){return b.key===a?b:void 0}),f=h.length?h[0].path:void 0,void 0!==f?{$ref:f}:(c.push({key:a,path:e}),Array.isArray(a)?(g=[],a.forEach(function(a,b){g[b]=d(a,e+"["+b+"]")})):(g={},Object.keys(a).forEach(function(b){g[b]=d(a[b],e+"["+JSON.stringify(b)+"]")})),g))}(a,"$")},clear:function(){ajax({url:this.url+"/console/"+this.page,type:"POST",dataType:"json",data:{clearScreen:!0},success:function(a){this.result&&this.result(a)}.bind(this)})},noop:function(){},auto:function(a){var b=a.build,c=this;return this.url=a.url?a.url:this.url,this.page=a.page?a.page:this.page,this.result=a.result?a.result:this.result,this.err="function"==typeof a.err?a.err:this.err,this.exports=Array.isArray(a.exports)?a.exports:this.exports,this.remote=a.remote===!1?!1:this.remote,this.exec=a.exec===!0,this.exports&&this.exports.length&&this.setMethodAlias(this.exports),b?this.send=this.noop:this.remote?(this.err&&this.err(function(){c.send.call(Logger,"error",arguments)}),this.exec&&this.execCommand(),(void 0===a.clear||a.clear===!0)&&this.clear(),void 0):(this.send=function(a,b){b=Array.prototype.slice.call(b,0),console[a].apply(console,b)},void 0)},setMethodAlias:function(a){var b,c,d,e,f;for(b=0,c=a.length;c>b;b++)d=a[b].split(/\sas\s/),e=d[0],f=2===d.length?d[1]:e,window[f]=logger[e].bind(this),this.methodAliasList.push(f)},execCommand:function(){setInterval(function(){ajax({url:this.url+"/execute/"+this.page,type:"GET",dataType:"json",success:function(response){if(response.status===!0){var command=decodeURIComponent(response.command);try{eval(command),this.result&&this.result(response)}catch(err){this.result&&this.result(err),this.send("error",[err])}}else this.result&&this.result(response)}.bind(this)})}.bind(this),1e3)}},export_obj={auto:Logger.auto.bind(Logger)};Logger.methodNames.forEach(function(a){export_obj[a]=function(){Logger.send.call(Logger,a,arguments)}}),window.logger=export_obj}();