在FireFox / Greasemonkey中使用JavaScript发送全局按键 [英] Send Global keystroke using JavaScript in FireFox/Greasemonkey

查看:220
本文介绍了在FireFox / Greasemonkey中使用JavaScript发送全局按键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个GM脚本,捕捉按键,并以编程方式点击网页上的元素。但是,这只有在页面/标签具有焦点时才起作用。有没有什么办法从第二页/选项卡捕获击键,并将点击应用到第一页/选项卡上的元素?我知道我可以通过在JavaScript中打开它来获得对另一个窗口的引用。但是,如果在另一个域中,则无法访问该窗口中的元素。如果我不能使用Greasemonkey做到这一点,任何关于如何做到扩展而不是GM脚本的建议。真的,这个问题更多的是如何获得一个GM脚本中打开的标签/窗口的引用,或作为一个扩展。

以下是示例代码使用:
$ b $ pre $ function dispatchMouseEvent(target,event){

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

};

function mouseClick(element){

dispatchMouseEvent(element,'mouseover');
dispatchMouseEvent(element,'mousedown');
dispatchMouseEvent(element,'click');
dispatchMouseEvent(element,'mouseup');


解决方案

或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');
}

解决方案

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:

这篇关于在FireFox / Greasemonkey中使用JavaScript发送全局按键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆