views:

50

answers:

2

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

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 ?

Thanks J

+1  A: 

I do not think that you will be able to click a link from the script. You should try to redirect to the link location instead:

.....
e=document.getElementById(':r5'); 
document.location.href = e.href;
.....
ZloiAdun
A: 

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;

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

unsafeWindow.SomeFunc();
Brock Adams