Trying to set up a keyboard shortcut of a button inside a webpage and press it multiple times

Viewed 94

My work is to watch videos, select some options and then press the "Submit" button. The thing is that the options I have to select are on the left side of the webpage and the "Submit" button is on the right side. I think I will be able to do my work faster if I can make a keyboard shortcut to press the "Submit" button. I somehow built this code

var button1 = document.getElementById("_SSsubmit"); 
 
window.addEventListener('keydown', function(e) { 
  if (e.altKey == true && e.keyCode == 83) 
    button1.click(); 
});

I put it in Chrome's Developer Tools Console and it works only 1 time. The next time when I press "Alt + S" - nothing happens. Is there a way I can make it work while the tab is opened?

1 Answers

The event listener itself will trigger multiple times so long as the page isn't reloaded, you can test this by replacing the button1.click() with a simple console.log(). It seems to me that either:

a) the page is being reloaded every time you submit, in which case you would need to paste the code into the console every time the page loads or

b) the submit button element is getting replaced with a new element each time, in which case you would need to re-assign button1 every time. Most likely, the page is being reloaded.

In either case, your best bet here is probably writing a small Chrome extension so that you won't need to keep pasting code into the console.

Related