QWebView: notify python when I clicked on a certain web eleemnt

G

Gelonida

I have a web page (existing page, can't modify it) and I would like
to browse it in a QtWebview. (This is already working)

Now I Wonder how I could achieve following behaviour:

When I click on a certain element e.g. "<span id="clickme"></a>"
I would like to notify my python script.

What is important:
I don't want to change anything else in the web pages behaviour (This
means if clicking on this element will call some java script functions,
load a pag, then this should still happen.


What is the correct way to do it?


Thanks in advance for any suggestions
 
Ð

Дамјан ГеоргиевÑки

I have a web page (existing page, can't modify it) and I would like
to browse it in a QtWebview. (This is already working)

Now I Wonder how I could achieve following behaviour:

When I click on a certain element e.g. "<span id="clickme"></a>"
I would like to notify my python script.

What is important:
I don't want to change anything else in the web pages behaviour (This
means if clicking on this element will call some java script
functions, load a pag, then this should still happen.


You can inject JavaScript using the method
QWebElement::evaluateJavaScript()

Also you can insert a Python object in the JS runtime with
QWebFrame::addToJavaScriptWindowObject()
see here:
http://pysnippet.blogspot.com/2010/01/calling-python-from-javascript-in-
pyqts.html


so make a Python object with some methods you'd wish to call, then
evaluate some small JS that will bind the click event on your element to
the method of the Python object.
 
G

Gelonida

Hi Damjan,

Thanks for your answer

You can inject JavaScript using the method
QWebElement::evaluateJavaScript()

Also you can insert a Python object in the JS runtime with
QWebFrame::addToJavaScriptWindowObject()
see here:
http://pysnippet.blogspot.com/2010/01/calling-python-from-javascript-in-
pyqts.html
so make a Python object with some methods you'd wish to call, then
evaluate some small JS that will bind the click event on your element to
the method of the Python object.

OK, but how do I make sure, that I don't override an existing binding to
the element's click() event?



I will start playing with it.
 
I

Ian Kelly

OK, but how do I make sure, that I don't override an existing binding to
the element's click() event?

If the element has an existing binding, then create a new JS function
that calls both the Python method and the original event handler, and
bind that function to the event.
 
G

Gelonida

Damjan,


Thanks once more for your help.
You pointed me in the right direction.

If the element has an existing binding, then create a new JS function
that calls both the Python method and the original event handler, and
bind that function to the event.

Not sure if it can be done easier, but what I roughly do now is:

top_frame = page.mainFrame()
my_qt_object = MyQtObject()
top_frame.addToJavaScriptWindowObject("pyObj", my_qt_object)
field = top_frame.findFirstElement(path)

js_inject_after = """{
var that = this;
var prev_action = this.%(evt)s;
if (prev_action == null){
prev_action = function(){};
}
this.%(evt)s = function(){
prev_action();
%(action)s;
};
}"""

prms = dict(
evt="onclick",
action=""" pyObj.action(that.id+ "clicked"); """
)

field.evaluateJavaScript(js_inject_after % prms)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top