Skip to content

Instantly share code, notes, and snippets.

@satyr
Created April 5, 2009 04:17
Show Gist options
  • Save satyr/90368 to your computer and use it in GitHub Desktop.
Save satyr/90368 to your computer and use it in GitHub Desktop.
check-calendar++
const Name = 'gcalendar',
GCal = 'http://www.google.com/calendar/',
Logo = ''+<a href={GCal}><img width="150" height="34"
style="background-color:#eee; border:none; margin-top:4px" accesskey="l"
src={GCal +'images/blue_beta_en.gif'}/></a>,
QA = <a
href="http://www.google.com/support/calendar/bin/answer.py?answer=36604"
>Quick Add</a>,
Icon = 'chrome://ubiquity/skin/icons/calendar.png';
function say(msg){
displayMessage({icon: Icon, title: Name, text: msg});
}
function err(x, s){
say(x.status +' '+ x.statusText +' ('+ s +')');
}
function url(d, m) GCal + (m || '') +'?as_sdt='+ d.toString('yyyyMMdd');
function abs(){
var attr, path = (this.getAttribute(attr = 'href') ||
this.getAttribute(attr = 'action'));
if(/^(?!https?:)/.test(path)) this[attr] = GCal + path;
}
function btn(){
var txt = this.textContent;
jQuery(this).replaceWith(
<button value={this.href} accesskey={txt[0]}>{txt}</button>.toXMLString());
}
function tie(me, pbl){
this.addEventListener('focus', function(e){
this.blur(), e.stopPropagation(), e.preventDefault();
this.disabled = true;
me.preview(pbl, {u: this.value});
}, true);
}
function cal(me, url, cb, pbl){
var opts = {
url: url, error: err,
success: function(htm){
cb.call(me, (jQuery('<div class="'+ me.name +'">'+
/<div class[^]+$/(htm))
.find('a, form').each(abs).end()));
}};
pbl ? CmdUtils.previewAjax(pbl, opts) : jQuery.ajax(opts);
}
CmdUtils.CreateCommand({
name: Name,
synonyms: ['C'],
description: <>Accesses your <a href={GCal}>Google Calendar</a>.</>+'',
help: (<>Enter:<ul style="list-style-image:none">
<li>date to check events in the preview,
then execute to open the calendar.</li>
<li>anything, then execute to {QA}.</li></ul></>+''),
icon : Icon,
takes: {'?': {
_name: 'date or event',
rankLast: true,
default: function() noun_type_date.default(),
suggest: function(txt, htm, cb, sx){
if(!txt) return [];
var ss = noun_type_date.suggest(txt),
ev = CmdUtils.makeSugg(txt, htm, null, sx);
ev.q = txt;
ev.summary = <small>{QA}:</small> + ev.summary;
ss.push(ev);
return ss;
}}},
execute: function({q, data: d}){
if(q) cal(this, url(new Date, 'm'), function($m){
var prms = {ctext: q};
$m.find('input[type="hidden"]')
.each(function(){ prms[this.name] = this.value });
jQuery.ajax({
type: 'post',
url: $m.find('form').attr('action'),
data: prms,
dataType: 'xml',
success: function(xml){
say(xml.getElementsByClassName('c1')[0].textContent);
},
error: err,
});
});
else Utils.openUrlInBrowser(url(d || new Date));
},
preview: function(pbl, {data: d, u}){
if(d || u) cal(this, u || url(d, 'm'), function($m){
($m.append(Logo)
.find('.c1:nth(1), form, span').hide().end()
.find('.c1 > a').each(btn).end()
.find('button').each(tie, [this, pbl]).end()
.appendTo((pbl.innerHTML = '', pbl)));
}, pbl);
},
author: 'satyr'.link('http://ubigist.appjet.net/?o=satyr'), license: 'MIT',
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment