-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtracker-compiled.js
More file actions
240 lines (194 loc) · 7.36 KB
/
tracker-compiled.js
File metadata and controls
240 lines (194 loc) · 7.36 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
'use strict';
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; };
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
/**
* Created by Dandy on 12/28/17.
* Version 1.0.0
* License MIT
*/
(function (g) {
//-----------------------------------------------------Ajax-Section-Start------------------------------------------------------
var Ajax = function () {
function Ajax() {
_classCallCheck(this, Ajax);
}
_createClass(Ajax, [{
key: 'send',
value: function send(url, method, data, headers, callbackSuccess, callbackFailed) {
var xhr = new XMLHttpRequest(); //Over IE7 Running Well
if (method === 'get') {
if ((typeof data === 'undefined' ? 'undefined' : _typeof(data)) === 'object') {
var data_send = '?';
for (var key in data) {
data_send += key + '=' + data[key];
data_send += '&';
}
data_send = data_send.slice(0, -1);
}
xhr.open(method, url + data_send, true);
xhr.send(null);
} else if (method === 'post') {
xhr.open(method, url, true);
for (var k in headers) {
xhr.setRequestHeader(k, headers[k]);
}
xhr.send(JSON.stringify(data)); //Convert Send-Data To Json Type
} else {
return false;
}
xhr.onreadystatechange = function () {
//Register Callback Functions
if (xhr.readyState === 4) {
if (xhr.status === 200) {
console.log('s');
if (callbackSuccess != null) callbackSuccess(xhr.responseText);
} else {
if (callbackFailed != null) callbackFailed();
console.error('Server Error, Please Check Your Server ' + url + ' If Well');
}
}
};
}
}]);
return Ajax;
}();
//-----------------------------------------------------Ajax-Section-End--------------------------------------------------------
//----------------------------------------------------Tracker-Section-Start----------------------------------------------------
/**
* @description Tracker Private Variable
**/
var opts = {},
//Merged Options
report_obj = {
tracker_msg: null,
tracker_file_url: null,
tracker_line: null,
tracker_column: null,
tracker_error_obj: null,
tracker_agent: null,
tracker_time: null
},
report_manager = {
_queue: {},
_oldestIndex: 1, //Pointer Of Report Queue Head
_newestIndex: 1 };
/**
* @step1: initialize ajax & listen errors
* @step2: catch errors & format data & push to report queue
* @step3: dequeue & send data & callback
**/
var Tracker = function () {
function Tracker() {
_classCallCheck(this, Tracker);
this.ajax = new Ajax();
this.listenError();
}
/**
* @name catchError
* @flow-control: listen & catch errors, format & enqueue, dequeue & report, catch response & run callback
**/
_createClass(Tracker, [{
key: 'listenError',
value: function listenError() {
var _this = this;
try {
window.onerror = function (msg, url, line, column, errorObj) {
_setTrackerMessage(msg);
_setTrackerFileUrl(url);
_setTrackerLine(line);
_setTrackerColumn(column);
_setTrackerErrorObj(errorObj);
_setTrackerTime();
_setTrackerUserAgent();
_enQueue();
_this.reportTrack(_deQueue());
};
} catch (e) {
console.error(e);
}
}
}, {
key: 'reportTrack',
value: function reportTrack(data) {
this.ajax.send(opts.report_url, opts.method, data, opts.headers, opts.callbackSuccess, opts.callbackFailed);
/* @description: Send Tracker Data With Image & Callback Options Won't Work Anymore
*
* var url = REPORT_URL + data.join('||');// 组装错误上报信息内容URL
* var img = new Image;
* img.onload = img.onerror = function(){
* img = null;
* };
* img.src = url;// 发送数据到后台cgi
**/
/* @description: Send Tracker Data With sendBeacon && Callback Options Won't Work Anymore
*
* sendBeacon(opts.url,data)
**/
// TODO: reportTrack method with sendBeacon & new Image() request
}
}]);
return Tracker;
}();
/**
* @description Tracker Private Functions
**/
var _enQueue = function _enQueue() {
report_manager._queue[report_manager._oldestIndex] = report_obj;
report_manager._newestIndex++;
};
var _deQueue = function _deQueue() {
var deletedData = void 0;
if (report_manager._oldestIndex !== report_manager._newestIndex) {
//Judge Report-Queue If Suppose-Overflow Or Null
deletedData = report_manager._queue[report_manager._oldestIndex];
delete report_manager._queue[report_manager._oldestIndex++];
return deletedData;
}
throw new Error("Tracker Queue Is Full Stack");
};
var _setTrackerMessage = function _setTrackerMessage(msg) {
report_obj.tracker_msg = msg;
};
var _setTrackerFileUrl = function _setTrackerFileUrl(url) {
report_obj.tracker_file_url = url;
};
var _setTrackerLine = function _setTrackerLine(line) {
report_obj.tracker_line = line;
};
var _setTrackerColumn = function _setTrackerColumn(column) {
report_obj.tracker_column = column;
};
var _setTrackerErrorObj = function _setTrackerErrorObj(obj) {
report_obj.tracker_error_obj = obj;
};
var _setTrackerUserAgent = function _setTrackerUserAgent() {
report_obj.tracker_agent = navigator.userAgent;
};
var _setTrackerTime = function _setTrackerTime() {
report_obj.tracker_time = Date.now();
};
//----------------------------------------------------Tracker-Section-End------------------------------------------------------
/**
* @description Define Global Tracker Instance
**/
g.ErrorTracker = function () {
var defaults = {
report_url: null,
method: 'post',
headers: {
'Content-Type': 'application/json'
},
callbackSuccess: null,
callbackFailed: null
};
return {
init: function init(opt) {
if (!opt.report_url) throw new Error("Tracker Report_Url Can't Be Null");
opts = Object.assign('', defaults, opt);
return new Tracker();
}
};
}();
})(typeof window === 'undefined' ? undefined : window);
//# sourceMappingURL=tracker-compiled.js.map