views:

323

answers:

3

Hi All,

I'm trying to write a Greasemonkey script that works with Gmail. the script is working good but I need to add simulate a refresh of the Gmail page by "hitting" programmatically the Refresh button. is this possible ? FYI, the refresh link in Gmail isn't a link like but rather : Refresh I've tried to use the click() method but it doesn't work ...

Thank you for your help !

+1  A: 

If you integrate jQuery into Greasemonkey, you could easily use the .trigger('click') method, for a true dispatchEvent/fireEvent work-around.

Alternatively, couldn't you just set the window.location.href to http://mail.google.com/mail/#inbox?

Alexander Gyoshev
I'm already using jQuery in my script. I've tried trigger('click') but it doesn't work.I'm trying the other method (using dispatchEvent) and I'm stuck with another problem : how to transform a jQuery object to a javascript object ? 'cause I'm using jQuery to select the div (AP class) and I need to "attach" it to myDiv.dispatchEvent(evt).
Med
you can get a DOM element from a jQuery object like this: $('.selector')[0] - where 0 is the index of the element in the jQuery collection
Alexander Gyoshev
thank you so much, it's working :)
Med
The link about integrating jQuery into GM scripts describes an unsecure procedure that relies heavily on `unsafeWindow`. I recommend *not* following that advice.
phyzome
A: 

Use fireEvent in IE or dispatchEvent for everything else.

Rakesh Pai
A: 

Med: could you post your working code with dispatchEvent?

csulok
simulateRefreshClick : function () { var evt = document.createEvent("MouseEvents"); evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);//simlulate the clic on refresh button var resfresh_link = $(".AP:first", kwagaPlugin.getActiveViewElement())[0]; resfresh_link.dispatchEvent(evt);},
Med