【发布时间】:2019-05-10 15:36:49
【问题描述】:
这是我的脚本(特意简化):
// ==UserScript==
// @name StackOverflowExample
// @include https://stackoverflow.com/*
// @version 1
// @grant none
// ==/UserScript==
document.addEventListener('keydown', (e) => {
console.log('I print before the "e"')
conosel.log({e})
console.log('I print after the "e"')
})
当这个脚本加载到我的页面(堆栈溢出)时,我看到“我在“e”之前打印”被打印到控制台,但我没有看到“e”或“我在之后打印” “e”'被记录下来。这是为什么呢?
我尝试添加 e.preventDefault() 之类的内容,但没有任何区别。
令人费解的是,事件监听器内部的这种东西居然还能用:
document.addEventListener('keydown', (e) => {
if(e.keyCode !== 40)){
console.log('you pressed some random key')
} else {
console.log('you pressed the "UP" arrow key')
}
})
所以e 对象被定义(只需按任意键,然后按“向上”)。有什么想法吗?
编辑:似乎我的第二部分错了,(虽然我很确定我看到它在另一个网站上工作......)
浏览器 = firefox 63.0.3(64 位)
操作系统 = Ubuntu 18.04
GreaseMonkey = 4.7
【问题讨论】:
标签: javascript firefox userscripts tampermonkey greasemonkey-4