为什么console.log在Chrome的某些网站上是空的? [英] Why is console.log an empty function on some sites in Chrome?

查看:135
本文介绍了为什么console.log在Chrome的某些网站上是空的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



window.addEventListener('keypress',function(e){ console.log('hello')},true)



(注意:第三个参数如何设置为真实可以使事件捕获,这导致事件首先被窗口截取,然后被子元素消耗。)



尝试按一些键。注意 hello 不会输出到控制台。添加 keydown keyup 的事件侦听器不会更改任何内容。



hello 将在大多数网站上获得输出,但不会在Twitter或Gmail等网站上获得输出。



为什么?什么是停止事件侦听器?



编辑似乎在Firefox上正常工作。但不是Chrome。为什么Chrome不按预期方式触发事件监听器?



编辑2:如下面的几个人推断的, console.log 是Chrome上用于Twitter和Gmail等网站的空白功能。为什么是这样?

解决方案

因为这些网站已经具体设置(对于webkit显然):

  console.log = function(){}; 

但是,在Chrome中,您可以恢复原始的 log()在控制台中发出此命令的功能:

  console.log = console .__ proto __。log 

然后你可以这样做:

  window.addEventListener('keypress',function(e){console.log('hello')},true)

它应该按预期工作。


Go to Twitter's login page and type the following in the console:

window.addEventListener('keypress', function(e){console.log('hello')}, true)

(NOTE: how the third parameter is set to true which enables event capture. This causes events to be intercepted first by the window before being consumed by a child element.)

Try pressing some keys. Notice how hello isn't output to the console. Adding an event listener for keydown or keyup doesn't change anything.

hello will get output on most websites, but not on sites like Twitter or Gmail.

Why? What's stopping the event listener?

EDIT: Seems to work as expected on Firefox. But not Chrome. Why isn't Chrome firing the event listener as expected?

EDIT 2: As deduced by a few people below, console.log is an empty function on Chrome for sites like Twitter and Gmail. Why is that?

解决方案

Because those sites have specifically set (for webkit apparently):

console.log = function(){};

However, in Chrome you can restore the original log() functionality by issuing this command in console:

console.log = console.__proto__.log

Then you can do this:

window.addEventListener('keypress', function(e){console.log('hello')}, true)

And it should work as expected.

这篇关于为什么console.log在Chrome的某些网站上是空的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆