Permalink
Browse files

Init.

  • Loading branch information...
winston committed Mar 31, 2010
0 parents commit 6d05aba789ad63720ea8a73759e40184bf899263
Showing with 232 additions and 0 deletions.
  1. +232 −0 gmail_unreads_summary.user.js
@@ -0,0 +1,232 @@
+// ==UserScript==
+// @name Gmail Unreads Summary
+// @description Summary of unread emails in Gmail's current view, grouped by sender with count.
+// @copyright 2009+, Winston Teo Yong Wei (http://www.winstonyw.com)
+// @version 0.2.3
+// @require http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js
+// @include *
+// ==/UserScript==
+//
+// ==Changelog==
+// 0.0.1
+// - Prototype.
+// 0.0.2
+// - Implemented [close] functionality by setting GM value.
+// 0.0.3
+// - Removed [close] functionality.
+// - Implemented [minimize] and [maximize] functionality.
+// 0.0.4
+// - Updated @require to use Google Ajax API.
+// 0.0.5
+// - Updated search urls with URL parameters.
+// - Updated search urls with 'target=_top' for in-browser refresh.
+// - Encoded email in search urls.
+// 0.0.6
+// - Updated [minimize] and [maximize] functionality to make display / hide sticky across views.
+// 0.1.0
+// - GMailGreasemonkey API broke for functions: getNavPaneElement() and addNavModule()
+// - Removed usage of getNavPaneElement() which was used for creating Query URL.
+// 0.1.1
+// - GMailGreasemonkey API broke for functions: getActiveViewType() and getActiveViewElement()
+// - Implemented winActiveViewType() and winActiveViewElement()
+// - Froze jQuery at version 1.3.2.
+// - Generalised @include; Fix for GoogleApp users.
+// 0.1.2
+// - GMailGreasemonkey API broke for functions: registerViewChangeCallback()
+// - Added manual "Refresh" link to refresh summary.
+// 0.2.0
+// - Updated position of "Refresh" link.
+// - Updated "Refresh" link to "Refresh GMUS" link.
+// - Refactored functions.
+// - Updated variable names.
+// 0.2.1
+// - Failed to update jQuery to version 1.4.2. - See http://forum.jquery.com/topic/importing-jquery-1-4-1-into-greasemonkey-scripts-generates-an-error
+// - Updated Main URL.
+// 0.2.2
+// - Updated display of Summary; Sort by "Number of Emails", descending.
+// - Fixed bug with email counts, caused by Hidden views generated by search links.
+// 0.2.3
+// - Fixed bug with 'sname and 'email', caused by Threaded and Sent Emails.
+// ==/Changelog==
+
+
+$(document).ready(function() {
+
+ var GMAIL = null;
+
+ // Loads Gmail-Greasemonkey API (http://code.google.com/p/gmail-greasemonkey/wiki/GmailGreasemonkey10API)
+ if (unsafeWindow.gmonkey) {
+ unsafeWindow.gmonkey.load('1.0', init);
+ }
+
+ function init(obj) {
+
+ GMAIL = obj;
+
+ create_view();
+
+ };
+
+ function create_view() {
+
+ window.setTimeout(function() {
+
+ GM_log("Gmail Unreads Summary: Start");
+
+ // Display Views
+ switch(winActiveViewType()) {
+ case 'tl' : break;
+ default : return false;
+ }
+
+ // Gmail API
+ var head = GMAIL.getMastheadElement();
+ var body = winActiveViewElement();
+
+ // jQuerised JS Elements
+ var jhead = $(head);
+ var jbody = $(body);
+
+ // Reset
+ var gmus = jhead.find('div#gmus');
+ gmus.remove();
+
+ // Create Views
+ create_refresh(jhead, jbody);
+ create_content(jhead, jbody);
+
+ GM_log("Gmail Unreads Summary: End");
+
+ }, 1000);
+
+ };
+
+ function create_refresh(jhead, jbody) {
+
+ if (!jbody.find('div#gmus_refresh').is('*')) {
+
+ // Create Refresh Link
+ var jlink = $('<div id="gmus_refresh" class="J-J5-Ji"><a class="AP" href="#">Refresh GMUS</a></div>');
+
+ // Append to Body
+ jbody.find('div.J-J5-Ji').find('div.AP').parent().after(jlink);
+
+ // Action - Refresh Binding
+ var gmus_refresh = jbody.find('div#gmus_refresh');
+
+ gmus_refresh.bind('click', function() {
+ create_view();
+ });
+
+ }
+
+ };
+
+ function create_content(jhead, jbody) {
+
+ // Summarise
+ var summary = new Array();
+ var cnt = 0;
+
+ var dhref = jhead.find('a.e').attr('href'); // Main URL
+ var items = jbody.find('tr.zE'); // Class for Unread Emails
+
+ for (var i=0; i<items.length; i++) {
+
+ var item = items[i];
+
+ // Search Links generate Hidden views. Ignore!
+ if ($(item).parents(':hidden').length) { continue; }
+
+ // Build Name, Email and Query
+ var auths = $(item).find('span.zF');
+ var sname = $(auths).filter( function (index) { if ($(auths).length > 1) { return $(this).html() != 'me'; } else { return true; } } ).filter(':last').html();
+ var email = $(auths).filter( function (index) { if ($(auths).length > 1) { return $(this).html() != 'me'; } else { return true; } } ).filter(':last').attr('email');
+ var query = dhref.substring(0, dhref.indexOf('#')) + '#search/in:inbox+label:unread+from:' + encodeURIComponent(email);
+
+ if (summary[email] == null) {
+ summary[email] = new Array(sname, email, query, 1);
+ } else {
+ summary[email][3] += 1;
+ }
+ cnt++;
+
+ }
+
+ // Convert Summary Hash to Array
+ var sgroups = new Array();
+ for (var email in summary) {
+ sgroups.push(summary[email]);
+ }
+
+ // Sorting Summary Array By Count Desc
+ sgroups.sort(
+ function(a,b) {
+ a = a[3];
+ b = b[3];
+ return a == b ? 0 : (a > b ? -1 : 1)
+ }
+ );
+
+ if (cnt > 0) {
+
+ // Create Content
+ var jelem = $('<div id="gmus" style="display: block; overflow: hidden; background: #E0ECFF; border: 4px solid #C3D9FF; margin: 10px; padding: 10px; ">&nbsp;</div>');
+ jelem.append('<h5 class="header" style="display: block; float: left; padding: 0px; margin: 2px 0;">Summary of Unread Emails in Current View' + ' (Total Items: ' + cnt + ')' + '</h5>');
+ jelem.append('<div id="gmus_toggler" style="float: right; cursor: pointer; font-family: Courier; font-size: 12px; color: #0000CC;">[' + (GM_getValue('gmus_hide', false) ? '+' : '-') + ']</div>');
+
+ var contents = '<div id="gmus_content" style="display:' + (GM_getValue('gmus_hide', false) ? 'none' : 'block') + ';"><ul style="list-style-type: none; padding: 0px; margin 0px;">'
+ for (var i=0; i<sgroups.length; i++) {
+ var sgroup = sgroups[i];
+ contents += '<li style="display: block; float: left; margin: 2px 10px 2px 5px; width: 185px; font-size: 12px; font-weight: bold;"><a target="_top" href="' + sgroup[2] + '" title="' + sgroup[1] + '">' + sgroup[0] + ' (' + sgroup[3] + ')' + '</a></li>';
+ }
+ contents += '</ul></div>';
+ jelem.append(contents);
+
+ // Append to Head
+ jhead.append(jelem);
+
+ // Action - Minimize + Maximize Binding
+ var gmus_toggler = jhead.find('div#gmus_toggler');
+ var gmus_content = jhead.find('div#gmus_content');
+
+ gmus_toggler.bind('click', function() {
+ if (GM_getValue('gmus_hide',false)) {
+ gmus_toggler.html('[-]');
+ gmus_content.show();
+ GM_setValue('gmus_hide', false);
+ } else {
+ gmus_toggler.html('[+]');
+ gmus_content.hide();
+ GM_setValue('gmus_hide', true);
+ }
+ });
+
+ }
+
+ };
+
+
+ /*****
+ * Gmail-Greasemonkey API (http://code.google.com/p/gmail-greasemonkey/wiki/GmailGreasemonkey10API) Implementation
+ *****/
+
+ // Re-implementation of getActiveViewType
+ function winActiveViewType() {
+
+ var body = $('iframe#canvas_frame', parent.document).contents().find('div.nH.q0CeU.z');
+
+ if (body.is("*")) {
+ return 'tl';
+ } else {
+ return 'na';
+ }
+
+ };
+
+ // Re-implementation of getActiveViewElement
+ function winActiveViewElement() {
+ return $('iframe#canvas_frame', parent.document).contents().find('div.nH.q0CeU.z');
+ };
+
+});

0 comments on commit 6d05aba

Please sign in to comment.