I'm converting a WebKitGTK+ project from C++ to Python (using PyGI). I need to detect any time an <input>
element on the page gains or loses focus. In the C++ project I did it using this code (based on this example):
WebKitDOMDocument* document = webkit_web_view_get_dom_document(view);
WebKitDOMNodeList* nodes = webkit_dom_document_get_elements_by_tag_name(document, "*");
int len = webkit_dom_node_list_get_length(nodes);
for(int i = 0; i < len; i++) {
WebKitDOMNode* node = webkit_dom_node_list_item(nodes, i);
if(WEBKIT_DOM_IS_HTML_INPUT_ELEMENT(node)) {
webkit_dom_event_target_add_event_listener(WEBKIT_DOM_EVENT_TARGET(node), "focus", G_CALLBACK(inputFocus), false);
webkit_dom_event_target_add_event_listener(WEBKIT_DOM_EVENT_TARGET(node), "blur", G_CALLBACK(inputBlur), false);
}
}
I tried to do the same thing in Python and came up with:
document = view.get_dom_document()
nodes = document.get_elements_by_tag_name('*')
for i in range(nodes.get_length()):
node = nodes.item(i)
if isinstance(node, WebKit.DOMHTMLInputElement):
node.add_event_listener('focus', inputFocus, False)
The problem is, DOMHTMLInputElement
doesn't have an add_event_listener
method. Based on the C bindings, it should be part of DOMEventTarget
(which DOMHTMLInputElement
extends), but it's missing along with remove_event_listener
, although dispatch_event
somehow made it in. It might be due to this WebKit bug; I'm not certain
Is there any way to do this in Python? It doesn't have to use the same scheme I was using in C++, I just need some way to register a callback that invokes every time focus changes on certain elements on the page