Onkeydown在某些网站上有效,而在某些网站上则不然

onkeydown works on some websites and on some it doesn't

本文关键字:网站 Onkeydown 有效      更新时间:2023-09-26

我正在尝试为gmail设置一些键盘快捷键。我使用篡改猴子和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

希望对您有所帮助,