forked from patik/console.log-wrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsolelog.detailprint.min.js
8 lines (8 loc) · 2.57 KB
/
consolelog.detailprint.min.js
1
2
3
4
5
6
7
8
/**
* Cross-Browser console.log() Wrapper
* Detailed Print Plugin
*
* Version 2.0.0, 2013-10-20
* By Craig Patik
* https://github.com/patik/console.log-wrapper/
*/window.log=window.log||function(){};window.log.needsDetailPrint=function(){var uaCheck,uaVersion,ua=window.navigator.userAgent;if(/iPad|iPhone|iPod/.test(window.navigator.platform)){uaCheck=ua.match(/OS\s([0-9]{1})_([0-9]{1})/);uaVersion=uaCheck?parseInt(uaCheck[1],10):0;if(uaVersion>=6)return!0}else if(window.opera){uaCheck=/Version\/(\d+)\.\d+/;if(uaCheck.test(ua)&&parseInt(uaCheck.exec(ua)[1],10)<=11)return!0}else if(/MSIE\s\d/.test(ua))return!0;return!1}();window.log.detailPrint=function(args){var j,thisArg,argType,str,beginStr,getSpecificType=function(obj){var reportedType=Object.prototype.toString.call(obj),types="Array,Date,RegExp,Null".split(","),found="",n=types.length;while(n--)if(reportedType==="[object "+types[n]+"]"){found=types[n].toLowerCase();break}if(found.length)return found;if(typeof HTMLElement=="object"&&obj instanceof HTMLElement||typeof obj.nodeName=="string"&&obj.nodeType===1)found="element";else if(typeof Node=="object"&&obj instanceof Node||typeof obj.nodeType=="number"&&typeof obj.nodeName=="string")found="node";/^\[object (HTMLCollection|NodeList|Object)\]$/.test(reportedType)&&typeof obj.length=="number"&&typeof obj.item!="undefined"&&(obj.length===0||typeof obj[0]=="object"&&obj[0].nodeType>0)&&(found="node");return found.length?found:typeof obj},detailedArgs=[],i=0;while(i<args.length){thisArg=args[i];argType=typeof thisArg;beginStr="Item "+(i+1)+"/"+args.length+" ";if(argType==="object"){argType=getSpecificType(thisArg);if(argType==="array")if(!thisArg.length)detailedArgs.push(beginStr+"(array, empty) ",thisArg);else{j=thisArg.length>3?3:thisArg.length;str="";while(j--)str=getSpecificType(thisArg[j])+", "+str;thisArg.length>3?str+="...":str=str.replace(/,\s$/,"");detailedArgs.push(beginStr+"(array, length="+thisArg.length+", ["+str+"]) ",thisArg)}else if(argType==="element"){str=thisArg.nodeName.toLowerCase();thisArg.id&&(str+="#"+thisArg.id);thisArg.className&&(str+="."+thisArg.className.replace(/\s+/g,"."));detailedArgs.push(beginStr+"(element, "+str+") ",thisArg)}else if(argType==="date")detailedArgs.push(beginStr+"(date) ",thisArg.toUTCString());else{detailedArgs.push(beginStr+"("+argType+")",thisArg);if(argType==="object"&&typeof thisArg.hasOwnProperty=="function")for(j in thisArg)thisArg.hasOwnProperty(j)&&detailedArgs.push(' --> "'+j+'" = ('+getSpecificType(thisArg[j])+") ",thisArg[j])}}else detailedArgs.push(beginStr+"("+typeof thisArg+") ",thisArg);i++}return detailedArgs};