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.