Hi, I'm new this forum and I don't know if this is in the right place. If it isn't, I apologize.

Anyway, for my Senior project I'm creating a toolbar for Firefox. I was wondering if there was a way for me to create an external javascript file to put all the urls and the menuitems in.

Dani AI

Generated

A compact, practical way to handle this is to keep the menu data outside the XUL overlay and populate the menupopup at runtime. , that makes the toolbar easier to edit and test; pointed toward a data-driven direction and that is the right idea. Two simple choices: a small external JavaScript file that exports a data array (simplest for extension code), or an external data file (JSON or XML) that your overlay script loads and parses.

Example data file (chrome/content/menu-data.js):

var toolbarMenuData = [
  { label: "Home", url: "https://example.com/" },
  { label: "Docs", url: "https://example.com/docs" }
];

Example overlay script that builds menuitems at load time:

window.addEventListener("load", function() {
  var popup = document.getElementById("my-menupopup");
  toolbarMenuData.forEach(function(item) {
    var mi = document.createElement("menuitem");
    mi.setAttribute("label", item.label);
    mi.setAttribute("data-url", item.url);
    mi.addEventListener("command", function() {
      var url = this.getAttribute("data-url");
      if (typeof openUILinkIn == "function") {
        openUILinkIn(url, "tab");
      } else {
        window.open(url);
      }
    }, false);
    popup.appendChild(mi);
  });
}, false);

Notes and cautions: place the data file under your extension content folder and include it in the overlay with a chrome:// script src, or load JSON/XML at runtime with XHR/fetch and parse into the same shape. Use a single event listener pattern to keep handlers small. Remember that XUL-based extensions and chrome-level APIs changed in later Firefox releases; if targeting current Firefox builds, investigate WebExtensions (different APIs for toolbar and context menus).

Recommended Answers

All 2 Replies

Okay. I'll check out those links. Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.