PDA

View Full Version : Implemetation of dispatchEvent for custom JavaScript object



platon
3rd October 2012, 08:51
There is a need to create in C++ an object which implements EventTarget DOM interface and export it to JavaScript.
I already managed to implement addEventListener and removeEventListener methods. But I still need to implement dispatchEvent.
In my implementation I just store JavaScript function names as QString and then call them using evaluateJavaScript() when correspondent event happens.
But I faced the problem that when JavaScript event is fired then event object which is passed to event handler has to have target field.
But unfortunately in JavaScript it is read only. So I'm stuck with the problem how to say JavaScript that target of the event is my object.
Usually target is set automatically in JavaScript when web element dispatches correspondent event.

To clarify why do I need this. Currently I'm trying to add audioTracks and videoTracks methods of HTML5 video tag which are currently are unsupported by WebKit.
That is, I want make it possible to support the following JavaScript code:


p = document.getElementById("player");
p.audioTracks.addEventListener("addtrack", evt_handler, false);

where "player" corresponds to <video> element.
audioTracks have to implement the next interface:


interface AudioTrackList : EventTarget {
readonly attribute unsigned long length;
getter AudioTrack (unsigned long index);
AudioTrack? getTrackById(DOMString id);

attribute EventHandler onchange;
attribute EventHandler onaddtrack;
attribute EventHandler onremovetrack;
};

platon
4th October 2012, 14:08
Export of audioTracks object I do in the following way:


element().webFrame()->addToJavaScriptWindowObject("mediaTracks", m_media_tracks);
element().evaluateJavaScript(QString("this.audioTracks = mediaTracks.audioTracks;"));

May be it is possible to make audioTracks available in JavaScript as a separate web element (e.g. inherit it from QWebElement)? But how in that case I can add to it my custom functionality?