3

I am very new to Python programming, so please bear with me.

I have an HTML file with several div layers. This file is opened in a webkit.WebView object. Each div layer saves a value in a global variable (JavaScript) when clicked upon.

How can I read the value of that global JavaScript variable from my Python script?

I found some answers but they don't seem to fit my situation (but I can be wrong, of course):

Passing JavaScript variable to Python

Parse JavaScript variable with Python

[EDIT]
I'm using webkit.WebView because I have to show this in an existing glade (libglade) application.

Community
  • 1
  • 1
Schoelje
  • 31
  • 1
  • 4

2 Answers2

0

try this out. It uses the addToJavaScriptWindowObject method to add a QObject into the QWebView. This will enable communication between your python script and the HMTL/Javascript in your webview. The example below will let you change the value of the javascript global variable message to whatever value you want through a JavaScript prompt, then whenever you click on the Python Print Message link it will execute your python code that will take the javascript value and just print it to the console.

import sys
from PyQt4 import QtCore, QtGui, QtWebKit

HTML = """
<html><body onload="broker.print_msg(message)">
<script>var message = 'print_msg message'</script>
<a href="javascript:message=prompt('Enter Message')">Change Message</a><br/>
<a href="javascript:broker.print_msg(message)">Python Print Message</a>
</body></html>
"""

class Broker(QtCore.QObject):
    @QtCore.pyqtSlot(str)
    def print_msg(self, data):
        print data

app = QtGui.QApplication(sys.argv)
view = QtWebKit.QWebView()
view.page().mainFrame().addToJavaScriptWindowObject('broker', Broker(view))
view.setHtml(HTML)
window = QtGui.QMainWindow()
window.setCentralWidget(view)
window.show()
sys.exit(app.exec_())
Marwan Alsabbagh
  • 25,364
  • 9
  • 55
  • 65
  • I used webkit.WebView because I have to show it in an existing glade (libglade) application...is this then still doable? – Schoelje Nov 07 '12 at 18:36
  • can you post some of your code in the question. Are you using gtk, because webkit has bindings for qt and gtk. which one are you using – Marwan Alsabbagh Nov 07 '12 at 18:49
  • It's been a while but until now I haven't gotten it working. Like Marwan's example, any other solution I found uses Qt as well. My project is an old(er) project that uses libglade and pyGTK and python (obviously). It seems that the way I choose is not the right way and I'm thinking about dropping this approach (loading HTML/Javascript into webkit.WebView and read the returned value) in favor for a python/pyGTK only solution (but not as pritty). – Schoelje Nov 21 '12 at 07:57
0

I know this is old question, but still answering it with the hope that it will be useful to someone.

You can use alert handler and read the value in python side. http://webkitgtk.org/reference/webkitgtk/stable/webkitgtk-webkitwebview.html#WebKitWebView-script-alert

Example on button click you can have action that says

 alert("Button Clicked");

On python side you will get the alert notification and you can parse the string. If the object is not a simple object, you will have to convert it to string format that can be parsed on python side. I have seen few examples of alert handler. https://github.com/nhrdl/notesMD/blob/master/notesmd.py is one that I wrote and uses alert handlers to pass lot of data between javascript and python

user937720
  • 182
  • 1
  • 1
  • 5