0

I am trying to make a script for DeviantArt that clicks "Remove from notifications" when I press the delete key. Deviantart allows the user to follow artists and provides any new artwork under a common notification system. When you click some artwork from notifications there is a button on the right that lets the user remove that artwork from notifications.

Image Link

Here is the code I've tried which isn't working.

// ==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);
})();

When I manually enter document.querySelector(".remove-message-button").click(); into the console it works as expected.

always-a-learner
  • 3,671
  • 10
  • 41
  • 81

1 Answers1

0

I assume that you want to create a shortcut key for the functionality Remove from notifications, through pressing down on a key.

I found a piece of sample code online http://jsfiddle.net/arunpjohny/SZ9TG/1/

For it to work, move your cursor to the UI output area, then click anywhere on the output section, I think this way it knows that you are on this window. Then it listen to your keydown event. It uses window.addEventListener instead of document.addEventListener

grepLines
  • 2,478
  • 3
  • 21
  • 32
  • I replaced document.querySelector(".remove-message-button").click(); // this will trigger the click event with your line. And it didn't make a difference. I did come across this link though which shows the javascript for what I'm trying to do but requires a chrome extension while I use firefox. https://bakkeby.deviantart.com/art/How-to-add-dA-keyboard-shortcuts-291719603 The problem is not the querySelector line, it does its function when I enter it directly into the console. The if statement isn't triggering I think. – Henry Chalice Jun 24 '17 at 04:14
  • ok, can you debug and see if the event is triggered? I mean the `keydown` event. – grepLines Jun 24 '17 at 04:33
  • @HenryChalice Is it correct that you want to handle `Remove from notifications` when user pressing down the key, which shared the same functionality as when user click on the button? So what you are trying to do is remove by shortcut, is it correct? – grepLines Jun 24 '17 at 05:02