Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send Global keystroke using JavaScript in FireFox/Greasemonkey

I currently have a GM script that captures a keystroke and programmatically clicks an element on a webpage. However, this only works when the page/tab has focus. Is there any way to capture the keystroke from the second page/tab and apply the click to an element on the first page/tab? I know I can get a reference to another window by opening it in JavaScript. However, I can't access elements in that window if it is on another domain. If I cannot do this using Greasemonkey, any suggestions on how to accomplish it as an extension instead of a GM script. Really, this question is more how to get a reference to an open tab/window in a GM script, or as an extension.

Here is the sample code I'm working with:

function dispatchMouseEvent(target, event) {

  var e = document.createEvent("MouseEvents");
  e.initMouseEvent(event, true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);  
  target.dispatchEvent(e);

};

function mouseClick(element) {

    dispatchMouseEvent(element, 'mouseover');
    dispatchMouseEvent(element, 'mousedown');
    dispatchMouseEvent(element, 'click');
    dispatchMouseEvent(element, 'mouseup');
}
like image 722
radicalpi Avatar asked Jul 09 '26 16:07

radicalpi


1 Answers

This cannot be done in JS or Greasemonkey, by design. It used to be a huge security hole. You'll have to write an extension (add-on).

Here are some add-on resources, I've found helpful:

  • Mozilla extensions home page
  • Add-on Builder
  • 10 Things They Never Tell You In Firefox Extension School
  • Developing Firefox Extensions – Paper
like image 180
Brock Adams Avatar answered Jul 11 '26 10:07

Brock Adams



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!