0
votes

I'm trying to convert an existing Chrome extension into its Firefox equivalent, using the Firefox SDK.

My addon needs to detect whether Gmail (mail.google.com) is open/active in current tabs and, if not and in specific circumstances, start a Listener in order to add some parameters to Gmail's URL whenever a Gmail tab will be opened by the user.

To make it clearer, here is what my code looks like in Chrome:

background.js [Chrome extension]

// Reload Gmail Tab(s)
function reloadTab(order, behavior) {
    chrome.tabs.query({ currentWindow: true }, function(tabs) {

        var countGmailTabs = 0,
            ntabs = tabs.length;

        for (var i = 0; i < ntabs; i++) {
            var t = tabs[i].url;
            // Gmail found !
            if ( /mail.google.com/g.test(t) ) {
                countGmailTabs += 1;
                if(behavior === 'noisy') {
                    var GmailTab = tabs[i];
                    // Do something...
                } else {
                    // Reload Tab
                    chrome.tabs.reload(tabs[i].id);
                }
            }
        }

        // Gmail not found !
        if (countGmailTabs < 1 && behavior === 'noisy') {
            // Start listener
            chrome.tabs.onUpdated.addListener( GmailListener );
        }

    });
}

// Gmail listener
function GmailListener (tabId, info, tab) {
    if ( /mail.google.com/g.test(tab.url) && info.status === 'loading' ) {
        // Do something...

        // Now, let's relieve ourselves from our listener duties
        chrome.tabs.onUpdated.removeListener(GmailListener);
        return;
    }
}

My code in Firefox currently looks like this. The tricky part is implementing a listener for the tabs when a Gmail tab is not detected... Any help appreciated!

main.js [Firefox add-on]

var tabs = require('sdk/tabs')

// [...]

// Reload Gmail Tab(s)
function reloadTab(order, behavior) {

    var countGmailTabs = 0,
        ntabs = tabs.length;

    for (var i = 0; i < ntabs; i++) {
        var t = tabs[i].url;

        // Gmail found !
        if ( /mail.google.com/g.test(t) ) {
            countGmailTabs += 1;
            if(behavior === 'mute') {
                var GmailTab = tabs[i];
                // Do something...
            } else {
                tabs[i].reload();
            }
        }
    }

    // Gmail not found !
    if (countGmailTabs < 1 && behavior === 'noisy') {
        GmailTabListener(order);
    }
}

// Gmail Listener (the tricky part)
function GmailTabListener( action ) {
    tabs.on('open', function(tab){
        tab.on('ready', function(tab){
            if ( /mail.google.com/g.test(tab.url) ) {
                // Do something...

                return;
            }
        });
    });
}
1

1 Answers

2
votes

You'll need to create a PageMod if the gmail tab isn't found, like so:

var pageMod = require('sdk/page-mod').PageMod({
  include: '*.gmail.com',
  attachTo: 'existing',       // See comment below
  onAttach: GmailListener
});

function GmailListener (worker) {
  var tab = worker.tab;
  var Id = tab.index;
  // Not sure what 'info' is
  pageMod.destroy();
  // Do something
}

Though I haven't tested it, you can add attachTo: 'existing' and you won't even need to cycle through the tabs as long as your // Do something function is the same in both situations.