Skip to content
Browse files

get synchronous python to js calls working

  • Loading branch information...
1 parent 91d37cf commit ba3578143b56a33b117130c09ba53fad3b4aed9b Luke Closs committed Apr 14, 2008
Showing with 53 additions and 48 deletions.
  1. +14 −36 index.html
  2. +39 −12 test-web-view.py
View
50 index.html
@@ -2,50 +2,28 @@
<head>
<title>Foo</title>
</head>
+ <script type="text/javascript" src="web/jquery-1.2.3.js"></script>
+ <script type="text/javascript" src="web/xo-code.js"></script>
<script type="text/javascript">
document.set_msg = function (msg) {
- document.getElementById('baz').innerHTML = msg;
+ jQuery('#bar').html(msg)
}
- document.observer = {
- observe: function(subject,topic,data){
- document.set_msg("Topic sent: " + topic + " - " + data);
- }
- };
-
-
- function foo_button(self) {
- try {
- netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
- var observerService = Components.classes["@mozilla.org/observer-service;1"]
- .getService(Components.interfaces.nsIObserverService);
- observerService.addObserver(document.observer,"my-topic",false);
- observerService.notifyObservers(self,"my-topic","foo");
- }
- catch(err) {
- document.set_msg(err)
- }
+ try {
+ XO.read_hook = function (data) {
+ jQuery('textarea').html(data)
+ }
+ XO.write_hook = function () {
+ return jQuery('textarea').html()
+ }
}
- function bar_button(self) {
- try {
- netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
- var observerService = Components.classes["@mozilla.org/observer-service;1"]
- .getService(Components.interfaces.nsIObserverService);
- observerService.addObserver(document.observer,"my-topic",false);
- observerService.notifyObservers(self,"my-topic","bar");
- }
- catch(err) {
- document.set_msg(err)
- }
+ catch (err) {
+ document.set_msg(err)
}
-
</script>
<body>
- <form>
- <input type="button" name="foo" value="Foo" onClick="foo_button()">
- <input type="button" name="bar" value="Bar" onClick="bar_button()">
- </form>
- <div id="baz">Empty</div>
+ <div id="status">No status</div><br />
+ <textarea width="600" height="400">Default text</textarea>
</body>
</html>
View
51 test-web-view.py
@@ -5,20 +5,52 @@
from hulahop.webview import WebView
from xpcom import components
-class TestObserver:
- _com_interfaces_ = components.interfaces.nsIObserver
-
- def observe(self, subject, topic, data):
- print "observed! - %s - %s" % (topic, data)
-
def quit(window):
hulahop.shutdown()
gtk.main_quit()
+# This is just a simple way to trigger events from python for testing
+def keypress(window, key2):
+ key = key2.string
+ if key == 'w': # Send an event to the javascript handler
+ result = send_to_browser('write')
+ if result:
+ print "Result from browser - '%s'"%result
+ else:
+ print "No data received in the browser's response"
+ if key == 'r':
+ import time
+ result = send_to_browser('read', "I am a monkey - %s"%time.time())
+ if key == 'q':
+ quit(None)
+
+observerService = components.classes["@mozilla.org/observer-service;1"]
+ob_serv = observerService.getService(components.interfaces.nsIObserverService);
+
+def send_to_browser(command, data=None):
+ array = components.classes["@mozilla.org/array;1"].createInstance(
+ components.interfaces.nsIMutableArray)
+ if data:
+ str = components.classes["@mozilla.org/supports-string;1"].createInstance(
+ components.interfaces.nsISupportsString)
+ str.data = data
+ print "Sending (%s) to browser"%str.data
+ array.appendElement(str, False)
+ ob_serv.notifyObservers(array, "xo-message", command);
+ # check if the browser returned anything
+ if array.length:
+ iter = array.enumerate()
+ result = iter.getNext()
+ result = result.QueryInterface(components.interfaces.nsISupportsString)
+ return result.toString()
+ return None
+
window = gtk.Window()
window.set_default_size(600, 400)
window.connect("destroy", quit)
+window.connect("key_press_event", keypress)
+# Give the browser permission to use XPCom interfaces
pref_class = components.classes["@mozilla.org/preferences-service;1"]
prefs = pref_class.getService(components.interfaces.nsIPrefService)
prefs.getBranch('signed.applets.').setBoolPref('codebase_principal_support', True);
@@ -27,16 +59,11 @@ def quit(window):
prefs.getBranch('capability.principal.').setCharPref(
'socialcalc.id', 'file:///home/olpc/src/testapp/index.html')
+# Create the embedded browser
web_view = WebView()
web_view.load_uri('file:///home/olpc/src/testapp/index.html')
web_view.show()
-observerService = components.classes["@mozilla.org/observer-service;1"]
-ob_serv = observerService.getService(components.interfaces.nsIObserverService);
-ob_serv.addObserver(TestObserver(),"my-topic",False);
-
-
window.add(web_view)
window.show()
-
gtk.main()

0 comments on commit ba35781

Please sign in to comment.
Something went wrong with that request. Please try again.