我想执行以下操作。
当我打开URL http://google.com时,如果我按下键盘组合(或热键) Z + P,那么谷歌应该在搜索框How to make userscripts中输入,当我按下键盘组合(或热键) Z + O时,谷歌应该在搜索框How to run userscripts中输入
请指导我如何实现这一功能,因为我是一个完整的新手,谢谢和问候,维基。
编辑:用于下列代码
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!应该会弹出一个弹出。怎么把这个放进坦佩莫克?请指点。@进化盒
发布于 2022-03-10 09:14:44
正如注释中提到的,您可以为火狐使用像坦帕猴这样的浏览器插件。用户脚本允许您设置规则,这样一旦满足这些条件,它们就会自动运行。我编写了一个基本的userscript,它应该能够满足您的要求。我用篡改猴子和火狐来测试它。
注意:使用键修饰符(shift、alt、ctrl)来使用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";
}
};https://stackoverflow.com/questions/71420923
复制相似问题