Firefox addon Javascript Object Management

Hey there! I posted my add-on to the Mozilla add-on site, and the editor came back and told me only one problem:

Your preliminary review request has been approved.

Here are a few things you need to fix in the next version, especially if you want to apply for full permission:

1) In order to avoid conflicts with other add-ons that can be set by users, you need to wrap your “free” variables and functions within the JavaScript object. You can see examples of how to do this at https://developer.mozilla.org/en/XUL_School/JavaScript_Object_Management .

So, I went there and started reading ... but there are so many things that seem to me gibberish, and I embarrassed myself (not at all difficult!)

Using the first example on this page, can you kindly tell me how to modify the xul file?

Currently it looks like this:

  <?xml version="1.0"?>
  <?xml-stylesheet href="chrome://global/skin/" type="text/css"?>

  <overlay id="quickfilterOverlay" xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<script type="application/x-javascript" src="chrome://quickfilter/content/quickfilter.js">
  </script>
  </overlay>

Thanks in advance! R

EDIT: We
downloaded the entire add-on here: http://www.mediafire.com/?fff6bjzjy6n39nx

+3
source share
2 answers

It is advisable to encapsulate the code in the namespace to avoid name conflicts. Here is what I always do in my additions:

if(!org) var org={};
if(!org.janek) org.janek={};

org.janek.Addon = function() {

  var pub = {};
  var self = this;

  pub.init = function() {
     //
     // Initialize addon, setup listeners, ...
     //
  }

  ...

  self.thisIsAPrivateMethod = function(arg) {
    // method that only visible inside org.janek.Addon
  }

  return pub;
}();


// Init addin after window loaded
window.addEventListener("load",
                        org.janek.Addon.init,
                        false);

First, I create my own org.janek namespace, making sure it does not exist yet. Then I add an Addon object that will contain the code for my addon.

"pub" "self". , , pub. .

, quickfilter_extension ( prefManager):

var quickfilter_extension = function() {
    var pub = {};

    // interface for preferences
    pub.prefManager = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);

    pub.init = function() {
        //Initiating the progressListerner
        gBrowser.addProgressListener(quickfilter_urlBarListener, Components.interfaces.nsIWebProgress.NOTIFY_STATE_DOCUMENT);
        //Load the block list xml form server
        quickfilter_quickfilter.request_xml();
    },

    pub.uninit =  function() {
        // Remove the progressListerner
        gBrowser.removeProgressListener(quickfilter_urlBarListener);
    }

    return pub;
}();

, prefManager, quickfilter_extension:

redirectToAnotherUrl:function()
{
    [ ... omitted ...]
    qucikFilterRedirectCount = quickfilter_extension.prefManager.getCharPref("extensions.quickfilter_redirect_count");

    [ ... omitted ...]
}

javascript- Yahoo YUI .

+3

JS-, /.

:

var x = 1;
function foo() {}

:

MyPluginName = {};
MyPluginName.x = 1;
MyPluginName.foo = function(){}
+2

All Articles