在Javascript中更改语法事件侦听器

时间:2011-07-05 09:06:45

标签: javascript events firefox-addon

我已经在Javascript中编写了一些代码,需要进行一些转换,但我不知道如何实现这一目标。

我的代码:

gBrowser.addEventListener("DOMContentLoaded", function(e) {...}), false);

window.addEventListener("load", function() {myExtension.init()}, false);

这里的关键是传递参数e。我已经尝试过"功能(e)"和" myExtension.init(e)"但它没有用。

编辑(我的所有代码 - 功能代码都位于代码的末尾):

var myExt_urlBarListener = {
  QueryInterface: function(aIID)
  {
   if (aIID.equals(Components.interfaces.nsIWebProgressListener) ||
       aIID.equals(Components.interfaces.nsISupportsWeakReference) ||
       aIID.equals(Components.interfaces.nsISupports))
     return this;
   throw Components.results.NS_NOINTERFACE;
  },

  onLocationChange: function(aProgress, aRequest, aURI)
  {
    myExtension.processNewURL(aURI);
  },

  onStateChange: function(a, b, c, d) {},
  onProgressChange: function(a, b, c, d, e, f) {},
  onStatusChange: function(a, b, c, d) {},
  onSecurityChange: function(a, b, c) {}
};

var myExtension = {
  oldURL: null,

  init: function(e) {
    // Listen for webpage loads

    gBrowser.addProgressListener(myExt_urlBarListener,
        Components.interfaces.nsIWebProgress.NOTIFY_LOCATION);
  },

  uninit: function(e) {
    gBrowser.removeProgressListener(myExt_urlBarListener);
  },

  processNewURL: function(aURI) {
    if (aURI.spec == this.oldURL)
      return;

    // now we know the url is new...
    this.oldURL = aURI.spec;

    var href = aURI.spec;

    var vars = [], hash;
    var hashes = href.slice(href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }

    if(vars[0].match(/^http:\/\/www\.google\.fr/))
    {
        //I want use the event e here
        //But it doesn't work
        displayBookmarks(e);
    }
  }
};

//The following doesn't work (I get "e is not defined" in the Error Console) 
window.addEventListener("DOMContentLoaded", function(e) {myExtension.init(e)}, false);
window.addEventListener("unload", function() {myExtension.uninit()}, false);

1 个答案:

答案 0 :(得分:0)

不要忘记在函数声明中添加参数:

window.addEventListener("load", function(e) {myExtension.init(e)}, false);
...
init: function(e) {