Visit Mozilla.org

Fragmenty kodu:Paski narzędzi

z Mozilla Developer Center, polskiego centrum programistów Mozilli.

Spis treści

UWAGA: Tłumaczenie tej strony nie zostało zakończone.
Może być ona niekompletna lub wymagać korekty.
Chcesz pomóc? | Dokończ tłumaczenie | Sprawdź ortografię | Więcej takich stron...

[edytuj] Dodawanie przycisków paska narzędzi

There are two tutorials available:

  • An elaborate step by step tutorial for beginners: Custom Toolbar Button
  • A tutorial describing the steps needed to add a toolbar button assuming you already have a working extension and know the basics of extension development: Creating toolbar buttons

[edytuj] Dodawanie przycisków przez użytkownika

When you create and deploy your extension and include a toolbar button for it by overlaying the Customize toolbarpalette, it is not available by default. The user has to drag it onto the toolbar. Here is some code to place your button on the toolbar by default after first install and restart of the XPI.

Adding a button by default is arguably a bad practice, since it's hard to get right: the user might have hidden the toolbar you're adding your button to, or customized his toolbars in a way that make your code work not quite well.

Bug 242071 has been filed against Firefox to provide an API for this.

Note that it may provide a better user experience to ask the user (in a dialog box), and then call BrowserCustomizeToolbar() directly, which will open the Toolbar->Customize sheet. This may avoid problems.

[edytuj] Uwagi

  1. This example is Firefox specific, but should work with Thunderbird too with tweaks to target the right nodes.
  2. Please only add your button by default if it adds real value to the user and will be a frequent entry point to your extension. Consider limited UI space the user may have and if they really want it.
  3. This code has to be included in an overlay of browser.xul

[edytuj] Przykład

 try {
   var firefoxnav = document.getElementById("nav-bar");
   var curSet = firefoxnav.currentSet;
   if (curSet.indexOf("my-extension-button") == -1)
   {
     var set;
     // Umieść przycisk przed panelem url
     if (curSet.indexOf("urlbar-container") != -1)
       set = curSet.replace(/urlbar-container/, "my-extension-button,urlbar-container");
     else  // na końcu
       set = firefoxnav.currentSet + ",my-extension-button";
     firefoxnav.setAttribute("currentset", set);
     firefoxnav.currentSet = set;
     document.persist("nav-bar", "currentset");
     // Jeśli nie zrobisz poniższego wywołania, będą się działy śmieszne rzeczy
     try {
       BrowserToolboxCustomizeDone(true);
     }
     catch (e) { }
   }
 }
 catch(e) { }

See also Programmatically adding items to the Firefox Toolbar, which has more concise code and pointers to other resources on the topic.