Skip to content

Instantly share code, notes, and snippets.

@qianjigui
Created October 15, 2009 00:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qianjigui/210523 to your computer and use it in GitHub Desktop.
Save qianjigui/210523 to your computer and use it in GitHub Desktop.
ubiquity Gmail
var gmailAppsDomain = "";
function findGmailTab()(
Utils.tabs.search(/^https?:\/\/mail\.google\.com\/mail\/(?:[?#]|$)/)[0]);
function extractGmailAppsDomain(URL) {
// given a URL, will find the gmail apps domain part of it
if (gmailAppsDomain.length > 0) {
return gmailAppsDomain;
}
var gmailAppsURL = "://mail.google.com/a/";
var index = URL.indexOf(gmailAppsURL);
if (index != -1) {
var domain = URL.slice(index+gmailAppsURL.length);
domain = domain.slice(0,domain.indexOf("/"));
if (domain != null && domain.length > 0) {
return domain;
}
}
return "none";
}
function getGmailAppsDomain() {
// looks for and returns the gmail-for-apps domain
// as well as caches it for next time
var gmailAppsURL = "://mail.google.com/a/";
// return from cache
if (gmailAppsDomain.length > 0) {
return gmailAppsDomain;
}
// look in cookie
var secure = false;
var secCookie = Utils.getCookie("mail.google.com", "GXAS");
if (secCookie == undefined) {
secCookie = Utils.getCookie("mail.google.com", "GXAS_SEC");
secure = true;
}
if (secCookie != undefined) {
// cookie is of the form hosted-domain.com=DQAAAH4AA....
var domain = secCookie.split("=")[0];
if (domain != undefined && domain.length > 0) {
gmailAppsDomain = domain;
gmailAppsDomain.secure = secure;
return gmailAppsDomain;
}
} else {
// no cookie but look in open tabs
var tab = findGmailTab();
if (tab != undefined) {
var location = String(tab.document.location);
if (location.indexOf(gmailAppsURL) != -1) {
gmailAppsDomain = extractGmailAppsDomain(location);
gmailAppsDomain.secure = (location.indexOf("https://") == 0);
}
}
}
return gmailAppsDomain;
}
function gmailCheckerAll(callback, service) {
var url = "https://mail.google.com/mail/feed/atom";
if(service == "googleapps"){
url = "https://mail.google.com/a/" + getGmailAppsDomain() + "/feed/atom";
}
jQuery.get(url, null, function(atom) {
var entries = jQuery("entry", atom);
callback((entries.map(function(i, x) {
x = jQuery(x);
return {author: x.find("author > name").text(),
subject: x.find("title").text(),
summary: x.find("summary").text(),
href: x.find("link").attr("href")};
})));
}, "xml");
}
CmdUtils.CreateCommand({
names: ["check emails","check mails","ge"],
arguments: [{role: "source",
nountype: noun_type_email_service,
label: "email service provier"}],
icon: "http://mail.google.com/mail/images/favicon.ico",
description: ("Displays your most recent incoming email. Requires a " +
'<a href="http://mail.google.com">Gmail</a> account.'),
preview: function(pBlock, arguments) {
var provider = (arguments.source && arguments.source.text) ?
arguments.source.text : "";
pBlock.innerHTML = _("Displays your most recent incoming emails...");
// Checks if user is authenticated first
// if not, do not ajaxGet, as this triggers authentication prompt
if (Utils.getCookie(".mail.google.com", "GX")) {
gmailCheckerAll(function(emails) {
if (emails.length)
pBlock.innerHTML = ("Last unread e-mail"+
(emails.length > 1 ? "s" : "")+":" +
(jQuery.map(emails, function(x, i) {
return _("<a href=\"${href}\"> <p><b>${author}</b> says: <b>${subject}</b></p> <p>${summary}</p></a>", x);
})).join("<br>"));
else
pBlock.innerHTML = _("<b>You have no new mail!</b>");
}, provider);
} else {
pBlock.innerHTML = _("You are not logged in!<br />Press enter to log in.");
}
},
execute: function(arguments) {
Utils.openUrlInBrowser("https://mail.google.com/mail/?source=navclient-ff");
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment