forked from padolsey/debug
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdebug.js
More file actions
340 lines (294 loc) · 11.9 KB
/
debug.js
File metadata and controls
340 lines (294 loc) · 11.9 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*
DEBUG.js
(c) James Padolsey
http://james.padolsey.com
DEBUG.js is an in-browser JavaScript error-reporter
Read README.txt for instructions
Edited by David Waller
*/
(function(){
var strictCommenting = true;
// Overlaying document; where the errors are displayed.
var modal = (function(){
// Modal interface.
var D = document,
style = function(el, s) {
for (var p in s) {
el.style[p] = s[p];
}
return el;
},
overlay = style(document.createElement('div'), {
height: '100%',
width: '100%',
position: 'absolute',
top: 0,
left: 0,
zIndex: 999,
background: '#DDD',
color: '#222',
overflow: 'auto',
display: 'none'
}),
list = style(document.createElement('ol'), {
padding: '20px',
listStyle: 'none',
margin: 0
}),
mainHeading = style(document.createElement('h1'), {
padding: '20px 0 0 ',
textAlign: 'center',
margin: 0,
fontSize: '20px'
}),
nextGroup = list;
mainHeading.innerHTML = 'syntax/runtime errors in your JavaScript';
overlay.appendChild(mainHeading);
var run = false;
return {
add : function(text, error) {
// Only inject the "modal" if this function is called: (for the 1st time):
if (!run) {
document.documentElement.style.height = document.body.style.height =
document.documentElement.style.width = document.body.style.width = '100%';
document.documentElement.style.overflow = document.body.style.overflow = 'hidden';
overlay.style.display = 'block';
overlay.appendChild(list);
document.body.appendChild(overlay);
run = true;
}
var li = style(document.createElement('li'), {
padding: '4px 0 10px 0',
fontWeight: '700',
fontFamily: '"Consolas","Lucida Console",Courier,mono',
fontSize: '15px'
});
if(error && error.evidence) {
var pre = style(document.createElement('pre'), {
padding: '10px',
background: 'white',
border: '1px solid black',
margin: '10px 10px 10px 30px',
fontSize: '13px',
overflow: 'auto'
});
var evidence = error.evidence;
pre.innerHTML = error.character && error.character < error.evidence.length ? ([
evidence.substring(0, error.character),
'<span style="padding:5px;background:pink;">',
evidence.charAt(error.character),
'</span>',
evidence.substring(error.character + 1)
].join('')) : error.evidence;
// Replace leading/trailing whitespace
pre.innerHTML = pre.innerHTML.replace(/^[^\S][^\S]*|[^\S][^\S]*$/g, '').replace(/(\n\r|\n)/g, '<br/>');
li.appendChild(pre);
}
li.innerHTML = text + li.innerHTML;
nextGroup.appendChild(li);
},
newGroup : function(scriptID, src) {
nextGroup = style(document.createElement('div'), {
background: '#FAFFA7',
margin: '0 0 15px 0',
padding: '10px'
});
var heading = style(document.createElement('h3'), {
color: 'black',
padding: '5px',
margin: '0 0 10px 0',
fontSize: '15px',
textAlign: 'left',
borderBottom: '1px solid black'
});
heading.innerHTML = 'Script ID: "' + scriptID + '"';
if (src) {
heading.innerHTML += ' <small>[' + src + ']</small>';
}
nextGroup.appendChild(heading);
list.appendChild(nextGroup);
}
}
})();
function init() {
var runLint = true,
uid = +(new Date()),
debugPatt = /:debug\((.+?)\)$/,
jsLintErrors = false;
parseScripts(/:debug\((.+?)\)$/, function(src, scriptEl){
var strings = [],
sid = '_' + (+new Date()),
lIndex = 0,
recovered = 0,
splitSRC,
scriptID = scriptEl.type.match(/:debug\((.+?)\)$/)[1] || 'NULL';
window[uid] = function(e) {
if (!isNaN(e)) {
recovered++;
} else {
modal.add('ERROR (<small>SCRIPT "' +scriptID+ '", STATEMENT: ' + (recovered+1) + '</small>): ' + e.message, {
// Replace blank lines (replace(...))
evidence: splitSRC[recovered].replace(/[\n\r]\s+(?=[\n\r])/, '')//.replace(/^.+?\{(?=(.|[\n\r])+)/,'')
});
}
};
modal.newGroup(scriptID, scriptEl.getAttribute('src'));
if (runLint) {
JSLINT(src);
for (var error in JSLINT.errors) {
jsLintErrors = true;
var thisError = JSLINT.errors[error];
if (thisError) {
modal.add([
'<a style="color:black;" href="http://jslint.com/" target="_blank">JSLINT</a> (<small>SCRIPT "',
scriptID,
'", LINE ',
thisError.line,
', CHARACTER ',
thisError.character,
'</small>): ',
(thisError.reason)
].join(''), thisError);
}
}
}
if (jsLintErrors) { return ''; }
// Remove ALL comments
src = strictCommenting ? removeComments(src, true) : src.replace(/\/\*.+?\*\/|\/\/.+?(?=[\n\r])/g, '');
// Replace strings, Replace each end-of-line with window.uid call.
src = src
.replace(/("|'|\/)((?:\\\1|.)+?)\1/g, function($0){
strings[strings.length] = $0;
return sid;
});
src = src.replace(/;/g, function(){
lIndex++;
return ';window[' + uid + '](' + lIndex + ');'
})
.replace(RegExp(sid,'g'), function(){
return strings.shift();
});
splitSRC = src.split(RegExp(';window\\[' + uid + '\\]\\(\\d+\\);'));
return 'try { ' + src + '}catch(e){ window[' + uid + '](e); }';
});
}
// Slightly adjusted version of "parseScripts":
function parseScripts(scriptType, parseFn) {
var scripts = document.getElementsByTagName('script'),
sLength = scripts.length,
execute = function(parsed) {
// Execute parsed script in global context.
var dScript = document.createElement('script');
try {
dScript.appendChild( document.createTextNode(parsed) );
document.body.appendChild(dScript);
} catch(e) {
dScript.text = parsed;
document.getElementsByTagName('head')[0].appendChild(dScript);
}
dScript.parentNode.removeChild(dScript);
};
for (var i = 0; i < sLength; i++) {
// All script elements matching scriptType are passed to parseFn.
var script = scripts[i],
type = script.type,
code = script.innerHTML;
if (scriptType.test ? scriptType.test(type) : type === scriptType) {
if (script.src) {
var xhr = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
xhr.open('GET', script.src, false);
xhr.send(null);
code = xhr.responseText;
xhr = null;
}
execute(parseFn ? parseFn(code, script) : code);
}
}
}
function removeComments(str, removeCondComp) {
str = ('__' + str + '__').split('');
var mode = {
singleQuote: false,
doubleQuote: false,
regex: false,
blockComment: false,
lineComment: false,
condComp: false
};
for (var i = 0, l = str.length; i < l; i++) {
if (mode.regex) {
if (str[i] === '/' && str[i-1] !== '\\') {
mode.regex = false;
}
continue;
}
if (mode.singleQuote) {
if (str[i] === "'" && str[i-1] !== '\\') {
mode.singleQuote = false;
}
continue;
}
if (mode.doubleQuote) {
if (str[i] === '"' && str[i-1] !== '\\') {
mode.doubleQuote = false;
}
continue;
}
if (mode.blockComment) {
if (str[i] === '*' && str[i+1] === '/') {
str[i+1] = '';
mode.blockComment = false;
}
str[i] = '';
continue;
}
if (mode.lineComment) {
if (str[i+1] === '\n' || str[i+1] === '\r') {
mode.lineComment = false;
}
str[i] = '';
continue;
}
if (mode.condComp) {
if (str[i-2] === '@' && str[i-1] === '*' && str[i] === '/') {
mode.condComp = false;
}
continue;
}
mode.doubleQuote = str[i] === '"';
mode.singleQuote = str[i] === "'";
if (str[i] === '/') {
if (str[i+1] === '*' && str[i+2] === '@') {
mode.condComp = true;
continue;
}
if (str[i+1] === '*') {
str[i] = '';
mode.blockComment = true;
continue;
}
if (str[i+1] === '/') {
str[i] = '';
mode.lineComment = true;
continue;
}
mode.regex = true;
}
}
return str.join('').slice(2, -2);
}
// Load JSLINT, don't start until we've got it!
if (!window.JSLINT){
var lint = document.createElement('script'),
sInt = setInterval(function(){
if (window.JSLINT) {
clearInterval(sInt);
init();
}
}, 100);
lint.src = 'http://www.jslint.com/fulljslint.js';
document.body.appendChild(lint);
}
else
init();
})();