[webkit-help] Perform an action when user clicks on node with mouse

Julien Chaffraix julien.chaffraix at gmail.com
Sun May 16 21:31:03 PDT 2010


Hi,

> I'm trying to perform an action when a user clicks on a particular DOM
> element in my document.  In my specific case, when the user clicks on
> an image in my application, I need to modify its "src" attribute.  It
> seems like the approach I need to take is to call addEventListener to
> add a "click" event listener when adding my image element.  In the
> event listener, I could then change the "src" attribute as needed.  Is
> this the correct approach to take or is there a better way to
> accomplish this?

This is a correct approach. If you choose it, I would recommend doing
everything in JavaScript by using a method to inject a custom script
from C++ (in Chromium this is WebView::addUserScript but the naming
varies a bit per port). You can listen to DOM mutation events on the
document to get notified for each new image (see
http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-MutationEvent)
and add your click listener at that time.

An alternative would be to act upon receiving the "click" event in
your event sink: you could do an (expensive) hit test to determine
what type of node is the target of the click (by checking the node's
tag) and do your processing at that time. It needs the hist testing
logic and the DOM to be exposed in your port thought (I don't think
they are exposed in Chromium right now).

Regards,
Julien


More information about the webkit-help mailing list