使用debug.js除錯手機網頁
創作背景
手機網頁相容性與ie相比,有過之無不及,除錯的時候,我們會使用console.log
,但是手機上看不到列印出來的資訊。所以只好各種alert
,但alert總顯得不那麼優雅。
初學js的時候就alert,現在還在各種alert。而且alert後還要點選確定,如果迴圈alert,那就悲劇了,一直點確定吧,可能手機還因此搞當機了。
那麼有沒有一種顯得更加優雅的方式去輸出除錯資訊呢?
有的。使用debug.js
就好了。
貌似看到過一次,有人在iPhone上開啟了控制檯,如果有人知道,請告訴我。
debug.js
會把除錯資訊顯示在網頁上,很方便的就能看到。原理也十分簡單,動態建立dom元素,然後append
到頁面中。
先睹為快
你可以點這裡看到使用debug.js
的網頁,想說它真的非常好看,你也可以用手機掃描二維碼訪問。
如何使用debug.js
引入debug.js後,你會得到debug
這個方法,它是掛載在window
下面的。
debug.js
的api十分簡潔簡單,如果你沒啥追求,就直接使用這麼使用:
debug.log('一個優雅的除錯資訊');
你就會在網頁頂部看到一個優雅的除錯資訊。
如果你不滿足於此,你追求色彩,追求除錯型別,那麼debug.js
還提供了其他幾個實用的方法:
- debug.success()
- debug.warn()
- debug.error()
- debug.danger()
意思就不用解釋了,有了這些,輸出的除錯資訊也繽紛了起來,似乎人生也變得colorful。
額,突然覺得說完了,就這麼簡單,講述起來簡單,理解和使用起來亦是如此。
深入瞭解debug.js
你可以在Github上關注或者Forkdebug.js
。
如果你關心如何實現,可以點這裡看到debug.js
的原始碼實現。
色彩
我喜歡各種各樣的色彩,我喜歡彩色的物品,住的地方有各種彩色的元素,比如沙發,比如床單,比如T恤上的塗鴉。我不太會統一所有的東西,比如我的儲物櫃是綠色的,那麼我的垃圾桶可能就是黃色,我的熱水壺可能就是紅色。也因此設計了豐富多彩的
debug.js
。也許這算是某種怪癖吧,你呢?喜歡彩色麼?debug.js
var __bind=function(fn,me){return function(){return fn.apply(me,arguments)}};(function(WIN,DOC){"use strict";var CLICK,DANGER,Debug,ERROR,LOG,NULL,SUCCESS,UNDEFINED,WARN,bind,dom,entry,errListener,exports,getBody,isArray,isNull,isObejct,isTouch,noop,toString,unbind;UNDEFINED=void 0;NULL=null;LOG="log";DANGER="danger";WARN="warn";SUCCESS="success";ERROR="error";CLICK="click";isTouch="ontouchend"in WIN;noop=function(){};dom=DOC.querySelectorAll;toString={}.toString;bind=function(el,evt,callback){return el.addEventListener(evt,callback,false)};unbind=function(el,evt,fn){return el.removeEventListener(evt,fn,false)};isNull=function(val){return val===NULL};isArray=Array.isArray||function(val){return val&&"[object Array]"===toString.call(val)};isObejct=function(val){return typeof val==="object"&&!isArray(val)&&!isNull(val)};getBody=function(){var _ref,_ref1;return DOC["body"]||((_ref=dom("body"))!=null?_ref[0]:void 0)||((_ref1=dom("html"))!=null?_ref1[0]:void 0)};Debug=function(){var childCss,debugMap,fn,joinCss,parentBottom,parentCss,publicCss,render,translate;debugMap={log:"0074cc",danger:"da4f49",warn:"faa732",success:"5bb75b",error:"bd362f"};render=function(msg){var arr,item,text,_i,_len;text="";arr=[];if(isArray(msg)){for(_i=0,_len=msg.length;_i<_len;_i++){item=msg[_i];arr.push(""+item)}text="["+arr.join(",")+"]"}else if(isObejct(msg)){for(item in msg){arr.push(""+item+": "+msg[item])}text="{"+arr.join(",")+"}"}else{text=String(msg)}return text};translate=function(el,y){el.style.webkitTransform="translate3d(0,"+y+",0)";return el.style.transform="translate3d(0,"+y+",0)"};joinCss=function(css){return css.join(";")};parentBottom=6;publicCss=["-webkit-transition: all .3s ease","transition: all .3s ease"];childCss=["margin-top:-1px","padding:.5em","border-top:1px solid rgba(255,255,255,.1)","margin:0"].concat(publicCss);parentCss=["-webkit-overflow-scrolling:touch","overflow:auto","line-height:1.5","z-index:5000","position:fixed","left:0","top:0","font-size:11px","background:rgba(0,0,0,.8)","color:#fff","width:100%","padding-bottom:"+parentBottom+"px"].concat(publicCss);function Debug(){this.toggle=__bind(this.toggle,this);this.isInit=this.isHide=false;this.msg=this.fn=this.color="";this.el=NULL}Debug.prototype.init=function(){var body,el;el=this.el=DOC.createElement("div");el.setAttribute("style",joinCss(parentCss));body=getBody();body.appendChild(el);translate(el,0);bind(el,CLICK,function(_this){return function(){return _this.toggle()}}(this));this.isInit=true;return this};Debug.prototype.print=function(){var child,css;if(!this.isInit){this.init()}css=childCss.concat(["color:#"+this.color]);child=DOC.createElement("p");child.setAttribute("style",joinCss(css));child.innerHTML=this.msg;this.el.appendChild(child);return this};Debug.prototype.toggle=function(event){return(this.isHide?this.show:this.hide).call(this,event)};Debug.prototype.show=function(event){translate(this.el,0);this.isHide=false;return this};Debug.prototype.hide=function(event){translate(this.el,"-"+(this.el.offsetHeight-parentBottom)+"px");this.isHide=true;return this};for(fn in debugMap){Debug.prototype[fn]=function(fn){return function(msg){this.fn=fn;this.msg=render(msg);this.color=debugMap[fn];return this.print()}}(fn)}return Debug}();entry=new Debug;errListener=function(error){var msg;msg=["Error:","filename: "+error.filename,"lineno: "+error.lineno,"message: "+error.message,"type: "+error.type];return entry.error(msg.join("<br/>"))};bind(WIN,ERROR,errListener);entry.guai=function(){return unbind(WIN,ERROR,errListener)};if(typeof exports!=="undefined"&&module.exports){return module.exports=exports=entry}else if(typeof define==="function"){return define(function(require,exports,module){return module.exports=exports=entry})}else if(typeof angular==="object"){return angular.module("binnng/debug",[]).factory("$debug",function(){return entry})}else{return WIN["debug"]=entry}})(window,document);
相關文章
- 如何使用chrome devtool除錯Mobile網頁?Chromedev除錯網頁
- 紅米手機聯機除錯除錯
- 前端手機端除錯前端除錯
- 如何在電腦上除錯手機網站除錯網站
- 移動端網頁除錯網頁除錯
- eruda手機端除錯神器除錯
- 給手機端頁面留一個除錯後門吧(vue)除錯Vue
- 不用USB,透過adb無線除錯安卓手機頁面除錯安卓
- 如何除錯移動端網頁除錯網頁
- 小技巧|移動端網頁除錯神器Eruda使用技巧網頁除錯
- 微信公眾號網頁開發——實用真機除錯網頁除錯
- 移動端網頁除錯 之 Eruda網頁除錯
- 移動端 Web 網頁除錯技巧Web網頁除錯
- 2: mac 下真機除錯 android 手機Mac除錯Android
- 遠端除錯 Android 裝置網頁除錯Android網頁
- 網頁外部注入vConsole除錯網頁除錯
- 如何使用微信開發者工具除錯在微信端訪問的網頁除錯網頁
- 手機除錯、webview跳轉、vue基礎除錯WebViewVue
- chrome以及safari遠端除錯手機webviewChrome除錯WebView
- 頁面除錯神器Reveal的簡單使用除錯
- 核心頁表除錯除錯
- 使用selenium爬取網頁,如何在scrapy shell中除錯響應網頁除錯
- phpstorm+x_debug 網頁除錯利器!PHPORM網頁除錯
- 使用Chrome開發者工具除錯Android端內網頁(微信,QQ,UC,App內嵌頁等)Chrome除錯Android內網網頁APP
- 一個安卓手機遠端真機除錯平臺安卓除錯
- 如何實用便捷的在本地真機除錯WEB端HTML5網頁除錯WebHTML網頁
- js實現手機網頁滑動JS網頁
- 22 真機除錯bug(除錯包)除錯
- 網頁開發的6種線上除錯環境網頁除錯
- adb通過WiFi對手機進行除錯WiFi除錯
- 一加6T怎麼開啟手機USB除錯?一加6T手機USB除錯連線電腦教程除錯
- 小米系列真機除錯程式碼時遇到小米系列手機除錯Installation failed with message Failed to establish session除錯AISession
- 手機網頁用Bootstrap還是jQuery Mobile網頁bootjQuery
- 用手機連pc localhost 學習,除錯網站localhost除錯網站
- Windows windbg kernel debug 雙機核心除錯 - USB3.0 除錯 USB除錯 除錯線Windows除錯
- IOS真機除錯iOS除錯
- Python 程式碼除錯—使用 pdb 除錯Python除錯
- 移動端網頁除錯 之 weinre&spy-debugger網頁除錯