Javascript onkeydown在一些网站上有效,在一些网站上不起作用';T
我正在尝试为gmail设置一些键盘快捷键。我使用tampermonkey和onkeydown函数。我发现gmail是一个特别的网站,因为我发现在很多网站上这种方法都有效,但在gmail上却不起作用。我试过这3种方法,但都不管用。你有什么建议Javascript onkeydown在一些网站上有效,在一些网站上不起作用';T,javascript,greasemonkey,tampermonkey,Javascript,Greasemonkey,Tampermonkey,我正在尝试为gmail设置一些键盘快捷键。我使用tampermonkey和onkeydown函数。我发现gmail是一个特别的网站,因为我发现在很多网站上这种方法都有效,但在gmail上却不起作用。我试过这3种方法,但都不管用。你有什么建议 // @match https://mail.google.com // ALL HAVE THIS LINE 选择1 document.onkeydown = keydown; function keydown(evt
// @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是正确的,问题在于模式:
/@matchhttps://mail.google.com
改为使用以下模式:/@matchhttps://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");
}
有关更多详细信息,请参阅
希望有帮助,选项1对我有效。它在gmail中是否明确有效?嗯,我没有看到任何控制台输出,因此我认为它不起作用。