转到 Twitter 的登录页面并在控制台中输入以下内容:
Go to Twitter's login page and type the following in the console:
window.addEventListener('keypress', function(e){console.log('hello')}, true)
(注意:如何将第三个参数设置为 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.)
尝试按一些键.注意 hello 是如何不输出到控制台的.为 keydown 或 keyup 添加事件监听器不会改变任何东西.
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 将在大多数网站上获得输出,但不会在 Twitter 或 Gmail 等网站上获得输出.
hello will get output on most websites, but not on sites like Twitter or Gmail.
为什么?是什么阻止了事件侦听器?
Why? What's stopping the event listener?
似乎在 Firefox 上按预期工作.但不是铬.为什么 Chrome 没有按预期触发事件侦听器?
Seems to work as expected on Firefox. But not Chrome. Why isn't Chrome firing the event listener as expected?
编辑 2:正如下面的一些人所推断的,console.log 是 Chrome 上的一个空函数,用于 Twitter 和 Gmail 等网站.这是为什么呢?
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?
因为那些网站已经专门设置了(显然是针对webkit):
Because those sites have specifically set (for webkit apparently):
console.log = function(){};
但是,在 Chrome 中,您可以通过在控制台中发出以下命令来恢复原始 log() 功能:
However, in Chrome you can restore the original log() functionality by issuing this command in console:
console.log = console.__proto__.log
那么你可以这样做:
window.addEventListener('keypress', function(e){console.log('hello')}, true)
它应该按预期工作.
这篇关于为什么在 Chrome 的某些网站上 console.log 是一个空函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
在 Angular 2/Typescript 中使用 IScrollUse IScroll in Angular 2 / Typescript(在 Angular 2/Typescript 中使用 IScroll)
Anime.js 在 Ionic 3 项目中不起作用anime.js not working in Ionic 3 project(Anime.js 在 Ionic 3 项目中不起作用)
Ionic 3 - 使用异步数据更新 ObservableIonic 3 - Update Observable with Asynchronous Data(Ionic 3 - 使用异步数据更新 Observable)
Angular 2:在本地 .json 文件中找不到文件Angular 2: file not found on local .json file(Angular 2:在本地 .json 文件中找不到文件)
在 Ionic 2 中,如何创建使用 Ionic 组件的自定义指In Ionic 2, how do I create a custom directive that uses Ionic components?(在 Ionic 2 中,如何创建使用 Ionic 组件的自定义指令?)
将 ViewChild 用于动态元素 - Angular 2 &离子2Use ViewChild for dynamic elements - Angular 2 amp; ionic 2(将 ViewChild 用于动态元素 - Angular 2 amp;离子2)