首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >热键函数用于输入特定URL中的文本

热键函数用于输入特定URL中的文本
EN

Stack Overflow用户
提问于 2022-03-10 08:27:02
回答 1查看 51关注 0票数 1

我想执行以下操作。

当我打开URL http://google.com时,如果我按下键盘组合(或热键) Z + P,那么谷歌应该在搜索框How to make userscripts中输入,当我按下键盘组合(或热键) Z + O时,谷歌应该在搜索框How to run userscripts中输入

请指导我如何实现这一功能,因为我是一个完整的新手,谢谢和问候,维基。

编辑:用于下列代码

代码语言:javascript
复制
document.onkeyup=functione{
  var e = e || window.event; // for IE to cover IEs window event-object
  if(e.altKey && e.which == 65) {
    alert('Keyboard shortcut working!');
    return false;
  }
}

对于上面的代码,当加载http://google.com并按Alt + A按钮时,Keyboard shortcut working!应该会弹出一个弹出。怎么把这个放进坦佩莫克?请指点。@进化盒

EN

回答 1

Stack Overflow用户

发布于 2022-03-10 09:14:44

正如注释中提到的,您可以为火狐使用像坦帕猴这样的浏览器插件。用户脚本允许您设置规则,这样一旦满足这些条件,它们就会自动运行。我编写了一个基本的userscript,它应该能够满足您的要求。我用篡改猴子和火狐来测试它。

注意:使用键修饰符(shift、alt、ctrl)来使用JavaScript创建热键可能更为理想,因为逻辑可以稍微简化一些,但希望这将给您提供一些可以使用的东西。

代码语言:javascript
复制
// ==UserScript==
// @name         Google HotKeys
// @version      0.1
// @include      https://www.google.com/
// @run-at document-end
// ==/UserScript==

//store the first condition as false to start
var firstCondition = false;

//change the input value
function performAction(s){
    document.querySelector(`[aria-label="Search"]`).value = s;
}
//listen for keydown event
document.onkeydown = function(e){
    if(firstCondition){
        //if first condition is true, then look for second condition
        if(e.key == "o"){
            //if o is pressed after z, then do this...
            performAction("How to run userscripts")
        }else if(e.key == "p"){
            //otherwise, if p is pressed after z, then do this...
            performAction("How to make userscripts")
        }
        //either way, reset the first condition
        firstCondition = false;
    }else{
        //if the first condition is false, then check if "z" was pressed
        firstCondition = e.key == "z";
    }
};
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71420923

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档