将firefox扩展名限制为特定域

时间:2010-09-21 18:11:59

标签: firefox firefox-addon

我想写一个firefox扩展。此扩展不是通用扩展,但专门针对我需要突出显示特定html组件的域。

我该怎么做?我只想在用户浏览特定域时加载js。

我当前的overaly.js基本上是空的(由扩展向导生成):

var myextension = {
  onLoad: function() {
    // initialization code
    this.initialized = true;
    this.strings = document.getElementById("myextension-strings");
  },

  onMenuItemCommand: function(e) {
    var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
                                  .getService(Components.interfaces.nsIPromptService);
    promptService.alert(window, this.strings.getString("helloMessageTitle"),
                                this.strings.getString("helloMessage"));
  },

  onToolbarButtonCommand: function(e) {
    // just reuse the function above.  you can change this, obviously!
    myextension.onMenuItemCommand(e);
  }
};

window.addEventListener("load", myextension.onLoad, false);

我的ff-overlay.xul是:

myextension.onFirefoxLoad = function(event) {
  document.getElementById("contentAreaContextMenu")
          .addEventListener("popupshowing", function (e){ myextension.showFirefoxContextMenu(e); }, false);
};

myextension.showFirefoxContextMenu = function(event) {
  // show or hide the menuitem based on what the context menu is on
  document.getElementById("context-myextension").hidden = gContextMenu.onImage;
};

window.addEventListener("load", myextension.onFirefoxLoad, false);

我正在考虑去 neanderthal 并在myextension.onFirefoxLoad内进行检查以查看当前页面是否是我想要的那个但是需要用户点击上下文菜单中的正确项目

1 个答案:

答案 0 :(得分:3)

我并不完全遵循你所拥有的,因为它们看起来都像JS文件,而不是XUL文件。但你可能想做的是listen for the load event coming from the web pages that are loaded。然后,在您的事件加载器中,只需查看加载的每个页面,看看它是否来自您想要的特定域。

一个伟大的(虽然并不总是那么容易听起来)找到如何在Firefox插件中做某事的方法是找到另一个类似的插件。 DOM InspectorInspect Context是您的朋友!在这种情况下想到的第一个这样的插件是WikiTrust,所以你可以尝试查看它,看它是否能给你任何灵感。