forked from gabrieleromanato/jQuery-TwitterFeed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.twitterfeed.js
executable file
·231 lines (178 loc) · 6.21 KB
/
jquery.twitterfeed.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
225
226
227
228
229
230
231
/** TwitterFeed - Get your latest tweets with jQuery
@requires jQuery 1.5+
@author Gabriele Romanato
Usage $(element).twitterFeed(options);
Options:
url: Path to the main twitterfeed.php file
cache: true|false If true, your tweets will be cached for 1 hour
*/
(function(factory) {
if (typeof define === 'function' && define.amd && define.amd.jQuery) {
define(['jquery'], factory);
} else {
factory(jQuery);
}
} (function($) {
var pluses = /\+/g;
function raw(s) {
return s;
}
function decoded(s) {
return decodeURIComponent(s.replace(pluses, ' '));
}
function converted(s) {
if (s.indexOf('"') === 0) {
s = s.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\');
}
try {
return config.json ? JSON.parse(s) : s;
} catch(er) {}
}
var config = $.cookie = function(key, value, options) {
if (value !== undefined) {
options = $.extend({},
config.defaults, options);
if (typeof options.expires === 'number') {
var days = options.expires,
t = options.expires = new Date();
t.setHours(t.getHours() + days);
}
value = config.json ? JSON.stringify(value) : String(value);
return (document.cookie = [encodeURIComponent(key), '=', config.raw ? value: encodeURIComponent(value), options.expires ? '; expires=' + options.expires.toUTCString() : '', options.path ? '; path=' + options.path: '', options.domain ? '; domain=' + options.domain: '', options.secure ? '; secure': ''].join(''));
}
var decode = config.raw ? raw: decoded;
var cookies = document.cookie.split('; ');
var result = key ? undefined: {};
for (var i = 0, l = cookies.length; i < l; i++) {
var parts = cookies[i].split('=');
var name = decode(parts.shift());
var cookie = decode(parts.join('='));
if (key && key === name) {
result = converted(cookie);
break;
}
if (!key) {
result[name] = converted(cookie);
}
}
return result;
};
config.defaults = {};
$.removeCookie = function(key, options) {
if ($.cookie(key) !== undefined) {
$.cookie(key, '', $.extend(options, {
expires: -1
}));
return true;
}
return false;
};
}));
(function($) {
$.fn.twitterFeed = function(options) {
var that = this;
var settings = {
url: 'twitterfeed.php',
cache: false
};
options = $.extend(settings, options);
var TwitterFeed = new
function() {
var url = options.url;
var relativeTime = function(time_value) {
var values = time_value.split(" ");
time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
var parsed_date = Date.parse(time_value);
var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
delta = delta + (relative_to.getTimezoneOffset() * 60);
if (delta < 60) {
return 'less than a minute ago';
} else if (delta < 120) {
return 'about a minute ago';
} else if (delta < (60 * 60)) {
return (parseInt(delta / 60)).toString() + ' minutes ago';
} else if (delta < (120 * 60)) {
return 'about an hour ago';
} else if (delta < (24 * 60 * 60)) {
return 'about ' + (parseInt(delta / 3600)).toString() + ' hours ago';
} else if (delta < (48 * 60 * 60)) {
return '1 day ago';
} else {
return (parseInt(delta / 86400)).toString() + ' days ago';
}
};
var createCookie = function(value) {
$.cookie('tweet', value, {
expires: 1
});
};
var getCookie = function() {
var cookie = $.cookie('tweet');
return cookie;
};
var replaceURLs = function(text) {
var replaced = text.replace(/((ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?)/gi, '<a href="$1">$1</a>').
replace(/(^|\s)#(\w+)/g, '$1<a href="https://twitter.com/search?q=%23$2&src=hash">#$2</a>').
replace(/(^|\s)@(\w+)/g, '$1<a href="https://twitter.com/$2">@$2</a>');
return replaced;
};
this.fetch = function() {
if (!options.cache) {
$.getJSON(url,
function(data) {
var html = '<div id="tweets-wrapper">';
$.each(data,
function(i, item) {
var tweet = replaceURLs(item.text);
var time = relativeTime(item.created_at);
var id = item.id_str;
html += '<div class="tweet">';
html += tweet;
html += '<small>' + time + '</small>';
html += '<div class="tweet-actions">';
html += '<a class="tweet-reply" href="https://twitter.com/intent/tweet?in_reply_to=' + id + '" target="_blank">Reply</a>';
html += '<a class="tweet-retweet" href="https://twitter.com/intent/retweet?tweet_id=' + id + '" target="_blank">Retweet</a>';
html += '<a class="tweet-favorite" href="https://twitter.com/intent/favorite?tweet_id=' + id + '" target="_blank">Favorite</a>';
html += '</div>';
html += '</div>';
});
html += '</div>';
that.html(html);
});
} else {
var tweet = getCookie();
if (tweet !== undefined) {
that.html(tweet);
} else {
$.getJSON(url,
function(data) {
var html = '<div id="tweets-wrapper">';
$.each(data,
function(i, item) {
var tweet = replaceURLs(item.text);
var time = relativeTime(item.created_at);
var id = item.id_str;
html += '<div class="tweet">';
html += tweet;
html += '<small>' + time + '</small>';
html += '<div class="tweet-actions">';
html += '<a class="tweet-reply" href="https://twitter.com/intent/tweet?in_reply_to=' + id + '" target="_blank">Reply</a>';
html += '<a class="tweet-retweet" href="https://twitter.com/intent/retweet?tweet_id=' + id + '" target="_blank">Retweet</a>';
html += '<a class="tweet-favorite" href="https://twitter.com/intent/favorite?tweet_id=' + id + '" target="_blank">Favorite</a>';
html += '</div>';
html += '</div>';
});
html += '</div>';
createCookie(html);
that.html(getCookie());
});
}
}
};
} ();
return that.each(function() {
TwitterFeed.fetch();
});
};
})(jQuery);