Javascript:Firefox,为什么会出现这个错误?

时间:2011-08-01 13:53:15

标签: javascript firefox-addon

我正在尝试创建一个firefox扩展,这是我非常简单的代码:

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);

这是问题,请参阅我用//*********标记的部分?
请参阅中间的代码 如果我将该代码放在两个函数之上,则会抛出错误...但如果我将放在函数1()中,那么它可以正常工作。问题是我需要从两个函数中引用变量prefManager

我做错了什么?

2 个答案:

答案 0 :(得分:3)

如果我没有误会,你正试图将代码放入一个对象中。

你应该将它放入一个建筑商。

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();
//...

我没有测试过,但这就是主意。

http://www.w3schools.com/js/js_objects.asp

答案 1 :(得分:0)

您是否尝试在//***之前放置var SlashUnblocker_Button部分?