繁体   English   中英

onkeydown在某些网站上有效,在某些网站上则无效

[英]onkeydown works on some websites and on some it doesn't

我正在尝试为gmail设置一些键盘快捷键。 我使用tampermonkey和onkeydown函数。 我发现gmail是一个特殊的网站,因为我发现在许多网站上都可以使用这种方法,但不适用于gmail。 我尝试了这3个选项,但都无济于事。 你有什么建议?

   // @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

希望能帮助到你,

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM