簡體   English   中英

如何制作一個油脂猴子腳本,當按下某個鍵時,該腳本單擊頁面上的一個按鈕?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM