开发者

Javascript: Firefox, why this error?

开发者 https://www.devze.com 2023-03-24 02:17 出处:网络
I am trying to make a firefox extension, and this is my very simple code: var SlashUnblocker_Button = {

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 //********* ?

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.

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?

0

精彩评论

暂无评论...
验证码 换一张
取 消