javascript 在控制台中抑制 Chrome 的“无法加载资源”消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4500741/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 13:54:15  来源:igfitidea点击:

Suppress Chrome 'Failed to load resource' messages in console

javascriptgoogle-chromexmlhttprequestgoogle-chrome-devtools

提问by VLostBoy

I'm writing a script that uses an XMLHttpRequest to search for a file defined by a relative path, by attempting to resolve that relative path against other same domain absolute paths that the script is aware of, then attempting to load the file from that resolved url. If I encounter a 404, I just try to resolve the files relative path against another absolute path, and try again. For this particular script, its perfectly fine to encounter a 404- however, my console is littered with 'Failed to load resource: the server responded with a status of 404 (Not Found) messages, and I want to suppress them.

我正在编写一个脚本,它使用 XMLHttpRequest 来搜索由相对路径定义的文件,尝试根据脚本知道的其他相同域绝对路径解析该相对路径,然后尝试从该解析的文件中加载文件网址。如果我遇到 404,我只是尝试根据另一个绝对路径解析文件的相对路径,然后再试一次。对于这个特定的脚本,遇到 404 完全没问题,但是,我的控制台上到处都是“无法加载资源:服务器以 404(未找到)消息的状态响应,我想抑制它们。

There is no error to catch as far as I can see- error cases are handled by the xmlHttpRequest.onreadystatechange handler, and there is no window.onerror.

就我所见,没有要捕获的错误 - 错误情况由 xmlHttpRequest.onreadystatechange 处理程序处理,并且没有 window.onerror。

Is there any way to suppress these messages?

有没有办法抑制这些消息?

Thanks

谢谢

回答by Konrad Dzwinel

This feature was introduced last year. You can enable it here: DevTools->Settings->General->Console->Hide network messages.

此功能于去年推出。您可以在此处启用它:DevTools-> Settings-> General-> Console-> Hide network messages

Hiding network messages in Chrome DevTools

在 Chrome DevTools 中隐藏网络消息

See also Filtering the Console outputand Additional settingsin the devtools documentation.

另请参阅devtools 文档中的过滤控制台输出其他设置

回答by AmerllicA

Unfortunately, this can't be done, as this type of message in the console is printed by chrome itself. Repressing this type of message has been debated for years, but the consensus seems to be that this message is desirable.

不幸的是,这无法完成,因为控制台中的此类消息是由 chrome 本身打印的。压制这种类型的信息已经争论了多年,但共识似乎是这种信息是可取的。

回答by Abk

Use console.clear()in the catchblock or error handler function. It will clear those request error on the console immediately after it is logged.

使用console.clear()catch块或错误处理功能。它会在记录后立即在控制台上清除这些请求错误。

PLEASE NOTE

请注意

From MDN

来自 MDN

Note that in Google Chrome, console.clear() has no effect if the user has selected "Preserve log upon navigation" in the settings.

请注意,在 Google Chrome 中,如果用户在设置中选择了“在导航时保留日志”,则 console.clear() 不起作用。

Read more about it on MDN

在 MDN 上阅读更多相关信息

try {
  var req = new XMLHttpRequest();
  req.open('GET', 'https://invalidurl', false);
  req.send();
} catch(e) {
  console.clear();
}

Should log this

应该记录这个

GET https://invalidurl/net::ERR_NAME_NOT_RESOLVED

获取https://invalidurl/net::ERR_NAME_NOT_RESOLVED

but it will be cleared.

但它会被清除。