Browse code

Twitter API call and links are now protocol relative

Brandon Mathis authored on 30/09/2012 at 03:42:47
Showing 1 changed files
... ...
@@ -41,9 +41,9 @@ function prettyDate(time) {
41 41
 
42 42
 function linkifyTweet(text, url) {
43 43
   // Linkify urls, usernames, hashtags
44
-  text = text.replace(/(https?:\/\/)([\w\-:;?&=+.%#\/]+)/gi, '<a href="$1$2">$2</a>')
45
-    .replace(/(^|\W)@(\w+)/g, '$1<a href="http://twitter.com/$2">@$2</a>')
46
-    .replace(/(^|\W)#(\w+)/g, '$1<a href="http://search.twitter.com/search?q=%23$2">#$2</a>');
44
+  text = text.replace(/(https?:)(\/\/)([\w\-:;?&=+.%#\/]+)/gi, '<a href="$2$3">$3</a>')
45
+    .replace(/(^|\W)@(\w+)/g, '$1<a href="//twitter.com/$2">@$2</a>')
46
+    .replace(/(^|\W)#(\w+)/g, '$1<a href="//search.twitter.com/search?q=%23$2">#$2</a>');
47 47
 
48 48
   // Use twitter's api to replace t.co shortened urls with expanded ones.
49 49
   for (var u in url) {
... ...
@@ -62,7 +62,7 @@ function showTwitterFeed(tweets, twitter_user) {
62 62
       content = '';
63 63
 
64 64
   for (var t in tweets) {
65
-    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>';
65
+    content += '<li>'+'<p>'+'<a href="//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>';
66 66
   }
67 67
   timeline.innerHTML = content;
68 68
 }
... ...
@@ -70,7 +70,7 @@ function showTwitterFeed(tweets, twitter_user) {
70 70
 function getTwitterFeed(user, count, replies) {
71 71
   count = parseInt(count, 10);
72 72
   $.ajax({
73
-      url: "http://api.twitter.com/1/statuses/user_timeline/" + user + ".json?trim_user=true&count=" + (count + 20) + "&include_entities=1&exclude_replies=" + (replies ? "0" : "1") + "&callback=?"
73
+      url: "//api.twitter.com/1/statuses/user_timeline/" + user + ".json?trim_user=true&count=" + (count + 20) + "&include_entities=1&exclude_replies=" + (replies ? "0" : "1") + "&callback=?"
74 74
     , type: 'jsonp'
75 75
     , error: function (err) { $('#tweets li.loading').addClass('error').text("Twitter's busted"); }
76 76
     , success: function(data) { showTwitterFeed(data.slice(0, count), user); }