context-menu and callback issue

37 views
Skip to first unread message

Jeff Aigner

unread,
Jan 27, 2012, 2:42:11 PM1/27/12
to mozilla-labs-jetpack
I'm having serious troubles using the context-menu with functions
created using a callback. The issue is that I am loading a shared
library packaged in the XPI file using the callback function passed to
AddonManager.getAddonByID(). Because the context-menu is already
created before the callback runs, it doesn't seem to be able to access
the functions bound with jsctypes during the callback.

I've tried initializing the functions as global variables set to null
prior to the callback being run, but in this case, the context-menu
doesn't recognize the variables have been reassigned to functions so
it doesn't work. I've also tried creating the context-menu within the
callback function, but when I do this the context menu doesn't even
exist.

I've posted this problem to the forums to no avail, the thread can be
found here: https://forums.mozilla.org/addons/viewtopic.php?f=27&t=4608

The code I am working on can be found on github here:
https://github.com/sheik/Cryptofox/

The basic snippet I am having problems with is below (main.js):

var cm = require("context-menu");
var selection = require("selection");
var s = require("self");

/* import ctypes */
const {Cu} = require("chrome");
Components.utils.import("resource://gre/modules/ctypes.jsm");
Components.utils.import("resource://gre/modules/AddonManager.jsm");

var cryptofox = null;

AddonManager.getAddonByID(s.id, function(addon) {
var libraryLocation =
addon.getResourceURI("libcryptofox.so").QueryInterface(Components.interfaces.nsIFileURL).file.path;
cryptofox = ctypes.open(libraryLocation);
});

var encrypt = cryptofox.declare("encrypt", ctypes.default_abi,
ctypes.void_t, ctypes.char.ptr, ctypes.char.ptr, ctypes.char.ptr);
var read_data = cryptofox.declare("read_data", ctypes.default_abi,
ctypes.int, ctypes.char.ptr, ctypes.int);
var init_keylist = cryptofox.declare("init_keylist",
ctypes.default_abi, ctypes.void_t);
var get_next_key = cryptofox.declare("get_next_key",
ctypes.default_abi, ctypes.int, ctypes.char.ptr, ctypes.int);

var cstring = ctypes.ArrayType(ctypes.char);
var gpg_result = new cstring(512);
var encrypted = new String();
var ckey = new cstring(512);
var key = new String();

cm.Item({
label: "Encrypt with GPG",
context: cm.SelectionContext(),
contentScript: 'self.on("click", function(node,data) {' +
' self.postMessage("encrypt");' +
'});',
onMessage: function(item) {
// encryption code
}
});


Does anyone have any idea how I can overcome this problem? I would
really prefer to use the addon-sdk instead of XUL.

Irakli Gozalishvili

unread,
Jan 31, 2012, 3:18:37 PM1/31/12
to mozilla-la...@googlegroups.com
Hi Jeff,

I don't see any reason why it would not work. Could you try logging to see what's actually happeining ?

On the other hand I think you could just use `self` module to get a URI of the .so file if you'd put it into a data folder

```
var libURI = require('self').data.url('libcryptofox.so')
```

Finally I'd recommend positing questions here!

Regards
--
Irakli Gozalishvili
Web: http://www.jeditoolkit.com/

--
You received this message because you are subscribed to the Google Groups "mozilla-labs-jetpack" group.
To post to this group, send email to mozilla-la...@googlegroups.com.
To unsubscribe from this group, send email to mozilla-labs-jet...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mozilla-labs-jetpack?hl=en.

Reply all
Reply to author
Forward
0 new messages