Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an equivalent for Chrome's 'setBadgeText' in Firefox Add-on SDK?

Chrome's method chrome.browserAction.setBadgeText overlays text on top of the extensions icon. This is handy if you want to display total unread mails for example.

Is there an equivalent method when developing a Firefox Addon SDK extension, or what is the best way to change the icon at runtime?

As of version 1.11 of SDK I can't find similar method in Firefox Add-on SDK API.

like image 906
Khurram Waris Avatar asked Aug 08 '12 16:08

Khurram Waris


2 Answers

Note: This answer does not work any more in Firefox 38+ due to the removal of the widget module. Use one of the buttons from the ui module instead.


I've ported the chrome.browserAction API to Firefox, and published it as a Jetpack module, see https://github.com/Rob--W/browser-action-jplib

If you're familiar with the chrome.browserAction API, using this module can't be easier. Even if you have never created a Firefox add-on before, you can get started within a few minutes:

  1. Install the add-on SDK (instructions from Mozilla).
  2. Activate the SDK (by running addon-sdk) and create the boilerplate files (cfx init).
  3. Download my browser-action Jetpack library (instructions).
  4. Edit your add-on's package.json, and add "browser-action" as a dependency.

At this point, you've set up all prerequisites. What remains is the creation of the browser action.

Example

This add-on adds a browser action near the location bar, which shows a popup on click. When you type something in the input field within this popup, the tab's title is changes to the field's value.

lib/main.js

// lib/main.js
var badge = require('browserAction').BrowserAction({
    default_icon: 'images/icon19.png', // optional
    default_title: 'Badge title',      // optional; shown in tooltip
    default_popup: 'popup.html'        // optional
});

badge.onMessage.addListener(function(message, sender, sendResponse) {
    require('sdk/tabs').activeTab.title = message;
});

data/popup.html

<!-- (Example) Please avoid inline JavaScript/CSS in your production code -->
<body style="width:200px;">
  Demo: This is a popup page.<br>
  Uses <code>extension.sendMessage</code> to notify the background page,
  which in turn changes the current tab's title to below value <br>
  <input onkeyup="extension.sendMessage(this.value);" style="display:block;">
</body>

Here's a short screencast of the result (note: tooltip color is slightly off; recorded with byzanz):

screencast

like image 167
Rob W Avatar answered Oct 02 '22 21:10

Rob W


In firefox 36 you can add badges (text) to your addon's buttons ..

var { ToggleButton } = require("sdk/ui/button/toggle");

var button = ToggleButton({
    id: "my-button1",
    label: "my button1",
    icon: "./icon-16.png",
    onChange: changed,
    badge: 0,
    badgeColor: "#00AAAA"
  });

function changed(state) {
  button.badge = state.badge + 1;
  if (state.checked) {
    button.badgeColor = "#AA00AA";
  }
  else {
    button.badgeColor = "#00AAAA";
  }
}

https://developer.mozilla.org/en-US/Add-ons/SDK/Low-Level_APIs/ui_button_action#Badged_buttons

like image 33
bobbyrne01 Avatar answered Oct 02 '22 22:10

bobbyrne01