Skip to content

Instantly share code, notes, and snippets.

@satyr
Created April 11, 2009 21:51
Show Gist options
  • Save satyr/93745 to your computer and use it in GitHub Desktop.
Save satyr/93745 to your computer and use it in GitHub Desktop.
const Name = 'check-twitter',
Twitter = 'http://twitter.com/',
Icon = Twitter +'favicon.ico',
Base = <div class={Name}><style><![CDATA[
${position:relative}
$ol {list-style:none; margin:0}
$li {font-size:92%; line-height:1.2em}
$ol, $li {padding:0.2em 0}
$a img {border:none}
$button {font:bold 100% "Consolas",monospace; padding:0; border-width:1px}
$.user, $.page {font-weight:bolder; font-size:larger; margin:0 0.2em}
$.user img {vertical-align:middle}
$.navi {position:absolute; top:0; left:0; width:100%; text-align:center}
$.created_at, $.source {font-size:smaller}
$.error {font-style:oblique; line-height:1.8}
$.logo {display:block}
$.loading + .logo {opacity:0.4}
$.error + .logo {opacity:0.7}
]]></style><div id={Name}> </div><a class="logo" href={Twitter}
><img width="125" height="29"
src="http://assets0.twitter.com/images/twitter_logo_125x29.png"
/></a></div>.toXMLString().replace(/\$/g, '.'+ Name +' '),
Sel = '#'+ Name,
noun_type_twitter_friend = {
_name: 'twitter friend',
list: null,
request: function request(fb, name, list, dict, page){
jQuery.ajax({
url: ('http://twitter.com/statuses/friends.json?id='+ name +
'&page='+ page),
dataType: 'json',
success: function(o){
if(page === 1) o.push({screen_name: name});
else if(!o.length) return;
var ss = [], i = 0, l = list.length;
for each(var {screen_name: sn} in o) if(!(sn in dict)){
dict[sn] = 1;
list[l++] = ss[i++] = {text: sn, summary: sn};
}
fb(ss);
request(fb, name, list, dict, page + 1);
},
error: ajaxerr,
});
},
friends: function(cb, re){
function filterBack(ss){
for(var sf = [], i = 0, l = ss.length; i < l; ++i){
var s = ss[i], x = s.text.search(re);
if(~x && s.text !== re.source) sf[x * l + i] = s;
}
sf.length && cb(sf.reduce(function(r, s)(r.push(s), r), []));
}
var {list} = this;
if(list)
Utils.setTimeout(function(){ filterBack(list) });
else {
list = this.list = [];
var dict = {};
for each(var u in noun_type_twitter_user.suggest(''))
this.request(filterBack, u.text, list, dict, 1);
}
},
suggest: function(txt, htm, cb, sx){
try { var re = RegExp(txt, 'i') } catch(_){}
if(re) this.friends(cb, re);
return [CmdUtils.makeSugg(txt, htm, null, sx)];
},
};
function ajaxerr(x, s, e){
say(x.status, x.statusText, '('+ s +')');
}
function say(){
displayMessage({icon: Icon, title: Name, text: Array.join(arguments, ' ')});
}
function set(c, a, p, f){
var [{user: {screen_name, profile_image_url}}] = a, ls = [
'<li>'+
t.text.replace(/https?:\/\/\S+/g, httplink).replace(/@(\w+)/g, atlink) +
' <a class="created_at" href="'+ Twitter +'status/'+ t.id +'">'+
new Date(t.created_at).toString('yy-MM-dd hh:mm:ss') +
'</a> <span class="source">'+ t.source +'</span></li>'
for each(t in a)],
prev = <button accesskey="," value={p - 1}>&lt;,</button>,
next = <button accesskey="." value={p + 1}>.&gt;</button>;
if(p < 2) prev.@disabled = 'disabled';
c.innerHTML = (
<><div class="navi">{prev}<span class="page">{p}</span>{next}</div>
<a class="user" href={Twitter + screen_name}
><img src={profile_image_url}/>{screen_name}</a></> +
'<ol>'+ ls.join('') +'</ol>');
for each(var b in c.getElementsByTagName('button'))
b.addEventListener('focus', function(e){
b.blur();
b.disabled = true;
f(b.value);
}, true);
c.className = '';
}
function hmm(c){
c.innerHTML = Array.slice(arguments, 1).join(' ');
c.className = 'error';
}
function httplink(t) t.link(t);
function atlink(_, t) '@'+ t.link(Twitter + t);
CmdUtils.CreateCommand({
name: Name,
synonyms: ['ctw'],
description:
'Checks '+ 'tweets'.link(Twitter) +' of the specified user.',
icon: Icon,
takes: {'screen name': noun_type_twitter_friend},
execute: function({text: sn}){
Utils.openUrlInBrowser(Twitter + sn);
},
preview: function preview(pbl, {text: sn}, _, page){
var [c] = jQuery(Sel, pbl), pg = +page || 1;
if(!c){
pbl.innerHTML = Base;
[c] = jQuery(Sel, pbl);
}
if(!sn) return;
c.className += ' loading';
CmdUtils.previewAjax(pbl, {
url: Twitter +'statuses/user_timeline.json?id='+ sn +'&page='+ pg,
dataType: 'json',
success: function(o){
if(o.length)
set(c, o, pg, function(p){ preview(pbl, {text: sn}, _, p) });
else
hmm(c, '<b>'+ sn +'</b> has no status on page <b>'+ pg +'</b>.');
},
error: function(x, s){ hmm(c, x.status, x.statusText, '('+ s +')') },
});
},
previewDelay: 222,
author: ['powchin'.link('http://tako3.com/search/powchin'),
'satyr'.link('http://ubigist.appjet.net/?o=satyr')].join(', '),
licence: 'MIT',
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment