[英]How to make a greasemonkey script that clicks a button on page when a key is pressed?
我正在尝试为DeviantArt创建一个脚本,当我按Delete键时单击“从通知中删除”。 Deviantart允许用户关注艺术家并在公共通知系统下提供任何新作品。 当您单击通知中的某些插图时,右侧会有一个按钮,使用户可以从通知中删除该插图。
这是我尝试过的无效代码。
// ==UserScript==
// @name Deviations remove shortcut
// @namespace DeviationsRemShortcut
// @include http://www.deviantart.com/art/*
// @include https://www.deviantart.com/art/*
// ==/UserScript==
(function(){
document.addEventListener('keydown', function(e) {
// pressed del
if (e.keyCode == 46 && !e.shiftKey && !e.ctrlKey && !e.altKey && !e.metaKey) {
document.querySelector(".remove-message-button").click(); // this will trigger the click event
}
}, false);
})();
当我手动输入document.querySelector(“。remove-message-button”)。click(); 进入控制台,它可以按预期工作。
我假设您要通过按下某个键来为“ Remove from notifications
”功能创建快捷键。
我在网上找到了一段示例代码http://jsfiddle.net/arunpjohny/SZ9TG/1/
为了使它起作用,请将光标移到UI输出区域,然后在输出部分上单击任意位置,我认为这样就知道您在此窗口中。 然后,它会监听您的keydown
事件。 它使用window.addEventListener
而不是document.addEventListener
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.