Permalink
Browse files

Fix up the javascript errors - this is looking good!

  • Loading branch information...
1 parent cfb4b5c commit ad9d211853d11ba039c2a45d4e170f5b56d7c1bd Luke Closs committed Apr 15, 2008
Showing with 72 additions and 40 deletions.
  1. +5 −8 XOCom.py
  2. +18 −17 index.html
  3. +2 −1 test-web-view.py
  4. +47 −14 web/xo-code.js
View
@@ -4,7 +4,8 @@
class XOCom:
# Constructor gives full XPCom access by default
# This should be improved for future apps that may not need/want full access
- def __init__(self):
+ def __init__(self, uri):
+ self.uri = uri
self.give_full_xpcom_access()
# Give the browser permission to use XPCom interfaces
@@ -19,18 +20,14 @@ def give_full_xpcom_access(self):
prefs.getBranch('capability.principal.').setCharPref(
'socialcalc.granted', 'UniversalXPConnect')
prefs.getBranch('capability.principal.').setCharPref(
- 'socialcalc.id',
- 'file:///home/olpc/src/testapp/web/index.html')
-
+ 'socialcalc.id', self.uri)
# Wrapper method to create a new webview embedded browser component
# Uses hulahop's WebView. Assumes that you'll want to serve
# web/index.html relative to your activity directory.
- def create_webview(self, uri=None):
+ def create_webview(self):
web_view = WebView()
- if not uri:
- uri = 'file:///home/olpc/src/testapp/web/index.html'
- web_view.load_uri(uri)
+ web_view.load_uri(self.uri)
web_view.show()
return web_view
View
@@ -1,26 +1,27 @@
<html>
<head>
- <title>Foo</title>
+ <title>XOCom Example</title>
</head>
<script type="text/javascript" src="web/jquery.js"></script>
<script type="text/javascript" src="web/xo-code.js"></script>
- <body>
- <div id="xostatus">No status</div><br />
- <textarea width="600" height="400">Default text</textarea>
- </body>
<script type="text/javascript">
- try {
- XO.register('read', function(data) {
- jQuery('textarea').html(data)
- })
- XO.register('write', function() {
- return jQuery('textarea').html()
- }
- jQuery('#xostatus').html("Registered XOCom hooks")
- }
- catch (err) {
- jQuery('#xostatus').html("Error registering hooks: " + err)
- }
+ /*
+ * This example code registers two simple functions
+ * to handle commands from the activity.
+ *
+ * The data for this page is stored in a textarea, and
+ * we just set or read the data in the textarea
+ */
+ XO.register('read', function(data) {
+ jQuery('textarea').html(data)
+ })
+ XO.register('write', function() {
+ return jQuery('textarea').html()
+ })
</script>
+ <body>
+ <div id="xo-status">No status</div><br />
+ <textarea rows="30" cols="80">Default text</textarea>
+ </body>
</html>
View
@@ -6,7 +6,8 @@
from XOCom import XOCom
# The XOCom object helps us communicate with the browser
-xocom = XOCom()
+uri = 'file:///home/olpc/src/testapp/index.html'
+xocom = XOCom(uri)
# This is just a simple way to trigger events from python for testing
# Handler for keypresses in the GTK Application
View
@@ -1,51 +1,84 @@
+/*
+ * XOCom Javascript Source
+ *
+ * This source creates a global XO object that communicates
+ * with the python XPCom code running this activity.
+ *
+ * Your HTML file should register several callbacks with
+ * the XO object to handle requests from the activity:
+ *
+ * Example: A handler for the activity read_file and write_file
+ *
+ * <script type="text/javascript">
+ * XO.register('read', function(content) {
+ * // Your code to consume the supplied content
+ * })
+ * XO.register('write', function() {
+ * // Your code to return the content to save
+ * return 'monkey'
+ * })
+ * </script>
+ *
+ * Some commands for debugging are put in an element with the
+ * id 'xo-status' if it exists.
+ */
+
var XO = window.XO = {
- callbacks: { }
+ callbacks: { },
set_status: function (msg) {
- jQuery("#xostatus").html(msg)
+ jQuery('#xo-status').html(msg)
},
observer: {
observe: function(req_obj,topic,command) {
XO.set_status("Handling " + command)
try {
// We need access to use the XPCom functions below
- netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
+ netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect")
// Unwrap the XPCom objects to get at the data passed to us
req_obj = req_obj.QueryInterface(Components.interfaces.nsIMutableArray)
- var iter = req_obj.enumerate()
- var data = iter.getNext()
- data = data.QueryInterface(Components.interfaces.nsISupportsString)
+ var command_arg = undefined
+ if (req_obj.length) {
+ var iter = req_obj.enumerate()
+ xp_arg = iter.getNext()
+ xp_arg = xp_arg.QueryInterface(Components.interfaces.nsISupportsString)
+ command_arg = xp_arg.toString()
+ }
// Execute the registered callback method
- // return_value = XO.callbacks{command}(data.toString())
- return_value = 'Poop!'
+ return_value = XO.callbacks[command](command_arg)
// Wrap the return value back into the XPCom object
var result = Components.classes["@mozilla.org/supports-string;1"].createInstance(
Components.interfaces.nsISupportsString)
result.data = return_value
- // req_obj.replaceElementAt(result, 0, false)
req_obj.clear()
req_obj.appendElement(result, false)
+ XO.set_status("Handled " + command)
}
catch (err) {
XO.set_status("Error handling event: " + err)
}
}
},
register: function(command, callback) {
- XO.callbacks{command} = callback
+ XO.callbacks[command] = callback
}
- }
+}
+/*
+ * This snippet registers the XO observer to receive commands
+ * from the python XPCom code
+ */
try {
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
var observerService = Components.classes["@mozilla.org/observer-service;1"]
.getService(Components.interfaces.nsIObserverService);
observerService.addObserver(XO.observer, 'xo-message', false);
}
catch(err) {
- jQuery('#xostatus', 'JS Error: ' + err);
+ // Wait a bit to show this error, so the page has time to load up.
+ setTimeout( function() {
+ jQuery('#xo-status', 'JS Error: ' + err);
+ }, 1000)
}
-
-XO.set_status("Loaded Javascript");

0 comments on commit ad9d211

Please sign in to comment.