0

I've written a simple greasemonkey script to click the next button on a popup module presentation window when I hit the right-arrow-key.

I haven't been able to find much information about this online. The code seems to work if the window is not a popup, otherwise it just prints the console.log outside of the function and never gets to the inside of the function myEventHandler. I think the problem seems to be with window.addEventListener or that the events are simply not being fired.

// ==UserScript==
// @name     Back/Next with Arrow Keys
// @version  1
// @grant    none
// @run-at          document-end
// ==/UserScript==

window.addEventListener("keypress", myEventHandler, false);

console.log('Outside function');
function myEventHandler(e) {
    console.log('fired EventHandler');
        var keyCode = e.keyCode;
        console.log(e, keyCode, e.which)
        if (keyCode === 39) {
      console.log('Right arrow key pressed!');
          var x = document.getElementsByClassName("btn cs-button inflexible  slide-control-button-next slide-lockable");
        x[0].click();
    }
};

Any help would be greatly appreciated. I'll be happy to provide any more information that's needed.

shanu2
  • 1
  • 2
  • Maybe this post helps https://stackoverflow.com/questions/47481141/how-do-i-run-code-on-dom-ready-in-greasemonkey-4 – mari Jun 28 '19 at 20:29
  • You need to provide a [mre]. Or at the very least, the full script (if it is short) and a link to the target page. – Brock Adams Jun 28 '19 at 20:43
  • @mari Thanks, I tried using DOMContentLoaded, but it didn't work. – shanu2 Jun 28 '19 at 20:58
  • @BrockAdams The website isn't accessible outside my company's network. It's basically one of those websites where you open a learning module and it opens in a new popup window. And I've updated the question with the full script. – shanu2 Jun 28 '19 at 20:59
  • That's where the [mre] comes in. You're not paying us enough to troubleshoot blind, via text exchanges. ... So, verify that you see a NEW `Outside function` appear in the console when the popup appears (and not before), but the `fired EventHandler` message never appears and niether do any errors? Also, try the script in Tampermonkey. – Brock Adams Jun 28 '19 at 21:09
  • Sounds like you are trying to automate your mandatory corporate training? Does it log 'outside function' or 'fired EventHandler'? – Jeff Jun 28 '19 at 21:14
  • @BrockAdams I know, and I apologize, but I am extremely grateful for everyone's help here. I confirm that I see a NEW `Outside function` appear in the console when the popup appears, and yes the `fired EventHandler` message never shows up and neither do any errors. I'm going to try the script in Tampermonkey and get back to you. – shanu2 Jun 28 '19 at 21:52
  • @Jeff It only logs 'outside function' and no not at all, the learning modules don't let you navigate using the arrow keys, so I'm creating a GM script for everyone to use ^_^ If I had to simply automate it, I'd have used PyAutoGUI or something haha – shanu2 Jun 28 '19 at 21:54
  • Check in the code for what launches the pop-up. If they use `window.open` and it's assigned a variable, you can do `.addEventListener` and `.document.getElementByClassName` that *should* allow you to work the pop-up DOM. You'd have to run it everytime the pop-up is launched though. I believe you can re-write the function that launches the pop-up in greasemonkey and it's override the function on the webpage. – Jeff Jun 28 '19 at 21:59
  • @Jeff The button has a `data-ctl-options='{"actiontype":"ACTION","value":"Click","sourceID":"_zl3lk_a"}'` – shanu2 Jun 28 '19 at 22:02
  • Well, if the script works fine in a regular tab, and works as described (in the comments) in a popup, then some other element may be destructively intercepting keypresses. This *might* be because the window is smaller and/or the focus has shifted. So: (1) listen for `keydown` not keypress. (2) Check and set the focus if needed. (3) Check for conflicting event handlers. (4) Set `useCapture` to true. (5) Change which element has your listener attached. – Brock Adams Jun 28 '19 at 22:21
  • 1
    @BrockAdams I tested the same script in TamperMonkey and it works just fine. This might just be something to do with Greasemonkey and popups perhaps. I will be updating the Usage instructions to use TamperMonkey instead of Greasemonkey in the Wiki where I have it up. We could still try to figure out why it doesn't work with Greasemonkey if you guys want to. :) – shanu2 Jun 29 '19 at 22:32
  • @BrockAdams I'll try your suggestions to make it work with Greasemonkey as well and get back to you. Thanks again for your help. :) – shanu2 Jun 29 '19 at 22:33

0 Answers0