2

Can Firefox play a little "bing" when one of my pin tabs changes?

Actually, Firefox highlight the tab icon but I would like a sound notification too.

JudgeProphet
  • 1,687
  • 3
  • 26
  • 44

1 Answers1

3

Of course! :) You'll need to make an add-on for this but I can help you out with the relevant details. Grab a wav file and put it into your add-on data directory; I called mine 'ding.wav'

I took the approach of watching tabs at a low level, though this might not be the right approach for you. There is another higher level approach that can also work and I added a description of it below. This code however does work.

var {Cc, Ci, Cr} = require("chrome");

var data = require('sdk/self').data;
var tabutils = require('sdk/tabs/utils');

// utility function
function newURI(uriStr, base) {
  var ios = Cc['@mozilla.org/network/io-service;1']
                .getService(Ci.nsIIOService);
  try {
    var baseURI = base ? ios.newURI(base, null, null) : null;
    return ios.newURI(uriStr, null, baseURI);
  }
  catch (e) {
    if (e.result === Cr.NS_ERROR_MALFORMED_URI) {
      throw new Error("malformed URI: " + uriStr);
    } else if (e.result === Cr.NS_ERROR_FAILURE ||
      e.result === Cr.NS_ERROR_ILLEGAL_VALUE) {
      throw new Error("invalid URI: " + uriStr);
    }
  }
  return null;
}

// actual function
function attachToTabs() {
  tabutils.getTabs().forEach(function (tab) {
    if (tab.getAttribute('pinned') === 'true') {
      var browser = tabutils.getBrowserForTab(tab);
      browser.addEventListener('DOMTitleChanged', function() {
        try {
          var sound = Cc["@mozilla.org/sound;1"].createInstance(Ci.nsISound);
          sound.play(newURI(data.url('ding.wav')));
        } catch (e) {
          console.log(e);
        }
      } );
    }
  });  
}

// call the actual function to run on startup
attachToTabs();

This code doesn't actively watch for new tabs which become pinned, I'll leave that to you. It only runs once at startup to attach to existing pinned tabs so you'd have to restart every time you pin a new tab for the sound to work.

--

Compared to this approach you could also take a higher level approach of using the tabs module and attaching to tabs that are pinned. The addon-sdk tabs module offers a way to see that a tab is pinned and then also attach to that tab. Once attached you could then watch the tab content from inside for a title change event and then play your sound at that moment.

Bryan Clark
  • 2,542
  • 1
  • 15
  • 19
  • Oh also, thank Wlad for his audio code snippet from http://stackoverflow.com/questions/10661507/how-to-play-audio-in-an-extension and beware as he says that it may be deprecated soon. – Bryan Clark Feb 21 '13 at 00:10
  • Tx for your Help! I made my AddOn available on [GitHub](https://github.com/judgeprophet/BingPinTab). Finally I did a Widget with a toggle switch feature. – JudgeProphet Feb 21 '13 at 18:55