Mimic Window. onerror in Opera using javascript

走远了吗. 提交于 2019-12-01 03:47:00

This also happens on Safari, AFAIK.

What you could certainly do is create a global try/catch block for all JS code instead of text parsing - which could be tricky if you come into things like:

(function ($){
  $(function (){
  });
})(jQuery);

Opera 11.60+ supports window.onerror.

Opera's Dragonfly supports remote debugging. You might be able to hack it (it's all written in JavaScript) and log errors yourself (unfortunately the protocol isn't published yet).

4esn0k

you can replace Error.prototype.toString in Opera!

window.onerror = function (msg) {
  // send msg to http://errors.net/log.php, for example
  (new Image()).src = 'http://errors.net/log.php?msg=' + encodeURIComponent(msg);
};
if (({}).toString.call(window.opera) === '[object Opera]') {
 (function () {
   var x = Error.prototype.toString;
   Error.prototype.toString = function () {
     var msg = '';
     try {
       msg = x.apply(this, arguments);
       if (typeof (window.onerror) === "function") {
         window.onerror(msg, typeof (this) === 'object' ? this.stack : '', '');
       }
     } catch (e) {}
     return msg;
   };
 }());
}

seems, it doesn't work for Opera 11.50... only for early versions ...

there is a mention here that Opera now supports window.onerror:

http://my.opera.com/ODIN/blog/2011/11/07/what-s-new-in-opera-development-snapshots-4-november-2011-edition

but window.onerror does not seem to work in Opera Mini (e.g. user agent "Opera/9.80 (J2ME/MIDP; Opera Mini/7.1.32422/30.3214; U; en) Presto/2.8.119 Version/11.10"). This makes it really hard to debug javascript on mobiles with Opera Mini.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!