I am trying to make a firefox extension, and this is my very simple code:
var SlashUnblocker_Button = {
//*************************************
var prefManager = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);
prefs.QueryInterface(Components.interfaces.nsIPrefBranch2);
var alertsService = Components.classes["@mozilla.org/alerts-service;1"].getService(Components.interfaces.nsIAlertsService);
var urlbar = window.content.location.href;
prefManager.setCharPref("extensions.mf_unblocker.blocker_latest_url",urlbar);
prefManager.setCharPref("extensions.mf_unblocker.blocker_latest_url_title",document.title);
//*************************************
1: function () {
if(prefManager.getCharPref("extensions.mf_unblocker.blocker_user_email") != "a@a.com")
{
gBrowser.selectedTab = gBrowser.addTab("chrome://mf_unblocker/content/1_options.html");
}
else
{
gBrowser.selectedTab = gBrowser.addTab("chrome://mf_unblocker/content/0_register.html");
}
},
test: function () {alert("testing!");}
}
window.addEventListener("load", function (e)
{
SlashUnblocker_Button['test']();
}, false);
Here’s the problem, see the part where I marked it with //********* ?
See the code inbetween.
If I put that code above the two functions it throws an error… but if I put it in function 1() then it works fine. The problem is I need to reference variable prefManager from both functions.
What am I doing wrong?
If I’m not mistaking, you are trying to put code into an object.
You should be putting it into a builder.
I haven’t tested it, but that’s the idea.
http://www.w3schools.com/js/js_objects.asp