添加keylistener并使用javascript单击Greasemonkey中的链接 [英] Adding keylistener and using javascript to click a link in Greasemonkey

查看:95
本文介绍了添加keylistener并使用javascript单击Greasemonkey中的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个油腻的猴子脚本,它将在一个邮件站点中添加用于注销操作的快捷键。

I want to create a greasy monkey script , which will add a shortcut key for logout action in one mail site.

目前注销链接? logout& hl = en,其中 id =:r5。我能够获得链接的节点但无法调用点击它。

Currently the logout link "?logout&hl=en" which have an id=":r5". I am able to get the node for the link but not able to call click on it.

我尝试了以下脚本

I tried the script as following

function key_event(event){
    GM_log("Hello");
    GM_log(event.keyCode);
    //if(event.keyCode != 112) return;
    e=document.getElementById(':r5');
    if(!e) {return;}
    var evObj = document.createEvent('MouseEvents');
    evObj.initMouseEvent(('click'),true,true,window,0,0,0,0,0,false,false,false,false,0,null);
    GM_log(e);
    e.dispatchEvent(evObj);
}

document.addEventListener("keypress", key_event, true);


但这是不工作你认为这里有什么问题?

But this is not working . What do you think is wrong here ?

谢谢
J

推荐答案

这是Mozilla浏览器的错误或安全功能(开发人员尚未决定)。请参阅:使用dispatchEvent模拟锚定点击并且initMouseEvent不会触发真正的点击

This is either a bug or a security "feature" of Mozilla browsers (developers haven't decided). See: "simulating a click on an anchor using dispatchEvent and initMouseEvent does not trigger a real click".

所以你不能以这种方式触发链接(现在)。

So you can't trigger a link that way (for now).

如果它是普通链接使用:

If it is an ordinary link use:

var sTargetURL  = document.getElementById(':r5').href;
window.location.href = sTargetURL;



如果是JavaScript电话,EG < a id =:r5href =SomeFunc()> foo< / a> 使用:

unsafeWindow.SomeFunc();

这篇关于添加keylistener并使用javascript单击Greasemonkey中的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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