Index: ports/ipython_extension/background.js |
diff --git a/ports/ipython_extension/background.js b/ports/ipython_extension/background.js |
new file mode 100644 |
index 0000000000000000000000000000000000000000..98bab105de2fc982e8c5c8a5a4062cb3a4b07a95 |
--- /dev/null |
+++ b/ports/ipython_extension/background.js |
@@ -0,0 +1,46 @@ |
+ |
Sam Clegg
2014/02/18 23:39:31
Please add copyright to all files in naclports.
|
+var createEmbed = function(id) { |
+ var embed = document.createElement('object'); |
+ embed.width = 0; |
+ embed.height = 0; |
+ embed.data = 'python.nmf'; |
+ embed.type = 'application/x-nacl'; |
+ document.body.appendChild(embed); |
+ |
+ return embed; |
+}; |
+ |
+ |
+chrome.runtime.onConnectExternal.addListener(function(port) { |
+ var embed = null; |
+ // Create an embed element that runs the NaCl kernel |
+ |
+ port.onMessage.addListener(function(msg) { |
+ if (msg.type === 'create') { |
+ embed = createEmbed(msg.id); |
+ |
+ // forward all messages across port |
+ embed.addEventListener('message', function(e) { |
+ var msg = e.data; |
+ msg.type = 'message'; |
+ port.postMessage(msg); |
+ }); |
+ embed.addEventListener('progress', function(e) { |
+ var progress = Math.round(100 * e.loaded / e.total); |
+ if (isNaN(progress)) { progress = 100; } |
+ port.postMessage({type: 'progress', progress: progress}); |
+ }); |
+ embed.addEventListener('loadend', function(e) { |
+ port.postMessage({'type': 'loadend'}) |
+ }); |
+ embed.addEventListener('crash', function() { |
+ port.disconnect(); |
+ }); |
+ } else if (msg.type === 'message') { |
+ embed.postMessage({json: msg.json}); |
+ } |
+ }); |
+ port.onDisconnect.addListener(function() { |
+ console.log("disconnected"); |
+ }); |
+}); |