-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathsnapbird.js
321 lines (271 loc) · 9.67 KB
/
snapbird.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
var store = (function () {
var useStorage = !!window.localStorage;
return {
get: function (key) {
var value, ca, c, i;
if (useStorage) {
value = window.localStorage.getItem(key);
} else {
ca = document.cookie.split(';');
for (i=0; i < ca.length; i++) {
c = ca[i];
while (c.charAt(0)==' ') {
c = c.substring(1, c.length);
}
if (c && c.indexOf(key) == 0) {
value = c.substring(key.length,c.length);
break;
}
}
}
if (value == null) {
value == "";
}
return value;
},
set: function (key, value) {
if (useStorage) {
try {
window.localStorage.setItem(key, value);
} catch (e) {
// could be a QUOTA_EXCEEDED_ERR (for some reason Webkit is giving me this, just needs a restart and it goes away...)
}
} else {
document.cookie = key + "=" + value + "; path=/";
}
}
};
})();
$('body').keyup(function (event) {
if (event.keyCode == 27) {
$('body').removeClass('auth loading');
twitterlib.cancel();
}
});
// very hacky code - sorry!
var $tweets = $('#tweets ul'),
screen_name = url = state = '',
page = 1,
total_tweets = 0,
total_searched = 0,
statusTop = null,
type_string = {
timeline : 'tweets',
favs: 'favourites',
withfriends: 'friends’ tweets',
list: 'member tweets',
dm: 'received direct messages',
dm_sent: 'sent direct messages'
};
twitterlib.custom('withfriends', '/friends.php?page=%page%');
twitterlib.custom('dm', '/friends.php?page=%page%&type=direct_messages');
twitterlib.custom('dm_sent', '/friends.php?page=%page%&type=direct_messagesSent');
$('input.search').live('click', function () {
$('form').submit();
return false;
});
$(function () {
var msie6 = $.browser == 'msie' && $.browser.version < 7;
if (!msie6) {
$(window).scroll(function (event) {
var y;
// what the y position of the scroll is
if (statusTop != null) {
y = $(this).scrollTop();
// whether that's below the form
if (y >= statusTop) {
// if so, ad the fixed class
$('#tweets aside').addClass('fixed');
} else {
// otherwise remove it
$('#tweets aside').removeClass('fixed');
}
}
});
}
});
$('input[type=radio]').bind('click change', function () {
var authRequired = $('input[type=radio].authRequired').is(':checked');
var auth = $('#auth_screen_name').length;
if (authRequired && !auth) {
// show lightbox
$('body').addClass('auth');
} else if (authRequired) {
$('#auth_screen_name').css('display', 'inline-block');
$('#screen_name').hide();
} else { // not checked
$('#auth_screen_name').css('display', 'none');
$('#screen_name').show();
}
});
$('form').submit(function (e) {
e.preventDefault();
screen_name = $('#screen_name').val();
var newstate = $(this).serialize(),
type = $(this).find('input[type=radio]:checked').val(),
search = $('#search').val(),
filter = twitterlib.filter.format(search);
// if ($.trim(search.length) == 0) {
// $('#status').html('Nothing to search for.');
// // return;
// }
if (state != newstate) {
state = newstate;
store.set('screen_name', screen_name);
// if ( $('#favs').is(':checked') ) {
// type = 'favs';
// } else if ($('#withfriends').is(':checked')) {
// type = 'withfriends';
// } else
if (screen_name.match(/\//)) {
type = 'list';
}
total_tweets = 0;
total_searched = 0;
$tweets.empty();
$('#permalink').attr('href', '/' + screen_name + '/' + type + '/' + encodeURIComponent(search));
$tweets.append('<li class="searchterm">Searching <em><strong>' + screen_name + '</strong>’s ' + type_string[type] + '</em> for <strong>' + search.replace(/<>/g, function (m) { return m == '<' ? '<' : '>'; }) + '</strong></li>');
$('body').addClass('results');
updateRequestStatus();
$('body').addClass('loading');
// cancel any outstanding request, and kick off a new one
twitterlib.cancel()[type](screen_name, { filter: search }, function (data, options) {
total_searched += options.originalTweets.length;
// if there's no results, do another call - and keep going until we hit something
if (data.length == 0 && total_tweets == 0 && options.originalTweets.length == 0) {
$('#status p:first').text('Searched ' + total_searched + ' tweets, found nothing.');
updateRequestStatus();
} else if (data.length == 0 && total_tweets == 0) {
$('#status').html('<p>Searching ' + total_searched + ' tweets.</p><p>Read to: <strong>' + twitterlib.time.datetime(options.originalTweets[options.originalTweets.length - 1].created_at).replace(/ (AM|PM)/, function (a, m) { return m.toLowerCase() + ','; }) + '</strong></p>');
updateRequestStatus();
setTimeout(function () {
twitterlib.next();
}, 1000);
return;
}
if (total_tweets) {
$tweets.find('li:last').addClass('more'); // hard split line
}
var i = 0, j = 0, t, r, scrollPos = null, searches = filter.and.concat(filter.or).join('|');
for (i = 0; i < data.length; i++) {
t = twitterlib.render(data[i], i);
$tweets.append(t);
if (total_tweets == 0 && i == 0) {
$tweets.find('li:first').addClass('first');
}
// really tricky code here, we're finding *this* and all nested text nodes
// then replacing them with our new <strong>text</strong> elements
$tweets.find('.entry-content:last, .entry-content:last *').contents().filter(function () {
return this.nodeName == '#text';
}).each(function () {
// ignore blank lines
// make matches bold
var change = '';
if (/[^\s]/.test(this.nodeValue)) {
// encoding of entities happens here, so we need to reverse back out
change = this.nodeValue.replace(/[<>&]/g, function (m) {
var r = '&';
if (m == '<') {
r = '<';
} else if (m == '>') {
r = '>';
}
return r;
}).replace(new RegExp('(' + searches + ')', "gi"), "<strong>$1</strong>");
// need to convert this textNode to tags and text
$(this).replaceWith(change);
}
});
}
scrollPos = $tweets.find('li:last').offset().top;
if (scrollPos != null) {
setTimeout(function () {
$('html,body').animate({ scrollTop: scrollPos }, 500, function () {
});
}, 100);
}
total_tweets += data.length;
if (options.originalTweets.length) $('#status').html('<p>Found ' + total_tweets + ' tweet' + (total_tweets == 1 ? '' : 's') + '.</p><p>Read to: <strong>' + twitterlib.time.datetime(options.originalTweets[options.originalTweets.length - 1].created_at).replace(/ (AM|PM)/, function (a, m) { return m.toLowerCase() + ','; }) + '</strong></p>');
updateRequestStatus();
$('body').removeClass('loading');
if (statusTop == null) {
statusTop = $('#tweets aside').offset().top - parseFloat($('#tweets aside').css('margin-top').replace(/auto/, 0));
}
});
} else {
$('#status').html('<p>Searched ' + total_searched + ' tweets.</p><p>Requesting more...</p>');
$('body').addClass('loading');
setTimeout(function () { twitterlib.cancel().next(); }, 250);
}
});
function two(s) {
return (s+'').length == 1 ? '0' + s : s;
}
function updateRequestStatus() {
$.getJSON('http://twitter.com/account/rate_limit_status.json?callback=?', function (data) {
var date = new Date(Date.parse(data.reset_time));
if (! $('#status p.rate').length) $('#status').append('<p class="rate" />');
$('#status p.rate').html('Requests left: ' + data.remaining_hits + '<br />Next reset: ' + two(date.getHours()) + ':' + two(date.getMinutes()));
});
}
function getQuery(s) {
var query = {};
s.replace(/\b([^&=]*)=([^&=]*)\b/g, function (m, a, d) {
if (typeof query[a] != 'undefined') {
query[a] += ',' + d;
} else {
query[a] = d;
}
});
return query;
}
$('input[type=reset]').click(function () {
$tweets.empty();
});
if ( !$('#screen_name').val() ) {
$('#screen_name').val(store.get('screen_name'));
}
// check location.search to see if we need to prepopulate
if (window.location.search) {
var query = getQuery(window.location.search.substr(1));
if (query.screen_name) {
$('#screen_name').val(decodeURIComponent(query.screen_name));
}
if (query.search) {
$('#search').val(decodeURIComponent(query.search));
}
if (query.favs) {
$('#favs').attr('checked', 'checked');
}
}
var $ref = $('<div>M</div>').css({
'visibility' : 'hidden',
'font-size': '10px',
'line-height': '10px',
'margin': 0,
padding: 0,
overflow: 'hidden'
}).appendTo('body'), oh = 10;
var timer = setInterval(function () {
var h = $ref.height();
if (h != oh && !$('#bang').length) {
// show exploded page
$('<div id="bang" />').appendTo('body');
} else if (h == oh && $('#bang').length) {
$('#bang').remove();
}
}, 500);
$('#auth .cancel').click(function () {
$('input[type=radio]:first').click();
$('body').removeClass('auth');
});
$('#logout').click(function () {
document.cookie = 'token=; path=/';
});
if ($('#screen_name').val() && $('#search').val()) {
try {
$('form').submit();
} catch (e) {
// why is this throwing in FF?
}
}