aboutsummaryrefslogtreecommitdiff
path: root/.themes/classic/source/javascripts/twitter.js
diff options
context:
space:
mode:
authorFrederic Hemberger <mail@frederic-hemberger.de>2011-09-26 13:53:12 +0200
committerFrederic Hemberger <mail@frederic-hemberger.de>2011-09-26 13:53:12 +0200
commit167db49940a7c7c8269232600c12ae6757ac4080 (patch)
tree389d2cfc61fd5f60311bc344f5e521e0f52438f4 /.themes/classic/source/javascripts/twitter.js
parent19a646b5e9e5f294896afc7552a4941933b8fb22 (diff)
downloadmy_new_personal_website-167db49940a7c7c8269232600c12ae6757ac4080.tar.xz
my_new_personal_website-167db49940a7c7c8269232600c12ae6757ac4080.zip
Refactores twitter widget
- Order of code: Define functions first before using them - Fixed issues found by jsHint (http://jshint.com) - Switched to Twitter's current API url - Expand shortened urls, fixes #165
Diffstat (limited to '.themes/classic/source/javascripts/twitter.js')
-rw-r--r--.themes/classic/source/javascripts/twitter.js113
1 files changed, 54 insertions, 59 deletions
diff --git a/.themes/classic/source/javascripts/twitter.js b/.themes/classic/source/javascripts/twitter.js
index a4e85557..ea1580e4 100644
--- a/.themes/classic/source/javascripts/twitter.js
+++ b/.themes/classic/source/javascripts/twitter.js
@@ -1,43 +1,5 @@
// JSON-P Twitter fetcher for Octopress
// (c) Brandon Mathis // MIT Lisence
-function getTwitterFeed(user, count, replies) {
- var feed = new jXHR();
-
- feed.onerror = function (msg,url) {
- $('#tweets li.loading').addClass('error').text("Twitter's busted");
- }
- feed.onreadystatechange = function(data){
- if (feed.readyState === 4) {
- var tweets = new Array();
- var i = 0;
- for (i in data){
- if(tweets.length < count){
- if(replies || data[i].in_reply_to_user_id == null){
- tweets.push(data[i]);
- }
- }
- }
- showTwitterFeed(tweets, user);
- }
- };
- feed.open("GET","http://twitter.com/statuses/user_timeline/"+user+".json?trim_user=true&count="+(parseInt(count)+60)+"&callback=?");
- feed.send();
-}
-
-function showTwitterFeed(tweets, twitter_user){
- var timeline = document.getElementById('tweets');
- timeline.innerHTML='';
- for (t in tweets){
- timeline.innerHTML+='<li>'+'<p>'+'<a href="http://twitter.com/'+twitter_user+'/status/'+tweets[t].id_str+'">'+prettyDate(tweets[t].created_at)+'</a>'+linkifyTweet(tweets[t].text.replace(/\n/g, '<br>'))+'</p>'+'</li>';
- }
-}
-function linkifyTweet(text){
- return text.replace(/(https?:\/\/)([\w\-:;?&=+.%#\/]+)/gi, '<a href="$1$2">$2</a>')
- .replace(/(^|\W)@(\w+)/g, '$1<a href="http://twitter.com/$2">@$2</a>')
- .replace(/(^|\W)#(\w+)/g, '$1<a href="http://search.twitter.com/search?q=%23$2">#$2</a>');
-}
-
-
// jXHR.js (JSON-P XHR) | v0.1 (c) Kyle Simpson | MIT License | http://mulletxhr.com/
// uncompressed version available in source/javascripts/libs/jXHR.js
@@ -45,38 +7,71 @@ function linkifyTweet(text){
/* Sky Slavin, Ludopoli. MIT license. * based on JavaScript Pretty Date * Copyright (c) 2008 John Resig (jquery.com) * Licensed under the MIT license. */
-
function prettyDate(time) {
- if (navigator.appName == 'Microsoft Internet Explorer') {
+ if (navigator.appName === 'Microsoft Internet Explorer') {
return "<span>&infin;</span>"; // because IE date parsing isn't fun.
- };
+ }
- var say = {};
- say.just_now = " now",
- say.minute_ago = "1m",
- say.minutes_ago = "m",
- say.hour_ago = "1h",
- say.hours_ago = "h",
- say.yesterday = "1d",
- say.days_ago = "d",
- say.weeks_ago = "w"
+ var say = {
+ just_now: " now",
+ minute_ago: "1m",
+ minutes_ago: "m",
+ hour_ago: "1h",
+ hours_ago: "h",
+ yesterday: "1d",
+ days_ago: "d",
+ weeks_ago: "w"
+ };
- var current_date = new Date();
- current_date_time = current_date.getTime();
- current_date_full = current_date_time + (1 * 60000);
- var date = new Date(time);
- var diff = ((current_date_full - date.getTime()) / 1000);
- var day_diff = Math.floor(diff / 86400);
+ var current_date = new Date(),
+ current_date_time = current_date.getTime(),
+ current_date_full = current_date_time + (1 * 60000),
+ date = new Date(time),
+ diff = ((current_date_full - date.getTime()) / 1000),
+ day_diff = Math.floor(diff / 86400);
- if (isNaN(day_diff) || day_diff < 0) return "<span>&infin;</span>";
+ if (isNaN(day_diff) || day_diff < 0) { return "<span>&infin;</span>"; }
- return day_diff == 0 && (
+ return day_diff === 0 && (
diff < 60 && say.just_now ||
diff < 120 && say.minute_ago ||
diff < 3600 && Math.floor(diff / 60) + say.minutes_ago ||
diff < 7200 && say.hour_ago ||
diff < 86400 && Math.floor(diff / 3600) + say.hours_ago) ||
- day_diff == 1 && say.yesterday ||
+ day_diff === 1 && say.yesterday ||
day_diff < 7 && day_diff + say.days_ago ||
day_diff > 7 && Math.ceil(day_diff / 7) + say.weeks_ago;
}
+
+function linkifyTweet(text, url) {
+ for (var u in url) {
+ var shortUrl = new RegExp(url[u].url, 'g');
+ text = text.replace(shortUrl, '<a href="' + url[u].expanded_url + '">' + url[u].expanded_url.replace(/https?:\/\//, '') + '</a>');
+ }
+ return text.replace(/(^|\W)@(\w+)/g, '$1<a href="http://twitter.com/$2">@$2</a>')
+ .replace(/(^|\W)#(\w+)/g, '$1<a href="http://search.twitter.com/search?q=%23$2">#$2</a>');
+}
+
+function showTwitterFeed(tweets, twitter_user) {
+ var timeline = document.getElementById('tweets'),
+ content = '';
+
+ for (var t in tweets) {
+ content += '<li>'+'<p>'+'<a href="http://twitter.com/'+twitter_user+'/status/'+tweets[t].id_str+'">'+prettyDate(tweets[t].created_at)+'</a>'+linkifyTweet(tweets[t].text.replace(/\n/g, '<br>'), tweets[t].entities.urls)+'</p>'+'</li>';
+ }
+ timeline.innerHTML = content;
+}
+
+function getTwitterFeed(user, count, replies) {
+ var feed = new jXHR();
+ feed.onerror = function (msg,url) {
+ $('#tweets li.loading').addClass('error').text("Twitter's busted");
+ };
+ feed.onreadystatechange = function(data){
+ if (feed.readyState === 4) { showTwitterFeed(data, user); }
+ };
+
+ // Documentation: https://dev.twitter.com/docs/api/1/get/statuses/user_timeline
+ feed.open("GET","http://api.twitter.com/1/statuses/user_timeline/" + user + ".json?trim_user=true&count=" + (parseInt(count, 10)) + "&include_entities=1&exclude_replies=" + (replies ? "0" : "1") + "&callback=?");
+ feed.send();
+} \ No newline at end of file