Search code examples
firefoxfirefox-addonfirefox-addon-sdkcontent-script

variable not recognized inside contentscript


I am trying to create an addon which after a user logs in to my site I will try and read the session id of that domain and use it for further interactions with my addon. I use the online addon builder and I have this code which I want to read the cookies:

var data = require("self").data;
var {Cc, Ci} = require("chrome");
var cookieMgr = Cc["@mozilla.org/cookiemanager;1"].getService(Ci.nsICookieManager); 
var cm = require("context-menu");
cm.Item({
  label: "My Menu Item",
  contentScript: 'self.on("click", function (node, data) {' +
                 'for (var e = cookieMgr.enumerator; e.hasMoreElements();) {  ' +
                 'var cookie = e.getNext().QueryInterface(Ci.nsICookie);   ' +
                 ' console.log(cookie.host + ";" + cookie.name + "=" + cookie.value + "\\n");'+
                 '}});'
});

but it throws an error every time I click on the 'My Menu Item' button, saying cookieMgr, the variable is not defined.


Solution

  • The content script context is entirely disconnected from the addon script context. It's not so easy to grasp this when you're using contentScript, but if you put it in another file and use it through a contentScriptFile it becomes more obvious. Content scripts are run in the context of the document, while addon scripts aren't.