stacktrace.js
1 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
(function(win) {
var StackTrace = function() {}
, proto = StackTrace.prototype
, stacktrace
// make error traces visitable outside the closure
win.ErrorTrace = [];
// set a url to pesist your error traces
proto.url = '';
// run a supervisor
proto.track = function() {
var that = this;
return function(msg, url, line) {
ErrorTrace.push(msg + '@' + line);
that.post(msg, url, line);
}
};
// post data to backend
proto.post = function(msg, url, line) {
var xhr, params;
params = 'url=' + url +
'&message=' + msg +
'&line=' + line +
'&ua=' + navigator.userAgent;
xhr = typeof win.XMLHttpRequest === 'undefined' ?
new ActiveXObject('Msxml2.XMLHTTP') :
new XMLHttpRequest();
xhr.open('POST', this.url, true);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.send(params);
};
stacktrace = new StackTrace();
// record error traces
win.onerror = stacktrace.track();
})(window);