简体   繁体   English

尝试在 javascript 中捕捉警告

[英]try to catch a warning in javascript

Failed to execute 'write' on 'Document': It isn't possible to write into a document from an asynchronously-loaded external script unless it is explicitly opened. 

sounds like a good "warning" like "Nothing worked - just warning you. No errors, really."听起来像是一个很好的“警告”,比如“没有任何效果 - 只是警告你。没有错误,真的。”

Chromium pros who added this: https://src.chromium.org/viewvc/blink/trunk/Source/core/dom/Document.cpp?r1=164310&r2=164309&pathrev=164310添加此内容的 Chromium 专业人士: https : //src.chromium.org/viewvc/blink/trunk/Source/core/dom/Document.cpp?r1= 164310&r2 = 164309&pathrev =164310

try catch won't help with a warning as I've tested, so is there a good way to detect this error as soon as possible?正如我所测试的那样,try catch 对警告没有帮助,所以有没有尽快检测到这个错误的好方法?

what exactly does hasInsertionPoint want to be true? hasInsertionPoint想要什么是真的? sometimes document.write works fine but not others.有时 document.write 工作正常,但不是其他人。 (it runs from my userscript on document-start and finding some things, don't ask to fix that or not use document.write - thread is not about that) (它在文档启动时从我的用户脚本运行并找到一些东西,不要要求修复它或不使用 document.write - 线程与此无关)

Sorry being so late, but for those who didn't find a good/correct answer or way of doing it, you can just replace the console.warn function, it's far away from a correct form of doing this, but it's the only work around I have managed to think/find so far...抱歉这么晚了,但是对于那些没有找到好的/正确答案或方法的人,您可以替换 console.warn 函数,这离正确的执行方式还很远,但这是唯一的工作到目前为止,我已经设法思考/找到...

Example:例子:

function myCustomWarn(...args){

  //Example - Customize for your needs...
  var messages = args.filter(e => typeof e == 'string');

  for(m in messages){
    if(messages[m].indexOf('The warning I am looking for...') != -1){
      //treat the warning as you want
      //you could use switch case if you want
    };
  };

  return console.oldWarn(...args);

};

console.oldWarn = console.warn;

console.warn = myCustomWarn;

Remember that this "work around" only works if this code is evaluated at first, before that any possible warning can be thrown.请记住,这种“解决方法”仅在首先评估此代码时才有效,然后才能抛出任何可能的警告。 Otherwise it may miss warnings thrown by any code that is executed first or before it.否则它可能会错过任何首先或在它之前执行的代码抛出的警告。

@Marco Silva answer isn't perfect, he is using ES6 stuffs which are not supported everywhere. @Marco Silva 的回答并不完美,他使用的是并非处处都支持的 ES6 东西。 I think a debugger should be always as low level and portable as possible.我认为调试器应该总是尽可能低级和便携。 This would allows you to avoid the situation where you own debugger is buggy.这将允许您避免您自己的调试器有问题的情况。

Here is a code more portable:这是一个更便携的代码:

function myCustomWarn(){
    var args = Array.prototype.slice.call(arguments);
    var messages = args.filter(function(a) {
        return typeof a == 'string';
    });


    for(var m in messages){
        alert(messages[m]);
    };

    /**
     *  Calling console.oldWarn with previous args seems to lead to a
     *  infinite recurvise loop on iOS. Not sure why, disabled. 
     *  then again, if you show your log message in alert why would you
     *  post them to console ? 
     */

    // return console.oldWarn(arguments);
};

console.oldWarn = console.warn;

console.warn = myCustomWarn;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM