开发者

Limit a firefox extension to a specific domain

开发者 https://www.devze.com 2023-01-17 05:04 出处:网络
I would like to write a firefox extension. This extension is not a generic extension but work specifically for a domain where I need to highlight specific html components.

I would like to write a firefox extension. This extension is not a generic extension but work specifically for a domain where I need to highlight specific html components.

How should I do that? I just want the js loaded when the user is browsing a specific domain.

My current overaly.js is basically empty (generated by the Extension Wizard):

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) {
    // jus开发者_Go百科t reuse the function above.  you can change this, obviously!
    myextension.onMenuItemCommand(e);
  }
};

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

And my ff-overlay.xul is:

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);

I was thinking to go neanderthal and do a check inside myextension.onFirefoxLoad to see if the currentpage is the one I want but that requires the user to click the proper item on the context menu.


I'm not totally following what you have because both of those look like JS files, not XUL files. But what you probably want to do is listen for the load event coming from the web pages that are loaded. Then, in your event loader, just look at each page that loads and see whether it's coming from the specific domain you want.

A great (though not always quite as easy as it sounds) way to find out how to do something in a Firefox addon is to find another addon that does something similar. DOM Inspector and Inspect Context are your friends! The first such addon that comes to mind in this case is WikiTrust so you could try looking at that one to see if it gives you any inspiration.

0

精彩评论

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

关注公众号