onkeydown 在某些网站上有效,但在某些网站上无效
onkeydown works on some websites and on some it doesn't
我正在尝试为 gmail 设置一些键盘快捷键。我使用 tampermonkey 和 onkeydown 函数。我发现gmail是一个特殊的网站,因为我发现在很多网站上这种方法都有效,但在gmail上却不行。我尝试了这 3 个选项,但 none 有效。你有什么建议?
// @match https://mail.google.com // ALL HAVE THIS LINE
选项 1
document.onkeydown = keydown;
function keydown(evt){
console.log("hhwhehehheeheh");
}
选项 2
document.documentElement.onkeydown = keydown;
function keydown(evt){
console.log("hhwhehehheeheh");
}
选项 3
document.body.focus();
document.documentElement.onkeydown = keydown;
function keydown(evt){
console.log("hhwhehehheeheh");
}
你的选项1是正确的,问题是模式:// @match https://mail.google.com
改为使用以下模式:// @match https://mail.google.com/*
您的模式的问题是仅适用于没有路径的 https://mail.google.com
,但是来自 gmail 的内容不是从 https://mail.google.com
提供的,它总是使用路径为 https://mail.google.com/mail/...
的位置所以你必须在模式中添加 *
才能加载你的脚本。
将第二个 @match
与您的脚本一起使用对我来说效果很好:
// ==UserScript==
// @name New Userscript
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author You
// @match https://mail.google.com/*
// @grant none
// ==/UserScript==
/* jshint -W097 */
'use strict';
document.onkeydown = keydown;
function keydown(evt){
console.log("hhwhehehheeheh");
}
有关详细信息,请参阅 @match
希望对您有所帮助,
我正在尝试为 gmail 设置一些键盘快捷键。我使用 tampermonkey 和 onkeydown 函数。我发现gmail是一个特殊的网站,因为我发现在很多网站上这种方法都有效,但在gmail上却不行。我尝试了这 3 个选项,但 none 有效。你有什么建议?
// @match https://mail.google.com // ALL HAVE THIS LINE
选项 1
document.onkeydown = keydown;
function keydown(evt){
console.log("hhwhehehheeheh");
}
选项 2
document.documentElement.onkeydown = keydown;
function keydown(evt){
console.log("hhwhehehheeheh");
}
选项 3
document.body.focus();
document.documentElement.onkeydown = keydown;
function keydown(evt){
console.log("hhwhehehheeheh");
}
你的选项1是正确的,问题是模式:// @match https://mail.google.com
改为使用以下模式:// @match https://mail.google.com/*
您的模式的问题是仅适用于没有路径的 https://mail.google.com
,但是来自 gmail 的内容不是从 https://mail.google.com
提供的,它总是使用路径为 https://mail.google.com/mail/...
的位置所以你必须在模式中添加 *
才能加载你的脚本。
将第二个 @match
与您的脚本一起使用对我来说效果很好:
// ==UserScript==
// @name New Userscript
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author You
// @match https://mail.google.com/*
// @grant none
// ==/UserScript==
/* jshint -W097 */
'use strict';
document.onkeydown = keydown;
function keydown(evt){
console.log("hhwhehehheeheh");
}
有关详细信息,请参阅 @match
希望对您有所帮助,