Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added Observe ApplyLabel action

  • Loading branch information...
commit efcb4ae541bc03df70e61bb25fe4da88915da26d 1 parent 0a95419
@ahmedkotb ahmedkotb authored
Showing with 79 additions and 69 deletions.
  1. +69 −63 chrome/lib/gmailr.js
  2. +10 −6 chrome/main.js
View
132 chrome/lib/gmailr.js
@@ -6,9 +6,9 @@
*/
(function($, window) {
-
+
// Utility methods
-
+
var dbg = function(msg) {
if(Gmailr.debug)
console.log(msg);
@@ -34,17 +34,17 @@
dbg(m);
p(els);
};
-
+
var isDescendant = function(el, t) {
return t.parents().index(el) >= 0;
};
var Gmailr = {
-
+
/*****************************************************************************************
* Public Methods and Variables
*/
-
+
debug: false,
inboxLink: null,
priorityInboxLink: null,
@@ -52,11 +52,11 @@
currentInboxCount: null,
archived: [],
elements: {},
-
+
/*
This is the main initialization routine. It bootstraps Gmailr into the Gmail interface.
You must call this with a callback, like so:
-
+
Gmailr.init(funciton(G) {
// .. G is the Gmailr API object
});
@@ -64,21 +64,21 @@
init: function(cb) {
var self = this;
-
+
dbg('Initializing Gmailr API');
-
+
var delayed_loader_count = 0;
-
+
// Here we do delayed loading until success. This is in the case
// that our script loads after Gmail has already loaded.
self.delayed_loader = setInterval(function() {
self.elements.canvas = $(document.getElementById("canvas_frame").contentDocument);
self.elements.body = self.elements.canvas.find('body').first();
-
+
if(self.loaded) {
if(delayed_loader_count != 0)
dbg('Delayed loader success.');
-
+
self.elements.canvas.bind("DOMSubtreeModified", function(e) {
self.detectDOMEvents(e);
});
@@ -91,36 +91,36 @@
}
}, 500);
},
-
+
/*
Inserts the element to the top of the Gmail interface.
*/
-
+
insertTop: function(el) {
if(!this.loaded) throw "Call to insertTop before Gmail has loaded";
this.elements.body.prepend($(el));
},
-
+
/*
Allows you to apply jQuery selectors in the Gmail DOM, like so:
-
+
G.$('.my_class');
*/
-
+
$: function(selector) {
return this.elements.body.find(selector);
},
-
+
/*
Inserts a CSS file into the Gmail DOM.
*/
-
+
insertCss: function(cssFile) {
var css = $('<link rel="stylesheet" type="text/css">');
css.attr('href', cssFile);
this.elements.canvas.find('head').first().append(css);
},
-
+
/**
* Subscribe to a specific event in Gmail
* name arguments passed to callback
@@ -130,36 +130,37 @@
* 'spam' - count
* 'compose'
* 'viewChanged'
+ * 'applyLabel'
*/
observe: function(type, cb) {
this.ob_queues[type].push(cb);
},
-
+
/**
* Number of unread messages.
*/
numUnread: function() {
if(!this.loaded) throw "Call to numUnread before Gmail has loaded";
-
+
// We always look to the inbox link, bc it always displays the number unread
// no matter where you are in Gmail
-
+
//var title = this.inboxLink[0].title;
var title = this.inboxLink[0].title;
var m = /\((\d+)\)/.exec(title);
return (m && m[1]) ? parseInt(m[1]) : 0;
},
-
+
/**
* Email address of the Gmail account.
*/
emailAddress: function() {
if(!this.loaded) throw "Call to emailAddress before Gmail has loaded";
-
+
// First, try old Gmail header
var el = this.elements.canvas.find('#guser b');
if(el.length > 0) return el.first().html();
-
+
// Try the new one
var el = this.elements.canvas.find('.gbmp1');
if(el.length > 0) return el.first().html();
@@ -170,25 +171,25 @@
*/
currentView: function() {
if(!this.loaded) throw "Call to currentView before Gmail has loaded";
-
+
if(this.elements.canvas.find('h1.ha').length > 0) {
return 'conversation';
} else {
return 'thread';
}
},
-
+
/*****************************************************************************************
* Private Methods
*/
-
-
+
+
/**
* This method attempts to bootstrap Gmailr into the Gmail interface.
- * Basically, this amounts polling to make sure Gmail has fully loaded,
+ * Basically, this amounts polling to make sure Gmail has fully loaded,
* and then setting up some basic hooks.
*/
-
+
bootstrap: function(el, cb) {
var self = this;
if(el) {
@@ -230,18 +231,18 @@
}
}
},
-
+
xhrWatcher: {
xhrParams: null,
initialized: null,
-
+
init: function() {
var self = this;
-
+
if(!this.initialized) {
this.initialized = true;
var win = top.document.getElementById("js_frame").contentDocument.defaultView;
-
+
win.XMLHttpRequest.prototype._Gmail_open = win.XMLHttpRequest.prototype.open;
win.XMLHttpRequest.prototype.open = function (method, url, async, user, password) {
var out = this._Gmail_open.apply(this, arguments);
@@ -251,7 +252,7 @@
};
return out;
};
-
+
win.XMLHttpRequest.prototype._Gmail_send = win.XMLHttpRequest.prototype.send;
win.XMLHttpRequest.prototype.send = function (body) {
var out = this._Gmail_send.apply(this, arguments);
@@ -261,7 +262,7 @@
}
return out;
}
-
+
if (!top._Gmail_iframeFn) {
top._Gmail_iframeFn = top.GG_iframeFn;
this.iframeData = {};
@@ -271,7 +272,7 @@
url: top.location.href,
responseData: top.VIEW_DATA
});
-
+
top.GG_iframeFn = function(win, data) {
var d = top._Gmail_iframeFn.apply(this, arguments);
try {
@@ -300,11 +301,11 @@
return d
}
}
-
+
}
}
},
-
+
currentLeftMenuItem: null,
ob_queues: {
archive: [],
@@ -315,10 +316,11 @@
compose: [],
numUnreadChange: [],
inboxCountChange: [],
- viewChanged: []
+ viewChanged: [],
+ applyLabel: []
},
loaded: false,
-
+
liveLeftMenuItem: function() {
if(!this.loaded) return null;
@@ -329,11 +331,11 @@
return null;
}
},
-
+
inboxTabHighlighted: function() {
return this.currentTabHighlighted() == 'inbox' || this.currentTabHighlighted() == 'priority_inbox';
},
-
+
currentTabHighlighted: function() {
if(this.inboxLink && this.inboxLink.closest('.TO').hasClass('nZ')) {
return 'inbox';
@@ -343,7 +345,7 @@
return null;
}
},
-
+
// Return true if a yellow archive highlight actually means the user is archiving
archiveableState: function() {
// For threads, this overrestricts:
@@ -352,11 +354,11 @@
// TODO: should detect if the current email is in the inbox, and only assign points if it is
return (this.inboxTabHighlighted() && this.currentView() == 'thread') || (this.currentView() != 'thread');
},
-
+
mainListingEl: function() {
return this.elements.canvas.find('.Cp').first();
},
-
+
mainListingEmpty: function() {
if(this.mainListingEl().length > 0 && this.currentView() == 'thread') {
return this.mainListingEl().find('table tr').length == 0;
@@ -364,18 +366,18 @@
return null;
}
},
-
+
toolbarEl: function() {
return this.elements.canvas.find('.A1.D.E').first();
},
-
+
toolbarCount: function() {
var el = this.toolbarEl().find('.Dj');
if(el[0]) {
var t = el[0].innerHTML;
var m = /of <b>(\d+)<\/b>/.exec(t);
-
+
if(m && m[1]) {
return parseInt(m[1]);
} else {
@@ -389,29 +391,28 @@
}
}
},
-
+
detectXHREvents: function(params) {
var self = this;
-
+
try {
var m = /[?&]act=([^&]+)/.exec(params.url);
if(m && m[1]) {
var action = decodeURIComponent(m[1]);
var count = 1;
-
+
var urlParams = $.deparam(params.url);
if(params.body.length > 0) {
var postParams = $.deparam(params.body);
if(postParams['t'] instanceof Array)
count = postParams['t'].length;
-
+
if(postParams['ba']) {
// The user has cleared more than a pageful, just give'em 50 points
count = 50;
}
}
-
switch(action) {
case "rc_^i": // Archiving
// only count if from inbox or query
@@ -428,7 +429,7 @@
});
this.archived = this.archived.concat(emailsArchived);
-
+
if(emailsArchived.length > 0) {
p("User archived " + emailsArchived.length + " emails.");
this.executeObQueues('archive', count);
@@ -439,6 +440,11 @@
}
}
break;
+ case "arl": //applying label
+ var label = urlParams["acn"];
+ var emails = postParams['t'] instanceof Array ? postParams['t'] : [ postParams['t'] ];
+ this.executeObQueues('applyLabel', label, emails);
+ break;
case "tr": // Deleting
p("User deleted " + count + " emails.");
this.executeObQueues('delete', count);
@@ -455,16 +461,16 @@
case "sp": // Spam
p("User spammed " + count + " emails.");
this.executeObQueues('spam', count);
- break;
+ break;
}
}
} catch(e) {
dbg("Error in detectXHREvents: " + e);
}
},
-
+
inConversationView: false,
-
+
detectDOMEvents: function(e) {
var el = $(e.target);
@@ -474,7 +480,7 @@
this.currentLeftMenuItem = s;
this.executeObQueues('tabChange', s);
}
-
+
// Unread change
var l = this.inboxLink ? this.inboxLink : this.priorityInboxLink;
if((el[0] == l[0]) || isDescendant(el, l)) {
@@ -483,7 +489,7 @@
this.currentNumUnread = this.numUnread();
}
}*/
-
+
if(this.elements.canvas.find('.ha').length > 0) {
if(!this.inConversationView) {
this.inConversationView = true;
@@ -495,7 +501,7 @@
this.executeObQueues('viewChanged', 'threaded');
}
}
-
+
// Inbox count change
if(isDescendant(this.toolbarEl(), el)) {
var toolbarCount = this.toolbarCount();
View
16 chrome/main.js
@@ -1,6 +1,6 @@
/**
This is the example app using the Gmailr API.
-
+
In this file, you have access to the Gmailr object.
*/
@@ -13,24 +13,28 @@ Gmailr.init(function(G) {
var status = function(msg) {
G.$('#gmailr #status').html(msg);
};
-
+
G.observe('archive', function(num) {
status('You archived ' + num + ' emails.');
});
-
+
G.observe('delete', function(c) {
status('You deleted ' + c + ' emails.');
});
-
+
G.observe('spam', function(c) {
status('You marked ' + c + ' emails as spam.');
});
-
+
G.observe('compose', function() {
status('You composed an email.');
});
-
+
G.observe('reply', function(c) {
status('You replied to an email.');
});
+
+ G.observe('applyLabel', function(label,emails) {
+ status("you applied label " + label + " to " + emails.length + " email(s)");
+ });
});
Please sign in to comment.
Something went wrong with that request. Please try again.