简体   繁体   English

使用FireFox / Greasemonkey中的JavaScript发送全局击键

[英]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. 我目前有一个GM脚本,可以捕获按键并以编程方式单击网页上的元素。 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. 我知道我可以通过在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. 如果我不能使用Greasemonkey执行此操作,那么有关如何将其作为扩展而非GM脚本的任何建议。 Really, this question is more how to get a reference to an open tab/window in a GM script, or as an extension. 实际上,这个问题更多的是如何获得GM脚本中的打开选项卡/窗口的引用,或作为扩展。

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. 这不能通过设计在JS或Greasemonkey中完成。 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: 以下是一些附加资源,我发现有用:

你可以在你的GreaseMonkey脚本之前使用对window.focus()的javascript调用吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM