Browse code

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

Frederic Hemberger authored on 26/09/2011 at 11:53:12
Showing 1 changed files
... ...
@@ -1,43 +1,5 @@
1 1
 // JSON-P Twitter fetcher for Octopress
2 2
 // (c) Brandon Mathis // MIT Lisence
3
-function getTwitterFeed(user, count, replies) {
4
-  var feed = new jXHR();
5
-
6
-  feed.onerror = function (msg,url) {
7
-    $('#tweets li.loading').addClass('error').text("Twitter's busted");
8
-  }
9
-  feed.onreadystatechange = function(data){
10
-    if (feed.readyState === 4) {
11
-      var tweets = new Array();
12
-      var i = 0;
13
-      for (i in data){
14
-        if(tweets.length < count){
15
-          if(replies || data[i].in_reply_to_user_id == null){
16
-            tweets.push(data[i]);
17
-          }
18
-        }
19
-      }
20
-      showTwitterFeed(tweets, user);
21
-    }
22
-  };
23
-  feed.open("GET","http://twitter.com/statuses/user_timeline/"+user+".json?trim_user=true&count="+(parseInt(count)+60)+"&callback=?");
24
-  feed.send();
25
-}
26
-
27
-function showTwitterFeed(tweets, twitter_user){
28
-  var timeline = document.getElementById('tweets');
29
-  timeline.innerHTML='';
30
-  for (t in tweets){
31
-    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>';
32
-  }
33
-}
34
-function linkifyTweet(text){
35
-  return text.replace(/(https?:\/\/)([\w\-:;?&=+.%#\/]+)/gi, '<a href="$1$2">$2</a>')
36
-    .replace(/(^|\W)@(\w+)/g, '$1<a href="http://twitter.com/$2">@$2</a>')
37
-    .replace(/(^|\W)#(\w+)/g, '$1<a href="http://search.twitter.com/search?q=%23$2">#$2</a>');
38
-}
39
-
40
-
41 3
 
42 4
 // jXHR.js (JSON-P XHR) | v0.1 (c) Kyle Simpson | MIT License | http://mulletxhr.com/
43 5
 // uncompressed version available in source/javascripts/libs/jXHR.js
... ...
@@ -45,38 +7,71 @@ function linkifyTweet(text){
45 45
 
46 46
 
47 47
 /* Sky Slavin, Ludopoli. MIT license.  * based on JavaScript Pretty Date * Copyright (c) 2008 John Resig (jquery.com) * Licensed under the MIT license.  */
48
-
49 48
 function prettyDate(time) {
50
-  if (navigator.appName == 'Microsoft Internet Explorer') {
49
+  if (navigator.appName === 'Microsoft Internet Explorer') {
51 50
     return "<span>&infin;</span>"; // because IE date parsing isn't fun.
52
-  };
51
+  }
53 52
 
54
-  var say = {};
55
-  say.just_now = " now",
56
-  say.minute_ago = "1m",
57
-  say.minutes_ago = "m",
58
-  say.hour_ago = "1h",
59
-  say.hours_ago = "h",
60
-  say.yesterday = "1d",
61
-  say.days_ago = "d",
62
-  say.weeks_ago = "w"
53
+  var say = {
54
+    just_now:    " now",
55
+    minute_ago:  "1m",
56
+    minutes_ago: "m",
57
+    hour_ago:    "1h",
58
+    hours_ago:   "h",
59
+    yesterday:   "1d",
60
+    days_ago:    "d",
61
+    weeks_ago:   "w"
62
+  };
63 63
 
64
-  var current_date = new Date();
65
-  current_date_time = current_date.getTime();
66
-  current_date_full = current_date_time + (1 * 60000);
67
-  var date = new Date(time);
68
-  var diff = ((current_date_full - date.getTime()) / 1000);
69
-  var day_diff = Math.floor(diff / 86400);
64
+  var current_date = new Date(),
65
+      current_date_time = current_date.getTime(),
66
+      current_date_full = current_date_time + (1 * 60000),
67
+      date = new Date(time),
68
+      diff = ((current_date_full - date.getTime()) / 1000),
69
+      day_diff = Math.floor(diff / 86400);
70 70
 
71
-  if (isNaN(day_diff) || day_diff < 0) return "<span>&infin;</span>";
71
+  if (isNaN(day_diff) || day_diff < 0) { return "<span>&infin;</span>"; }
72 72
 
73
-  return day_diff == 0 && (
73
+  return day_diff === 0 && (
74 74
     diff < 60 && say.just_now ||
75 75
     diff < 120 && say.minute_ago ||
76 76
     diff < 3600 && Math.floor(diff / 60) + say.minutes_ago ||
77 77
     diff < 7200 && say.hour_ago ||
78 78
     diff < 86400 && Math.floor(diff / 3600) + say.hours_ago) ||
79
-    day_diff == 1 && say.yesterday ||
79
+    day_diff === 1 && say.yesterday ||
80 80
     day_diff < 7 && day_diff + say.days_ago ||
81 81
     day_diff > 7 && Math.ceil(day_diff / 7) + say.weeks_ago;
82 82
 }
83
+
84
+function linkifyTweet(text, url) {
85
+  for (var u in url) {
86
+    var shortUrl = new RegExp(url[u].url, 'g');
87
+    text = text.replace(shortUrl, '<a href="' + url[u].expanded_url + '">' + url[u].expanded_url.replace(/https?:\/\//, '') + '</a>');
88
+  }
89
+  return text.replace(/(^|\W)@(\w+)/g, '$1<a href="http://twitter.com/$2">@$2</a>')
90
+    .replace(/(^|\W)#(\w+)/g, '$1<a href="http://search.twitter.com/search?q=%23$2">#$2</a>');
91
+}
92
+
93
+function showTwitterFeed(tweets, twitter_user) {
94
+  var timeline = document.getElementById('tweets'),
95
+      content = '';
96
+
97
+  for (var t in tweets) {
98
+    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>';
99
+  }
100
+  timeline.innerHTML = content;
101
+}
102
+
103
+function getTwitterFeed(user, count, replies) {
104
+  var feed = new jXHR();
105
+  feed.onerror = function (msg,url) {
106
+    $('#tweets li.loading').addClass('error').text("Twitter's busted");
107
+  };
108
+  feed.onreadystatechange = function(data){
109
+    if (feed.readyState === 4) { showTwitterFeed(data, user); }
110
+  };
111
+
112
+  // Documentation: https://dev.twitter.com/docs/api/1/get/statuses/user_timeline
113
+  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=?");
114
+  feed.send();
115
+}
83 116
\ No newline at end of file