debug.js
3.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/**
* Created by zhanghong on 14-7-29.
*/
var debugArea = document.createElement("div");
debugArea.setAttribute("style", "position: fixed;max-height:30%;overflow:scroll");
//var info = document.createElement("div");
//info.setAttribute("style", "background: green;max-height:20%;overflow:scroll");
//var warn = document.createElement("div");
//warn.setAttribute("style", "background: yellow;max-height:20%;overflow:scroll");
//var error = document.createElement("div");
//error.setAttribute("style", "background: red;max-height:20%;overflow:scroll");
//debugArea.appendChild(info);
//debugArea.appendChild(warn);
//debugArea.appendChild(error);
var url = window.location.href,
parts = url.split('?'),
d = {};
if(parts.length == 2){
var hash = parts[1].indexOf('#')
if(hash != -1){
parts[1] = parts[1].substring(0, hash)
}
var pairs = parts[1].split('&')
pairs.forEach(function(pair){
var kv = pair.split('=')
if(kv.length == 2){
d[kv[0]] = kv[1]
}
})
}
var debug = (d.debug && d.debug == 1)?true:false;
var body = null;
if(debug) {
var init = function () {
body = document.getElementsByTagName("body")[0];
if (body) {
body.appendChild(debugArea);
if (startUpTimer) {
clearInterval(startUpTimer);
}
}
}
var startUpTimer = setInterval(init, 1000);
}
window.wwsDebug = {
_log : function(type, message){
var target = null;
var style = null;
switch(type){
case 'info':
// target = info;
style = "background: green;";
break;
case 'warn':
// target = warn;
style = "background: yellow;";
break;
case 'error':
// target = error;
style = "background: red;";
break;
}
//if(target){
if(debug) {
var line = document.createElement("h3");
line.setAttribute("style", style);
line.innerHTML = message;
debugArea.appendChild(line);
}
// console.log(message);
//}
},
error : function (message) {
this._log('error', message);
},
warn : function(message){
this._log('warn', message);
},
info : function(message){
this._log('info', message);
},
log : function(message){
this._log('info', message);
}
}
//if(debug){
// console.originalLog = console.log;
// console.log = function(message){
// wwsDebug.info(message);
// console.originalLog(message);
// }
//}
//function takeOverConsole(){
var console = window.console
if(console) {
function intercept(method) {
var original = console[method]
console[method] = function () {
wwsDebug[method](arguments);
if (original.apply) {
// Do this for normal browsers
original.apply(console, arguments)
} else {
// Do this for IE
var message = Array.prototype.slice.apply(arguments).join(' ')
original(message)
}
}
}
var methods = ['warn', 'error']
for (var i = 0; i < methods.length; i++)
intercept(methods[i])
}
//}