/** * * @authors bert (bertmaosong@gmail.com) * @date 2019-06-04 14:35:03 * @version 1.0 */ var catchErr = (function() { var catchErr = function() { return new catchErr.fn.init(); } catchErr.fn = catchErr.prototype = { constructor: catchErr, init: function() { this.noSendErrs = []; this.allErrs = []; this.listen = function() { var tthis = this; try { window.addEventListener('error', function(e){ var obj = { category: 'pc_error_statistics', action: 'page(' + window.location.pathname + ')', label: '' }; if(e.srcElement === window) { obj.label = 'Source(' + e.filename + ')' + '_Line(' + e.lineno + ')' + '_Column(' + e.colno + ')' + '_Error(' + e.message + ')'; } else { obj.label = 'Source(' + e.srcElement.src + ')' + '_Line()' + '_Column()' + '_Error(load fail)'; } if(tthis.checkExist(obj, tthis.allErrs)) { return; } else { tthis.allErrs.push(obj); tthis.sendInfo(obj) } }, true); } catch(error) { console.log(error) } }; this.sendInfo = function (obj) { try { if(typeof(ga) != 'undefined') { ga('send', 'event', obj.category, obj.action, obj.label , { 'dimension1': 'null' }); } else { !this.checkExist(obj, this.noSendErrs) ? this.noSendErrs.push(obj) : ''; this.reSendInfo(); } } catch(error) { console.log(error) } }; this.reSendInfo = function() { var tthis = this; setTimeout(function(){ if(tthis.noSendErrs.length > 0) { if(typeof(ga) != 'undefined') { for (var i=0; i < tthis.noSendErrs.length; i++ ) { tthis.sendInfo (tthis.noSendErrs[i]); } tthis.noSendErrs = []; } else { tthis.reSendInfo(); } } }, 5000) } this.checkExist = function(obj, arr) { var bool = false; for (var i=0; i < arr.length; i++ ) { if( arr[i].category == obj.category && arr[i].action == obj.action && arr[i].label == obj.label ) { bool = true; break; } } return bool; } } } catchErr.fn.init.prototype = catchErr.fn; return catchErr; })(); var msCatchErr = catchErr() msCatchErr.listen();