代替鼠标点击,使用按键

instead of mouse click , use keypress

你好,我目前在 chrome 上的 tampermonkey 脚本上遇到问题,我必须在搜索按钮上单击鼠标才能执行脚本,我正在为代码的其他部分使用 keyup 函数这很好用,但是当谈到这个时,我似乎无法让它工作,这是我想添加按键的代码部分,我希望它是在按下“p”按钮时它会执行并工作谢谢

 function attachfunctionToSearch(){
        if( $('.btn-standard.call-to-action').length>0){
            console.log('Search Button is Ready to click');
            $('.btn-standard.call-to-action').on("click",function(){
                var t = document.createElement("style");
            t.type = "text/css",
            t.innerText = "\n    .SearchResults.ui-layout-left .listFUTItem {\n        height: 60px;\n    }\n    .SearchResults.ui-layout-left .listFUTItem .label {\n        font-size: 10px;\n  }\n    .SearchResults.ui-layout-left .auction {\n        margin-top: 0 !important;\n        font-size: 12px;\n        top: 4px;\n    }\n",
            document.head.appendChild(t)
                highlightValues();
            });
        }
        else{
            setTimeout(function(){
                attachfunctionToSearch();
            }, 1000);

        }
    }

我试过更换

.on("click", function(){

类似于

.keyup(function(p){
if (p.keycode == 80)

但它似乎不起作用,如果有人可以帮助它意味着很多谢谢

如果需要,这里是所有脚本

// ==UserScript==
// @require      http://code.jquery.com/jquery-3.5.1.min.js
// @name         Fifa By Kappers
// @namespace    http://tampermonkey.net/
// @version      0.3
// @description  try to take over the world!
// @author       Kappers
// @match       https://www.ea.com/fifa/ultimate-team/web-app/
// @grant        none
// @updateURL   https://github.com/sefbsdfb/fielkappers/tree/main/downloads/Fifa_By_Kappers.user.js
// @downloadURL https://github.com/sefbsdfb/fielkappers/tree/main/downloads/Fifa_By_Kappers.user.js

// ==/UserScript==

(function() {
    'use strict';

    // Your code here...

    let PlayerDataMap = new Map();
    function hightLightPlayer(){
        $.get( "https://raw.githubusercontent.com/sefbsdfb/fielkappers/main/Replace%20-%20Sheet1%20(3).csv", function( CSVdata) {
            var lines=CSVdata.split("\n");
            var headers=lines[0].split(",");

            for(var i=1;i<lines.length;i++){
                if(lines[i] != ""){
                var currentline=lines[i].replaceAll('"' , "").split(",");
                    var name = currentline[0].normalize("NFD").replace(/[\u0300-\u036f]/g, "").split(" ");
                    let key = name[name.length-1]+currentline[1];
                    let value = currentline[2].trim();
                    PlayerDataMap.set(key , value);
                }
            }
            console.log('loading player data....');
            if($('.icon-transfer').length>0){
                console.log('Transfers Button is Ready to click');
                $('.icon-transfer').on('mouseover' , function(){
                    setTimeout(function(){
                        attachFunctionToTranfer();
                    }, 2000);
                });
            }else{
                setTimeout(function(){
                    hightLightPlayer();
                }, 2000);
            }

        });

    }
    hightLightPlayer();
    function attachFunctionToTranfer(){
        if($('.ut-tile-transfer-market').length>0){
            console.log('Transfers banner is Ready to click');
            $('.ut-tile-transfer-market').on("mouseover",function(){
                setTimeout(function(){
                    attachfunctionToSearch();
                }, 800);
            });
        }
        else{
            attachFunctionToTranfer();
        }
    }
    function attachfunctionToSearch(){
        if( $('.btn-standard.call-to-action').length>0){
            console.log('Search Button is Ready to click');
            $('.btn-standard.call-to-action').on("click",function(){
                var t = document.createElement("style");
            t.type = "text/css",
            t.innerText = "\n    .SearchResults.ui-layout-left .listFUTItem {\n        height: 60px;\n    }\n    .SearchResults.ui-layout-left .listFUTItem .label {\n        font-size: 10px;\n  }\n    .SearchResults.ui-layout-left .auction {\n        margin-top: 0 !important;\n        font-size: 12px;\n        top: 4px;\n    }\n",
            document.head.appendChild(t)
                highlightValues();
            });
        }
        else{
            setTimeout(function(){
                attachfunctionToSearch();
            }, 1000);

        }
    }
    function highlightValues(){
        setTimeout(function(){
            console.log('Getting Everything Ready');
            getPlayerDataFromSite();
        },1000);
    }

    function getPlayerDataFromSite(){
        for(var i=0;i<=19;i++){
            var name = $('.name:eq('+i+')').text();
            name = name.normalize("NFD").replace(/[\u0300-\u036f]/g, "");
            var rating = $('.rating:eq('+i+')').text();
            var fullNAme = name.split(' ');
            var SitePlayerPrice = '';
            if($('.auction:eq('+i+')').children('div').eq(2).text().split(":")[1]!=undefined){
              SitePlayerPrice= $('.auction:eq('+i+')').children('div').eq(2).text().split(":")[1].replaceAll(",","");
            }
            var ExcelDataSheetPrice = PlayerDataMap.get(fullNAme[fullNAme.length-1]+""+rating);
            console.log('ExcelSheetPrice',fullNAme[fullNAme.length-1]+""+rating,"==>" , ExcelDataSheetPrice);
            console.log('SitePrice',name+""+rating+"==>" , SitePlayerPrice);
            if(ExcelDataSheetPrice!=undefined && parseInt(SitePlayerPrice)<=parseInt(ExcelDataSheetPrice*1.03)){
                $('.name:eq('+i+')').parent().css('background-color','orange');
            }
            if(ExcelDataSheetPrice!=undefined && parseInt(SitePlayerPrice)<=parseInt(ExcelDataSheetPrice)){
                $('.name:eq('+i+')').parent().css('background-color','green');
            }
            if(ExcelDataSheetPrice!=undefined && parseInt(SitePlayerPrice)<=parseInt(ExcelDataSheetPrice*0.97)){
                $('.name:eq('+i+')').parent().css('background-color','magenta');
            }
            if(ExcelDataSheetPrice!=undefined && parseInt(SitePlayerPrice)<=parseInt(ExcelDataSheetPrice*0.80)){
                $('.name:eq('+i+')').parent().css('background-color','red');
            }

        }
        $('.pagination.prev').keyup(function(e){
            if (e.keycode == 37)
            e.preventDefault();
            var t = document.createElement("style");
            t.type = "text/css",
            t.innerText = "\n    .SearchResults.ui-layout-left .listFUTItem {\n        height: 60px;\n    }\n    .SearchResults.ui-layout-left .listFUTItem .label {\n        font-size: 10px;\n  }\n    .SearchResults.ui-layout-left .auction {\n        margin-top: 0 !important;\n        font-size: 12px;\n        top: 4px;\n    }\n",
            document.head.appendChild(t)
            setTimeout(function(){
                getPlayerDataFromSite();
            }, 500);
        });

        $('.pagination.next').keyup(function(e){
            if (e.keycode == 39)
            e.preventDefault();
            var t = document.createElement("style");
            t.type = "text/css",
            t.innerText = "\n    .SearchResults.ui-layout-left .listFUTItem {\n        height: 60px;\n    }\n    .SearchResults.ui-layout-left .listFUTItem .label {\n        font-size: 10px;\n  }\n    .SearchResults.ui-layout-left .auction {\n        margin-top: 0 !important;\n        font-size: 12px;\n        top: 4px;\n    }\n",
            document.head.appendChild(t)
            setTimeout(function(){
                getPlayerDataFromSite();
            }, 500);
        });
    }








})();

您曾与:

.keyup(function(p){
if (p.keycode == 80)

但应该是:

.on("keyup", function(event){
  if (event.key == "p"){
     . . . .
  }
});

这是一个工作示例:

$("textarea").on("keyup", function(event){
  if (event.key == "p"){
    console.log("p key pressed");
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<textarea>Click in here and press the "p" key</textarea>

如果您只是从“单击”切换到“按键”,它将不起作用,因为您正在将事件添加到相关元素。据我了解,您希望它充当您页面上的键盘快捷键。为此,您必须将按键事件添加到“文档”中,从而使整个文档都能听到您的事件。

可以这样实现:

$(document).on('keyup', function(event) {

  if(event.which === 80){ // 80 is equivalent to "p"
   // do stuf
  } 
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

请记住,必须实施此代码段,并且 运行 页面加载后,它不应位于例如点击函数内。