简体   繁体   English

Firefox插件Javascript对象管理

[英]Firefox addon Javascript Object Management

Hey there! 嘿!
I submitted my add on to the Mozilla add ons site and the editor got back and told me only one problem: 我将我的添加内容提交到Mozilla add ons网站,编辑回来告诉我只有一个问题:

Your preliminary review request has been approved. 您的初步审核请求已获批准。

Here are a few things that you need to fix in your next version, specially if you want to apply for full approval: 以下是您需要在下一版本中修复的一些内容,特别是如果您想申请完全批准:

1) In order to prevent conflicts with other add-ons that may be installed by users, you need to wrap your "loose" variables and functions within a JavaScript object. 1)为了防止与用户可能安装的其他附加组件发生冲突,您需要将“松散”变量和函数包装在JavaScript对象中。 You can see examples on how to do this at https://developer.mozilla.org/en/XUL_School/JavaScript_Object_Management . 您可以在https://developer.mozilla.org/en/XUL_School/JavaScript_Object_Management上查看有关如何执行此操作的示例。

So I went there and started reading up... but it's a lot of stuff that feels like gibberish to me and I confused myself (not hard to do at all!) 所以我去了那里开始阅读...但是很多东西对我来说都是胡言乱语而且让自己感到困惑(根本不难做到!)

Using the first example on that page, can you kindly tell me how to modify my xul file? 使用该页面上的第一个示例,您能否告诉我如何修改我的xul文件?

Presently 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 [R

EDIT: 编辑:
Have uploaded the entire add on here: http://www.mediafire.com/?fff6bjzjy6n39nx 已在此处上传了整个添加内容: http//www.mediafire.com/?ff6bjzjy6n39nx

It is advisable to encapsulate your code in a namespace to avoid name collisions. 建议将代码封装在命名空间中以避免名称冲突。 Here's what I always do in my addons: 这是我在插件中总是做的事情:

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's 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 namespace org.janek, making sure it doesn't already exist. 首先,我创建自己的命名空间org.janek,确保它不存在。 Then I add the object Addon which will contain the code for my addon. 然后我添加对象Addon,它将包含我的插件的代码。

Please note the "pub" and "self" objects. 请注意“酒吧”和“自我”物品。 Every method that should be callable from other objects is added to the pub object. 应该可以从其他对象调用的每个方法都会添加到pub对象中。 Private methods are added to self. 私人方法被添加到自我。

To be more specific, I would change the quickfilter_extension to the following code (I included the global prefManager object as an example): 更具体地说,我将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;
}();

Code that uses the prefManager object now needs to go through the quickfilter_extension object: 现在使用prefManager对象的代码需要通过quickfilter_extension对象:

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

    [ ... omitted ...]
}

The blog for Yahoo's javascript library YUI has a nice article about the pattern . Yahoo的javascript库YUI的博客有一篇关于模式的好文章

The reviewer is talking about your JS code, suggesting that you have global variables/functions. 评论者正在讨论您的JS代码,建议您拥有全局变量/函数。

For example: 例如:

var x = 1;
function foo() {}

Compare that with: 与之相比:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM