开发者

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开发者_如何学Python 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');
}


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


Can you precede your GreaseMonkey script with a javascript call to window.focus()?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜