// ----------------------------------------------------------------- // EMAIL COMMANDS // // Replaces the standard ones in Ubiquity // to work for Google Apps // v0.9.0 - Jeremy Laidman, no liability assumed, use as you will // ----------------------------------------------------------------- // This should be object-oriented // but instead it's a global var gmailAppsDomain=""; 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 findGmailTab() { var window = Application.activeWindow; var gmailURL = "://mail.google.com"; var currentLocation = String(Application.activeWindow.activeTab.document.location); if(currentLocation.indexOf(gmailURL) != -1) { return Application.activeWindow.activeTab; } for (var i = 0; i < window.tabs.length; i++) { var tab = window.tabs[i]; var location = String(tab.document.location); if (location.indexOf(gmailURL) != -1) { return tab; } } return null; } 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 secCookie = Utils.getCookie("mail.google.com", "GXAS_SEC"); if (secCookie != undefined) { // cookie is of the form laidman.org=DQAAAH4AA.... var domain=secCookie.split("=")[0]; if (domain != undefined && domain.length > 0) { gmailAppsDomain = domain; return gmailAppsDomain; } } // look in tabs var tab=findGmailTab(); if (tab != undefined) { var location=String(tab.document.location); if (location.indexOf(gmailURL) != -1) { gmailAppsDomain = extractGmailAppsDomain(location); } } return gmailAppsDomain; } function getGmailContacts( callback ) { // ensure we have a value if (gmailAppsDomain == "") { getGmailAppsDomain(); } // NOTE: URL must be SSL var url = "https://mail.google.com/a/"+gmailAppsDomain+"/contacts/data/export"; var params = { exportType: "ALL", out: "CSV" }; jQuery.get(url, params, function(data) { data = data.split("\n").slice(1); var contacts = {}; // Note: we skip the CSV heading (first) line for each( var line in data ) { var splitLine = line.split(","); var name = splitLine[0]; var email = splitLine[1]; contacts[name] = email; } callback(contacts); }, function() { // probably not logged in - fail gracefully callback({}); }); } CmdUtils.CreateCommand({ name: "email", takes: {"message": noun_arb_text}, icon: "chrome://ubiquity/skin/icons/email.png", modifiers: {to: noun_type_contact}, description:"Begins composing an email to a person from your contact list.", help:"Currently only works with Google Mail, so you'll need a Gmail account to use it." + " Try selecting part of a web page (including links, images, etc) and then issuing "email this". You can" + " also specify the recipient of the email using the word "to" and the name of someone from your contact list." + " For example, try issuing "email hello to jono" (assuming you have a friend named "jono").", preview: function(pblock, directObj, modifiers) { var html = "Creates an email message "; if (modifiers.to) { html += "to " + modifiers.to.text + " "; } if (directObj.html) { html += "with these contents:" + directObj.html; } else { html += "with a link to the current page."; } pblock.innerHTML = html; }, execute: function(directObj, headers) { var html = directObj.html; var document = context.focusedWindow.document; var title; var toAddress = ""; if (document.title) title = document.title; else title = html; var location = document.location; var gmailTab = findGmailTab(); var pageLink = "" + title + ""; if (html) { html = ("
From the page " + pageLink + ":
" + html); } else { // If there's no selection, just send the current page. html = "You might be interested in " + pageLink + ".
"; } title = "'" + title + "'"; if (headers.to) if (headers.to.text) toAddress = headers.to.text; if (gmailTab) { // Note that this is technically insecure because we're // accessing wrappedJSObject, but we're only executing this // in a Gmail tab, and Gmail is trusted code. var console = gmailTab.document.defaultView.wrappedJSObject.console; var gmonkey = gmailTab.document.defaultView.wrappedJSObject.gmonkey; var continuer = function() { // For some reason continuer.apply() won't work--we get // a security violation on Function.__parent__--so we'll // manually safety-wrap this. try { var gmail = gmonkey.get("1.0"); var sidebar = gmail.getNavPaneElement(); var composeMail = sidebar.getElementsByTagName("span")[0]; //var composeMail = sidebar.getElementById(":qw"); var event = composeMail.ownerDocument.createEvent("Events"); event.initEvent("click", true, false); composeMail.dispatchEvent(event); var active = gmail.getActiveViewElement(); var toField = composeMail.ownerDocument.getElementsByName("to")[0]; toField.value = toAddress; var subject = active.getElementsByTagName("input")[0]; if (subject) subject.value = title; var iframe = active.getElementsByTagName("iframe")[0]; if (iframe) iframe.contentDocument.execCommand("insertHTML", false, html); else { var body = composeMail.ownerDocument.getElementsByName("body")[0]; html = ("Note: the following probably looks strange because " + "you don't have rich formatting enabled. Please " + "click the 'Rich formatting' link above, discard " + "this message, and try " + "the email command again.\n\n" + html); body.value = html; } gmailTab.focus(); } catch (e) { displayMessage({text: "A gmonkey exception occurred.", exception: e}); } }; gmonkey.load("1.0", continuer); } else { // No Gmail tab open? Open a new one: var params = {fs:1, tf:1, view:"cm", su:title, to:toAddress, body:html}; Utils.openUrlInBrowser("http://mail.google.com/a/"+gmailAppsDomain+"/mail/?" + Utils.paramsToString(params)); } } }); function gmailChecker(callback) { if (gmailAppsDomain == "") { getGmailAppsDomain(); // populate the gmailAppsDomain string } var url = "http://mail.google.com/a/"+gmailAppsDomain+"/feed/atom"; Utils.ajaxGet(url, function(rss) { var emailDetails = { account: jQuery(rss).find("title:first").text().split(" ").pop(), messageCount: jQuery(rss).find("fullcount:first").text() }; var firstEntry = jQuery(rss).find("entry").get(0); if (firstEntry) { emailDetails.lastEmail = { author: jQuery(firstEntry).find("author > name").text(), subject: subject = jQuery(firstEntry).find("title").text(), summary: jQuery(firstEntry).find("summary").text(), href: jQuery(firstEntry).find("link").attr("href") }; } callback(emailDetails); }); } CmdUtils.CreateCommand({ name: "last-email", icon: "chrome://ubiquity/skin/icons/email_open.png", description: "Displays your most recent incoming email. Requires a Google Mail account.", preview: function( pBlock ) { pBlock.innerHTML = "Displays your most recent incoming email..."; // Checks if user is authenticated first - if not, do not ajaxGet, as this triggers authentication prompt if (Utils.getCookie("mail.google.com", "S") != undefined) { gmailChecker(function(emailDetails) { var previewTemplate = "You (${account}) have no new mail"; if (emailDetails.lastEmail) { var previewTemplate = "Last unread e-mail (of ${messageCount}) for ${account}:" + "" + "${lastEmail.author} says: ${lastEmail.subject}
" + "${lastEmail.summary}
" + ""; } pBlock.innerHTML = CmdUtils.renderTemplate(previewTemplate, emailDetails); }); } else { pBlock.innerHTML = "You are not logged in!