simonzack
simonzack

Reputation: 20928

Firefox toolbarbutton initialization event

What is the proper method to listen to toolbarbutton initialization (say I want to change the icon depending on the current of the addon)?

Right now I'm just listening to window.load, and detecting if the toolbarbutton exists via document.getElementById.

For user toolbar customizations I'm using the aftercustomization event in case the toolbarbutton is removed then re-added by the user.

But is there a more direct method?

Upvotes: 1

Views: 382

Answers (1)

nmaier
nmaier

Reputation: 33162

There are several ways to "modify" a toolbar button, even if it is hidden away in the palette (the user didn't yet add it, or removed it). Here is a selection:

You may get a reference to the DOM node anyway, by searching the palette manually if document.getElementById does not yield a result. This works for browser.xul, anyway. Some code (simplied from code I'm using, but untested after some cleanup). It uses <toolbox>.palette

function findToolbarButtonAnyway(id) {
  let tb = document.getElementById(id);
  if (tb) {
    return tb;
  }
  let palette = $("navigator-toolbox").palette;
  for (let c = palette.firstChild; c; c = c.nextSibling) {
    if (c.id == id) {
      return c;
    }
  }
  return null; // not found
}

You may instead use <broadcaster> (or <command>) to "broadcast" a set of attributes all other nodesobserving the broadcaster will take. You may e.g. set attributes and have corresponding CSS rules to select an appropriate icon or whatever. This solution would be cleaner, but takes some time to figure out.

Upvotes: 1

Related Questions