Passing messages between the add-on and the content script
[gssweb.git] / firefox / lib / main.js
1 var tabs = require("sdk/tabs");
2 const {Cu} = require("chrome");
3
4 for (i = 0; i < 10; i++) {
5   console.log("");
6 }
7
8 console.log("About to load ctypes");
9 Cu.import("resource://gre/modules/ctypes.jsm");
10 console.log("  ... loaded.");
11
12 var json_gssapi;
13
14
15 // TODO: detect which OS we're running, and proactively search for the
16 //       correct library name.
17 console.log("About to load libjsongssapi");
18 try {
19     json_gssapi = ctypes.open("libjsongssapi.so");
20 }
21 catch (e) {
22     try {
23         json_gssapi = ctypes.open("libjsongssapi.dll");
24     }
25     catch (e) {
26         console.log("  ... " + e);
27     }
28 }
29 console.log("  ... loaded.");
30
31 console.log("Declaring the call_gss function:");
32 const call_gss = json_gssapi.declare("gss_request",
33     ctypes.default_abi,
34     ctypes.char.ptr,
35     ctypes.char.ptr);
36 console.log("  ... declared.");
37
38 console.log("Calling import_name");
39 var reply;
40 reply = call_gss("{\"method\":\"gss_import_name\",\"arguments\":{\"input_name\":\"HTTP@localhost\",\"input_name_type\":\"{1 2 840 113554 1 2 1 4 }\"}}");
41 console.log("  ... Reply: " + reply.readString());
42
43
44 var self = require("sdk/self");
45 var data = require("sdk/self").data;
46 var pageMod = require("sdk/page-mod");
47
48 pageMod.PageMod({
49   include: "*",
50   contentScriptFile: [data.url("navigator.gss.js")],
51   contentScriptWhen: "ready"
52 });
53
54 tabs.on("ready", function(tab) {
55   worker = tab.attach({ contentScriptFile: data.url("navigator.gss.js") });
56   worker.port.on("gss_request", function(message) {
57     console.log("main.js received message: " + message);
58   });
59   worker.port.emit("alert", "Message from the add-on");
60 });
61
62
63 /*var gssweb = {
64   myListener: function(evt) {
65     alert("Received from web page: " +
66           evt.target.getAttribute("attribute1") + "/" + 
67           evt.target.getAttribute("attribute2"));
68   }
69 }*/
70 //document.addEventListener("GsswebEvent", function(e) { gssweb.myListener(e); }, false, true);
71 // The last value is a Mozilla-specific value to indicate untrusted content is allowed to trigger the event.
72