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("extensio开发者_JAVA百科ns.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 //*********
?
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.
function SlashUnlockerButton(){
//*************************************
this.prefManager = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
this.prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);
prefs.QueryInterface(Components.interfaces.nsIPrefBranch2);
this.alertsService = Components.classes["@mozilla.org/alerts-service;1"].getService(Components.interfaces.nsIAlertsService);
this.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);
//*************************************
this.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");
}
};
this.test = function () {alert("testing!");};
}
var SlashUnblocker_Button = new SlashUnlockerButton();
//...
I haven't tested it, but that's the idea.
http://www.w3schools.com/js/js_objects.asp
Have you tried placing the //***
section before var SlashUnblocker_Button
?
精彩评论