TabBetweenGmailCal

From Chickenfoot Script Repository

Here is a scripts that improves the feel of navigating between Gmail and Calendar by using the existing links in the upper-left. This script was used as a prototype in creating the GCalQuickTab (https://addons.mozilla.org/firefox/2372/) extension.

// ==UserScript==
// @name GCalQuickTab
// @author Michael Bolin (bolinfest@gmail.com)
// @version 0.1
// @description Natural switching between Gmail and Calendar
//
// @include http://mail.google.com/*
// @include https://mail.google.com/*
// @include http://www.google.com/calendar/render*
// @include https://www.google.com/calendar/render*
// ==/UserScript==

/**
 * Tries to switch to an open tab
 * whose location matches the pattern;
 * otherwise, opens a new tab to the
 * provided destination.
 */
function swapTo(/*RegExp*/ pattern, /*string*/ destination) {
  return function() {
    var tabbrowser = chromeWindow.gBrowser;
    var tabs = tabbrowser.tabContainer.childNodes;
    for (var i = 0; i < tabs.length; ++i) {
      var tab = tabs[i];
      var browser = tabbrowser.getBrowserForTab(tab);
      var doc = browser.contentDocument;
      var loc = doc.location.toString();
      if (loc.match(pattern)) {
        tabbrowser.selectedTab = tab;
        return;
      }
    }
    openTab(destination, true);
  }
}

if (find('calendar link').hasMatch) {
  var callback = swapTo(/^https?:\/\/www\.google\.com\/calendar/,
    "https://calendar.google.com/");
  replace(find('calendar link'), new Link("Calendar", callback));
}

if (find('gmail link').hasMatch) {
  var callback = swapTo(/^https?:\/\/mail\.google\.com/,
    "https://mail.google.com/"); 
  replace(find('gmail link'), new Link("Gmail", callback));
}