Gmail Unread Mail Link

By Thomas Chapin Last update Jun 28, 2008 — Installed 522 times. Daily Installs: 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0
// Copyright 2008 Tom Chapin <tchapin@gmail.com> - Web site: http://www.tchapin.com
// All rights Reserved.
//
//
// RELEASE NOTES
// 
// v1.1 (06/27/2008)
// * Added an invisible iframe that would check for unread messages every 30 seconds
// * Made Unread Mail link display the number of unread messages
// * Made Unread Mail link activate/deactivate on selection
// * Rewrote entire script so it doesn't even bother with the gmail greasemonkey API
//
// v1.0 (06/26/2008)
// * Initial release of script.
//
//
// DESCRIPTION
// ============
// Adds an "Unread Mail" link to the left sidebar of your gmail interface
//
//
// KNOWN ISSUES
// ============
// * Only works with new Gmail interface.
//
//
// ==UserScript==
// @name           Gmail Unread Mail Link
// @namespace      tchapin.com
// @description    Adds an "Unread Mail" link to your gmail sidebar
// @include        http://mail.google.com/mail/*
// @include        https://mail.google.com/mail/*
// @include        http://mail.google.com/a/*
// @include        https://mail.google.com/a/*
// ==/UserScript==

// How long should the script wait before executing after page load (in milliseconds)?
unsafeWindow._GMNKY_CheckUnreadEmailStartDelay = 2000;

// How often should this script check for new unread messages (in milliseconds)?
unsafeWindow._GMNKY_CheckUnreadEmailInterval = 30000;

// How often should the script check for URL changes (in milliseconds)?
unsafeWindow._GMNKY_CheckURLChangesInterval = 500;

// Placeholder variable for unread email count
unsafeWindow._GMNKY_CheckUnreadEmailCount = 0;

// Figure out the gmail base URL that we're currently on
var topLevelURL = unsafeWindow.parent.location.href;
var googleBaseURL = unsafeWindow.parent.location.href;
if(googleBaseURL.indexOf("#")>-1){
	googleBaseURL = googleBaseURL.substring(0,googleBaseURL.indexOf("#"));
}

// Set up an unread mail URLs
var unReadMailViewURL = googleBaseURL+'#search/in%3Aunread';
var unReadMailHTMLViewURL = googleBaseURL+'h/?s=q&q=in%3Aunread';

// Function used to communicate between parent gmail screen and child gmail html-based iframe
unsafeWindow._GMNKY_UpdateUnreadEmailCount = function(UnreadCount){

	// Update placeholder count variable
	unsafeWindow._GMNKY_CheckUnreadEmailCount = UnreadCount;

	// Add/Update unread email link in side nav
	unsafeWindow._GMNKY_AddUpdateUnreadEmailLink();
	
	// Check for new unread mail after a specified delay
	setTimeout(function(){
		unsafeWindow._GMNKY_AddUpdateUnreadMessagesiFrame();
	}, unsafeWindow._GMNKY_CheckUnreadEmailInterval);

}

// Function used to add/update the unread email link in the side nav
unsafeWindow._GMNKY_AddUpdateUnreadEmailLink = function(ActivateNavLink){
	
	// Check to make sure that the nav actually exists and if this instance of the greasemonkey script is executing on the main parent screen
	var InboxNavDiv = document.getElementById("1fbc");
	if(InboxNavDiv){
		
		// Inactive selection classes
		var TopDivClass = "XoqCub ACpQre";
		var LinkClass = "yyT6sf";
	
		// Check our current URL to find out which page we're on the search in unread screen
		if(unsafeWindow.parent.location.href.indexOf("#search/in%3Aunread")>-1 || ActivateNavLink){
			// Active selection class
			TopDivClass = "XoqCub";
		}
	
		// Default Link Title
		var LinkTitle = "Unread Mail";
	
		// Check the unread count, make it bold, and set the count
		if(unsafeWindow._GMNKY_CheckUnreadEmailCount>0){
			LinkClass = "yyT6sf PQmvpb";
			LinkTitle = "Unread Mail ("+unsafeWindow._GMNKY_CheckUnreadEmailCount+")";
		}
		
		// HTML that is used to display unread email link
		var UnreadEmailLink = '';
		UnreadEmailLink += '<div class="XoqCub">';
		UnreadEmailLink += '  <div class="'+TopDivClass+'">';
		UnreadEmailLink += '    <div style="height: 4px;">';
		UnreadEmailLink += '      <div class="z1IiMc P0tbHb" style="width: 4px;"></div>';
		UnreadEmailLink += '      <div class="z1IiMc wRoeNc" style="width: 4px;"></div>';
		UnreadEmailLink += '      <div class="R7iiN kwmAmd"></div>';
		UnreadEmailLink += '    </div>';
		UnreadEmailLink += '    <div class="R7iiN c1norb">';
		UnreadEmailLink += '      <div class="R7iiN vHYYR" style="margin-left: 4px;">';
		UnreadEmailLink += '        <div class="wWwc8d" style="margin: -2px 4px -2px 0px; padding: 0px;">';
		UnreadEmailLink += '          <div class="diLZtc">';
		UnreadEmailLink += '            <div class="XoqCub">';
		UnreadEmailLink += '              <div class="NIPhib"><span id="UnreadMailTitleSpan" class="" idlink="">';
		UnreadEmailLink += '                <div style="overflow: hidden; white-space: nowrap;">';
		UnreadEmailLink += '                  <div id="UnreadMailLinkDiv" class="'+LinkClass+'" idlink=""><a href="'+unReadMailViewURL+'" title="'+LinkTitle+'" target="_top" onclick="javascript:_GMNKY_AddUpdateUnreadEmailLink(true);">'+LinkTitle+'</a></div>';
		UnreadEmailLink += '                </div>';
		UnreadEmailLink += '                </span></div>';
		UnreadEmailLink += '            </div>';
		UnreadEmailLink += '          </div>';
		UnreadEmailLink += '        </div>';
		UnreadEmailLink += '      </div>';
		UnreadEmailLink += '    </div>';
		UnreadEmailLink += '    <div class="LV4T4d"></div>';
		UnreadEmailLink += '    <div class="Pj5gjf" style="height: 4px;">';
		UnreadEmailLink += '      <div class="z1IiMc F2yfRe" style="width: 4px;"></div>';
		UnreadEmailLink += '      <div class="z1IiMc Hemrjb" style="width: 4px;"></div>';
		UnreadEmailLink += '      <div class="R7iiN m8lwn"></div>';
		UnreadEmailLink += '    </div>';
		UnreadEmailLink += '  </div>';
		UnreadEmailLink += '</div>';
		
		// Find the inbox div
		var InboxNavDiv = unsafeWindow.document.getElementById("1fbc");
		
		// Find the left nav div
		var LeftNavDiv = InboxNavDiv.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode;
		
		// Look for the unread email nav link and remove it if it already exists
		var UnreadEmailNavDiv = unsafeWindow.document.getElementById("UnreadEmailNavDiv");
		if(UnreadEmailNavDiv){
			UnreadEmailNavDiv.parentNode.removeChild(UnreadEmailNavDiv);
		}
		
		// Create an unread email nav link and add it in the nav
		var UnreadEmailNavDiv = document.createElement('div');
		UnreadEmailNavDiv.id = "UnreadEmailNavDiv";
		UnreadEmailNavDiv.innerHTML = UnreadEmailLink;
	
		LeftNavDiv.appendChild(UnreadEmailNavDiv);
	}
}

// Function used to add/update the hidden iframe, which loads the unread messages screen
unsafeWindow._GMNKY_AddUpdateUnreadMessagesiFrame = function(){

	// Remove the iframe if it already exists (so we can reload it and get fresh data)
	var existingifrm = unsafeWindow.document.getElementById("UnreadMailCheckeriFrame");
	if(existingifrm){
		existingifrm.parentNode.removeChild(existingifrm);
	}

	// Create an invisible iframe to check for unread mail
	var ifrm = unsafeWindow.document.createElement("IFRAME"); 
	ifrm.setAttribute("src", unReadMailHTMLViewURL); 
	ifrm.setAttribute("id", "UnreadMailCheckeriFrame");
	ifrm.style.width = 1+"px"; 
	ifrm.style.height = 1+"px";
	ifrm.style.visibility = "hidden"; 
	unsafeWindow.document.body.appendChild(ifrm);
}

// Execute this block after everything loads and a specified delay has passed
window.addEventListener('load', function() {
										 
	setTimeout(function(){
		// Check to see if this instance of the greasemonkey script is executing on the main parent screen
		var InboxNavDiv = document.getElementById("1fbc");
		if(InboxNavDiv){
			// Add and/or reload the hidden iframe
			unsafeWindow._GMNKY_AddUpdateUnreadMessagesiFrame();

			// Set up a script that will monitor the page URL and update the navigation on change
			var intervalId = null;
			function _MonitorTopURL(){
				// clear any interval callbacks to this function
				window.clearInterval(intervalId);
				
				// Compare URL with previous one
				if(topLevelURL !== unsafeWindow.parent.location.href){
					topLevelURL = unsafeWindow.parent.location.href;
					// Call nav updater
					unsafeWindow._GMNKY_AddUpdateUnreadEmailLink();
				}
			
				// call this function again on a regular interval
				intervalId = window.setInterval(_MonitorTopURL, unsafeWindow._GMNKY_CheckURLChangesInterval);
			}
			
			_MonitorTopURL();

		}else{
			// Check to see if we're executing this instance of the greasemonkey script inside the iframe
			if(location.href.indexOf("/h/") > -1 && window.parent.location.href.indexOf("ui=2") > -1){
				// Look for the number of unread messages in the HTML
				var UnreadMessagesRegEx = unsafeWindow.document.body.innerHTML.match(/<td.*>[\r\n]*<b>[\d]*<\/b>&nbsp;-&nbsp;<b>[\d]*<\/b>.*<b>([\d]*)<\/b>/m);
				// Return it to the parent window
				if(UnreadMessagesRegEx){
					unsafeWindow.parent._GMNKY_UpdateUnreadEmailCount(UnreadMessagesRegEx[1]);
				}else{
					unsafeWindow.parent._GMNKY_UpdateUnreadEmailCount(0);
				}
			}
		}
	}, unsafeWindow._GMNKY_CheckUnreadEmailStartDelay);
	
}, true);