Adding a keylistener and using javascript to follow the link in Greasemonkey

I want to create a fat monkey monkey script that will add a shortcut to the action of logging out to one email site.

There is currently an exit link for "logout & hl = en" , which has id = ": r5" . I can get the node for the link, but I can’t call, click on it.

I tried the script as below

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 that does not work. What do you think is wrong here?

Thanks J

+2
javascript javascript-events greasemonkey hyperlink
source share
2 answers

This is either a mistake or a security feature of Mozilla browsers (the developers did not decide). See: "simulating a click on the anchor using the dispatcher and initMouseEvent does not cause a real click . "

Thus, you cannot invoke the link this way (yet).

If this is a regular link, use:

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

.
If this is a JavaScript call, EG <a id=":r5" href="SomeFunc()">foo</a> uses:

 unsafeWindow.SomeFunc(); 
+1
source share

I don’t think you can click the link from the script. Instead, try redirecting to the link location:

 ..... e=document.getElementById(':r5'); document.location.href = e.href; ..... 
+2
source share

All Articles