Created
December 12, 2008 04:26
-
-
Save satyr/35015 to your computer and use it in GitHub Desktop.
Checks/opens/composes Gmail
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Name = 'gmail', | |
Gmail = 'https://mail.google.com/mail/', | |
Base = ''+ <div class={Name}><style><![CDATA[ | |
kbd {font:bold 108% monospace; text-decoration:underline} | |
dd {margin:0.2em 0 0.8em 1em} | |
em {line-height:1.8} | |
.count {font-weight:bolder} | |
.logo {display:inline-block} | |
.loading + .logo {opacity:0.4} | |
]]></style><div id={Name} | |
class="loading"> </div><a href={Gmail} class="logo" | |
><img width="143" height="59" border="0" src="http://www.google.com/mail/help/images/logo1.gif"/></a></div>; | |
CmdUtils.CreateCommand({ | |
name: Name, | |
icon: Gmail +'images/favicon.ico', | |
description: 'Checks/opens/composes '+ 'Gmail'.link(Gmail), | |
arguments: {'object @': noun_type_contact}, | |
execute: function gm_execute({object: {text: addr}}){ | |
var {title, URL} = CmdUtils.getDocument(), me = this, [tab] = | |
Utils.tabs.search(/^https?:\/\/mail\.google\.com\/mail\/(?:[?#]|$)/); | |
if(!tab){ | |
var url = Gmail; | |
if(addr) url += Utils.paramsToString({ | |
su: title, to: addr, body: this._txt(URL), fs:1, tf:1, view: 'cm'}); | |
Utils.openUrlInBrowser(url); | |
return; | |
} | |
var {gmonkey} = tab.document.defaultView.wrappedJSObject; | |
if(gmonkey) gmonkey.load('1.0', function(gmail){ | |
try { | |
if(gmail.getActiveViewType() !== 'co'){ | |
var [cm] = $('span[role="link"]', gmail.getNavPaneElement()); | |
var ev = cm.ownerDocument.createEvent('Events'); | |
ev.initEvent('click', true, false); | |
cm.dispatchEvent(ev); | |
} | |
var $v = ($(gmail.getActiveViewElement()) | |
.find('textarea[name="to"]').val(addr).end() | |
.find('input:first').val(title).end()), | |
[ifr] = $v.find('iframe'); | |
(ifr | |
? ifr.contentDocument.execCommand('insertHTML', false, me._htm(URL)) | |
: $v.find('textarea[name="body"]').val(me._txt(URL))); | |
Utils.setTimeout(function(){ gtab.focus() }); | |
} catch(e){ me._say('Error in Gmail manipulation.', e) } | |
}); | |
else this._say('Failed to grab Gmail API.'); | |
}, | |
preview: function gm_preview(pb){ | |
if(pb.ownerDocument.getElementById(Name)) return; | |
pb.innerHTML = Base; | |
var me = this; | |
$.ajax({ | |
url: Gmail +'feed/atom', | |
dataType: 'text', | |
success: function gm_success(atom){ | |
var c = pb.ownerDocument.getElementById(Name); | |
if(!c) return; | |
c.innerHTML = me._lst(XML(atom.replace(/^\s*<\?[^>]*>/, '') | |
.replace(/ xmlns=".*?"/, ''))); | |
c.className = ''; | |
}}); | |
}, | |
_say: function gm__say(msg, err){ | |
displayMessage({text: msg, exception: err}, this); | |
}, | |
_txt: function gm__txt(u) u +'\n'+ CmdUtils.getSelection(), | |
_htm: function gm__htm(u)( | |
<><a href={u}>{u}</a><br/></> + CmdUtils.getHtmlSelection()), | |
_lst: function gm__lst(feed){ | |
var num = +feed.fullcount.text(); | |
if(!num) return '<em>No new mail.</em>'; | |
var dl = <dl/>, i = 0, k; | |
for each(var e in feed.entry) dl.appendChild( | |
<dt><kbd>{k = (++i).toString(36)}</kbd><a class="title" accesskey={k} | |
href={[email protected](/^http/, '$&s')}>{e.title +''}</a></dt> + | |
(<dd><div class="author"><a href={'mailto:'+ e.author.email} | |
>{e.author.name +''}</a><span class="issued">({ | |
(e.issued +'').replace(/[TZ]/g, ' ')})</span></div | |
><div class="summary">{e.summary +''}</div></dd>)); | |
return (<div class="count"><b>{num}</b>new mail{num > 1 ? 's' : ''} | |
</div> + dl); | |
}, | |
author: 'satyr', license: 'MIT', | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment