我在网站的所有页面上都有这段代码,用于跟踪javascript错误
<script>
window.addEventListener('error', function(event) {
if (!window.XMLHttpRequest && !window.JSON) {
return;
}
if (!event.filename || !event.filename.includes("mywebsite.com")) {
// Exclude external scripts
return;
}
var httpRequest = new XMLHttpRequest();
httpRequest.open('POST', '/post-js-error', true);
httpRequest.setRequestHeader('Content-Type', 'application/json');
httpRequest.send(JSON.stringify(event));
});
</script>
第二条if语句旨在排除与我的代码无关的各种错误(人们拥有的浏览器插件、我在网站上安装的外部脚本等)的干扰
发生了这样的事情:除了一个病例外,这种排除方法到目前为止效果很好。我收到来自谷歌机器人的错误(用户代理“Mozilla/5.0 AppleWebKit/537.36(KHTML,如Gecko;兼容;谷歌机器人/2.1+
http://www.google.com/bot.html
)Safari/537.36”)。我得到的文件名是一个空字符串。
如果(!event.filename),则错误不应通过条件,但它确实通过了条件。
为什么?