-
Notifications
You must be signed in to change notification settings - Fork 3
/
jquery-twitter-feed.js
224 lines (192 loc) · 8.84 KB
/
jquery-twitter-feed.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
Twitter Feed
by Zachary Kniebel
Copyright (C) 2013
This software is protected under the MIT license, below, and under the
Open Source GPL v3.0 license (http://opensource.org/licenses/GPL-3.0).
Copyright (C) 2013 Zachary Kniebel
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
(function ($) {
$.fn.twitterFeed = function (options) {
var settings = $.extend(
true, {
count: 5,
rawData: "",
//screen_name: "", /** DEPRECATED: April 1st, 2013 - Parameter is redundant, as value can be taken from JSON data, and limits plugin support**/
prepend: "",
append: "",
tweetBodyClass: "",
date: {
show: true,
cssClass: "feedItem",
order: 0,
prepend: "",
append: ""
},
reply: {
show: true,
cssClass: "feedItem",
order: 1,
prepend: "",
append: ""
},
retweet: {
show: true,
cssClass: "feedItem",
order: 2,
prepend: "",
append: ""
},
favorite: {
show: true,
cssClass: "feedItem",
order: 3,
prepend: "",
append: ""
},
callbackOnEach: false,
callback: null
},
options);
var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
var parseTweetDate = function (date) {
var date = new Date(date);
var now = new Date(Date.now());
var dateDiff = now - date;
//(max dateDiff value + 1) to display date as relative time in the corresponding unit
var seconds = 60;
var minutes = seconds * 60;
var hours = minutes * 24;
if (dateDiff < seconds) {
return dateDiff + " seconds ago";
} else if (dateDiff < minutes) {
return dateDiff + " minutes ago";
} else if (dateDiff < hours) {
return dateDiff + " hours ago";
} else {
return months[date.getUTCMonth()] + " " + date.getUTCDate() + " '" + (date.getUTCFullYear() + "").substring(2);
}
};
var optionalMarkup = function (rawTweet) {
var date = "";
var reply = "";
var retweet = "";
var favorite = "";
var orderedElements = [];
if (settings.date.show && rawTweet.created_at) {
date += settings.date.prepend;
var parsedDate = parseTweetDate(rawTweet.created_at);
if (settings.date.link) {
date += "<a href='https://twitter.com/" + rawTweet.user.screen_name + "/status/" + rawTweet.id_str + "'";
date += " class='" + settings.date.cssClass + "'>" + parsedDate + "</a>";
} else {
date += "<span class='" + settings.date.cssClass + "'>" + parsedDate + "</span>";
}
date += settings.date.append;
orderedElements.splice(settings.date.order, 0, date);
}
if (settings.reply.show) {
reply += settings.reply.prepend;
reply += "<a href='https://twitter.com/intent/tweet?in_reply_to=" + rawTweet.id_str + "' class='" + settings.reply.cssClass + "'>reply</a>";
reply += settings.reply.append;
orderedElements.splice(settings.reply.order, 0, reply);
}
if (settings.retweet.show) {
retweet += settings.retweet.prepend;
retweet += "<a href='https://twitter.com/intent/retweet?tweet_id=" + rawTweet.id_str + "' class='" + settings.retweet.cssClass + "'>retweet</a>";
retweet += settings.retweet.append;
orderedElements.splice(settings.retweet.order, 0, retweet);
}
if (settings.favorite.show) {
favorite += settings.favorite.prepend;
favorite += "<a href='https://twitter.com/intent/favorite?tweet_id=" + rawTweet.id_str + "' class='" + settings.favorite.cssClass + "'>favorite</a>";
favorite += settings.favorite.append;
orderedElements.splice(settings.favorite.order, 0, favorite);
}
var markup = "";
for (var i = 0; i < orderedElements.length; i++) {
if (orderedElements[i]) {
markup += orderedElements[i];
}
}
return markup;
};
function sanitizeTweet(text) {
var urlPattern = new RegExp("(((file|gopher|news|nntp|telnet|http|ftp|https|ftps|sftp)://)|(www\\.))+(([a-zA-Z0-9\\._-]+\\.[a-zA-Z]{2,6})|([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}))(/[a-zA-Z0-9\&%_\\./-~-]*)?", "gi");
text = text.replace(urlPattern, "<a href='$&'>$&</a>");
var handlePattern = new RegExp("@([A-Za-z0-9_]+)", "gi");
text = text.replace(handlePattern, function (match) {
return "<a href='https://twitter.com/" + match.substring(1) + "'>" + match + "</a>";
});
return text;
};
var $this = $(this);
var numTargets = $this.length;
for (var i = 0; i < numTargets; i++) {
var ele = $this[i];
/** UPDATED: Compatible with Twitter API 1.1 **/
var jsonTweets = $.parseJSON(settings.rawData);
if ($.isArray(jsonTweets)) {
jsonTweets = jsonTweets.slice(0, settings.count);
} else {
var temp = [];
temp[0] = jsonTweets;
jsonTweets = temp;
}
var numTweets = jsonTweets.length;
for (var j = 0; j < numTweets; j++) {
var rawTweet = jsonTweets[j];
if (rawTweet.text !== undefined) {
var $tweet = $(
settings.prepend +
"<span class='" + settings.tweetBodyClass + "'>" +
sanitizeTweet(rawTweet.text) +
"</span>" +
optionalMarkup(rawTweet) +
settings.append
);
$tweet.appendTo(ele);
if (settings.callbackOnEach && !! settings.callback) {
settings.callback();
}
}
}
/** DEPRECATED: March 5th, 2013 - Twitter API upgrade to 1.1, Twitter API 1.0 shutdown **/
/*var url = "http://api.twitter.com/1/statuses/user_timeline.json?callback=?&screen_name=";
url += settings.screen_name;
url += "&count=" + settings.count;
$.ajax({
cache: false
});
$.getJSON(url, function (data) {
$.each(data, function (i, rawTweet) {
if (rawTweet.text !== undefined) {
var $tweet = $("<span class='" + settings.tweetBodyClass + "'>" + sanitizeTweet(rawTweet.text) + "</span>" + optionalMarkup(rawTweet));
$tweet.appendTo(ele);
if (settings.callbackOnEach && !!settings.callback) {
settings.callback();
}
}
});
}); */
}
if (!settings.callbackOnEach && !! settings.callback) {
settings.callback();
}
};
})(jQuery);