Skip to content

Instantly share code, notes, and snippets.

@ba3r
Created December 2, 2010 07:51
Show Gist options
  • Save ba3r/724952 to your computer and use it in GitHub Desktop.
Save ba3r/724952 to your computer and use it in GitHub Desktop.
/**
* Twitter Timeline (twtuser) v2.2b
*/
+function($j){
const TWT_URL = {
japi: "http://twitter.com/statuses/user_timeline.json",
usr: "http://twitter.com/"
};
const TWT_TMPL = "<h3><a href='" + TWT_URL.usr + "${scn}' target='ws'>"
+ "<img src='${pimg}' width='32' border='0'></a> "
+ "${snm}'s tweets</h3>"
+ "{for post in posts}"
+ "<hr>"
+ "<p style='font-size:14px;'>"
+ "${post.text}"
+ "</p>"
+ "<p style='text-align:right; font-size:10px;'>"
+ "<a href='${post.link}'>"
+ "${post.pdate}"
+ "</a>"
+ "</p>"
+ "{/for}";
function processJsonData(json, posts, scn){
var nepoch = Math.floor(Date.now() / 1000);
$j.each(json, function(i, d){
var pepoch = Math.floor((new Date(d.created_at)).getTime() / 1000);
var tmp = nepoch - pepoch;
var pdate = "";
if(tmp <= 60){
pdate = tmp + "secs ago";
}else if(tmp <= (60 * 60)){
pdate = Math.floor(tmp / 60) + "mins ago";
}else if(tmp <= (60 * 60 * 24)){
pdate = Math.floor(tmp / (60 * 60)) + "hours ago";
}else if(tmp <= (60 * 60 * 24 * 15)){
pdate = Math.floor(tmp / (60 * 60 * 24)) + "days ago";
}else{
var date = new Date(d.created_at);
pdate = date.getFullYear() + "/"
+ ("0" + (date.getMonth() + 1)).slice(-2) + "/"
+ ("0" + date.getDate()).slice(-2) + " "
+ ("0" + date.getHours()).slice(-2) + ":"
+ ("0" + date.getMinutes()).slice(-2);
}
d.link = TWT_URL.usr + scn + '/status/' + d.id;
d.pdate = pdate;
posts.push(d);
});
}
wtw_rendar = {
byJSONData: function(pb, scn){
CmdUtils.previewGet(pb, TWT_URL.japi+"?id="+scn, null, function(json){
var data = [""];
data["scn"] = scn;
data["pimg"] = json[0].user.profile_image_url;
data["snm"] = json[0].user.screen_name;
var posts = new Array();
processJsonData(json, posts, scn);
data["posts"] = posts;
$j(pb).html(CmdUtils.renderTemplate(TWT_TMPL, data));
}, "json");
},
}
var noun_type_twitter_username = {
label: "user",
rankLast: true,
noExternalCalls: true,
cacheTime: 0,
suggest: function nt_twuser_suggest(text, html, cb, selected) {
if (!text || selected)
return [];
var foundAt = text[0] === '@';
if (foundAt) text = text.slice(1);
var suggs = CmdUtils.grepSuggs(text, this.logins());
if (/\w*/.test(text))
suggs.push(CmdUtils.makeSugg(text, null, {}, 0.5));
if (foundAt)
suggs = [{
__proto__: s,
text: '@' + s.text,
html: '@' + s.html,
summary: '@' + s.summary,
score: Math.pow(s.score, 0.8),
} for each (s in suggs)];
return suggs;
},
logins: function nt_twuser_logins(reload) {
if (this._list && !reload) return this._list;
var list = [];
var token = (Cc["@mozilla.org/security/pk11tokendb;1"]
.getService(Ci.nsIPK11TokenDB)
.getInternalKeyToken());
if (!token.needsLogin() || token.isLoggedIn()) {
// Look for twitter usernames stored in password manager
var usersFound = {};
var passwordManager = (Cc["@mozilla.org/login-manager;1"]
.getService(Ci.nsILoginManager));
for each (let url in ["https://twitter.com", "http://twitter.com"]) {
for each (let login in passwordManager.findLogins({}, url, "", "")) {
let {username} = login;
if (username in usersFound) continue;
usersFound[username] = true;
list.push(CmdUtils.makeSugg(username, null, login));
}
}
}
return this._list = list;
},
_list: null,
};
CmdUtils.CreateCommand({
names: ["twtuser", "twitter timeline"],
description: "Shows the Twitter timeline of the selected user.",
help: "twtuser [username]",
icon: "chrome://ubiquity/skin/icons/twitter.ico",
author: {name: "powchin", homepage: "http://wassr.jp/user/powchin"},
contributor: {name: "Michael Baer", homepage: "http://twitter.com/michabaer"},
licence: "MIT",
homepage: "http://twitter.com",
arguments:[{role: "object", nountype: noun_type_twitter_username, label: "username"}],
preview: function(pb, args){
pb.innerHTML = this.help+this._logo;
var scn = args.object.text;
if(!scn) return;
pb.innerHTML = this._loading+this._logo;
wtw_rendar.byJSONData(pb, scn);
},
previewDelay: 333,
execute: function(args){
Utils.openUrlInBrowser(TWT_URL.usr + args.object.text);
},
_logo: "<p><a href='http://twitter.com/'><img src='http://assets1.twitter.com/images/twitter_logo_s.png' width='175' height='41'></a></p>",
_loading: "Loading..."
});
}(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment