I want to trigger events from a Firefox extension, specifically click events. I’ve tried jQuery’s .click() as well as the whole:
var evt = document.createEvent("HTMLEvents");
evt.initEvent("click", true, false );
toClick[0].dispatchEvent(evt);
This is not working for me, and I was wondering if this is even possible? (to trigger events from a Firefox extension)?
I think it may have something to do with what document I create the event on…but I’m not sure.
If so, how does one do it?
First of all, for click events, you need to create an event object with type
MouseEvents, notHTMLEvents, and useevent.initMouseEventinstead ofevent.initEvent.To access the
documentof the current tab of Firefox from a XUL overlay, you can use thecontent.documentproperty, but since you already have access to the DOM element you want to click, you can use theNode.ownerDocumentproperty, which will refer to the top-leveldocumentobject for this node.I have made a simple function to simulate MouseEvents:
Usage:
Check a test usage here.
You can pass also an object as the third argument, if you want to change the values of the event object properties.