Created
September 5, 2008 10:31
-
-
Save satyr/8952 to your computer and use it in GitHub Desktop.
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
+function(timeline, pdate){ | |
const Twitter = 'https://twitter.com/'; | |
const Icon = 'http://assets3.twitter.com/images/favicon.ico'; | |
CmdUtils.CreateCommand({ | |
name: "say", | |
icon: Icon, | |
takes: {status: noun_arb_text}, | |
description: ''+ <>Sets/displays your | |
<a href={Twitter}>Twitter</a> status/timeline</>, | |
execute: function(input){ | |
var stat = toStat(input); | |
if(!stat) return pdate = showError('no status'); | |
jQuery.ajax({ | |
type: 'POST', | |
url: Twitter +'statuses/update.json', | |
data: {source: 'ubiquity', status: stat}, | |
dataType: 'json', | |
error: ajaxError, | |
success: function(x) | |
displayMessage({ icon: Icon, | |
title: x.truncated ? x.text +' ...' : x.text, | |
text: new Date(x.created_at).toLocaleString() }), | |
}); | |
}, | |
preview: function(block, input){ | |
var stat = toStat(input), size = stat.length, date = new Date; | |
block.innerHTML = [ | |
Style, '<div class="twitter">', | |
<span class="status">{stat} <span class={ | |
size > MaxLen ? 'excess size' : 'size'}><sup>{ | |
size}</sup>/<sub>{MaxLen}</sub></span></span>, | |
timeline || Loading, '</div>', Logo].join(''); | |
if(pdate && date - pdate < 6e4) return; | |
pdate = date; | |
jQuery.ajax({ | |
type: 'GET', | |
url: Twitter +'statuses/friends_timeline.json', | |
data: {count: 16}, | |
dataType: 'json', | |
error: ajaxError, | |
success: function(x){ | |
block.innerHTML = block.innerHTML.replace(Loading, toHTML(x)) }, | |
}); | |
}, | |
author: 'satyr'.link('http://d.hatena.ne.jp/murky-satyr'), | |
license: 'MIT', | |
}); | |
function showError(msg, type) | |
displayMessage({icon: Icon, title: (type || '')+ 'Error', text: msg}); | |
function ajaxError(x, e) | |
showError(x.status +' '+ x.statusText +' / '+ e, 'Ajax'); | |
function toStat(input) | |
input.text.replace(/^\s+|\s+$/g, '') | |
.replace(/</g, '<').replace(/</g, '>'); | |
function toHTML(res){ | |
var r = /\bhttps?:\/\/\S+/g; | |
var a = ['<table>']; | |
for each(o in res) a.push( | |
'<tr><th><img src="', o.user.profile_image_url, | |
'" width="32" height="32"/></th><td valign="top"><div>', | |
'<span class="name">', o.user.screen_name, ' [', o.user.name, ']</span> ', | |
'<span class="date">', new Date(o.created_at).toLocaleString(), | |
'</span></div>', o.text.replace(r, '<a href="$&">$&</a>'), '</td></tr>'); | |
a.push('</table>'); | |
return timeline = a.join(''); | |
} | |
const Style = <style><![CDATA[ | |
table { border:none } | |
table, .size { font-size: 72% } | |
.status { font-size: 112% } | |
.status, .name { font-weight: bold } | |
.excess { font-style: italic } | |
]]></style>.toXMLString(); | |
const Logo = <div class="logo" style="height:41px;margin-top:8px"><a | |
href={Twitter}><img style="border:none" | |
src="http://assets1.twitter.com/images/twitter_logo_s.png" | |
width="175" height="41"/></a></div>; | |
const Loading = '<p class="loading">...</p>'; | |
const MaxLen = 140; | |
}(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment