/
endless_tweets.js
376 lines (317 loc) · 12.4 KB
/
endless_tweets.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
// ==UserScript==
// @name Endless Tweets
// @namespace http://mislav.caboo.se/
// @description Loads older tweets endlessly when you scroll on Twitter
// @include http://twitter.com/*
// @include https://twitter.com/*
// ==/UserScript==
(function(realWindow){
//= toolkit/gm_functions.js
//= toolkit/time.js
var jQuery = realWindow.jQuery,
twttr = realWindow.twttr
function livequeryRun() {
jQuery.livequery && jQuery.livequery.run()
}
var $et = {
getTimeline: function() { return(this.timeline = $('timeline')) },
getPage: function() { return(this.page = document.body.id) },
getUpdateForm: function() { return(this.updateForm = find(null, 'form.status-update-form')) },
inspectPage: function() { this.getTimeline(); this.getPage(); this.getUpdateForm() },
sidebar: $('side'),
currentUser: selectString('meta[@name="session-user-screen_name"]/@content'),
lastRead: Number(getValue('lastReadTweet', 0)),
setLastRead: function(id) { setValue('lastReadTweet', (this.lastRead = id).toString()) },
debug: getValue('debugMode', false),
sourceString: 'endlesstweets',
version: '0.9.9',
scriptSize: 0,
pageTracker: realWindow._gat && realWindow._gat._getTracker("UA-87067-6"),
segmentUser: function(seg) {
if (this.pageTracker) {
try { this.pageTracker._setVar(seg) } catch(err) {}
}
},
trackPageview: function(path) {
if (this.pageTracker) {
if (path) {
var url = (path instanceof URL) ? path : new URL(path)
path = url.pathWithQuery()
if (url.domain && url.domain != 'twitter.com') path = '/' + url.domain + '/' + path
}
try { this.pageTracker._trackPageview(path) } catch(err) {}
}
},
trackClicks: function(element, fn) {
element.addEventListener('mousedown', function(e) {
if (e.button == 0) {
var url = null
if (typeof fn == "function") url = fn.call(this, e)
else if (fn) url = fn
else if (element.href) url = element.href
if (url) this.trackPageview(url)
}
}, false)
},
getSessionCookie: function() {
return (document.cookie.toString().match(/_twitter_sess=[^\s;]+/) || [])[0]
}
}
$et.inspectPage()
$et.trackPageview()
// have "from Endless Tweets" appear when users post updates
var statusUpdateSource = find($et.updateForm, '#source')
if (statusUpdateSource) statusUpdateSource.value = $et.sourceString
function log(message) {
if ($et.debug) {
for (var i = 1; i < arguments.length; i++)
message = message.replace('%s', arguments[i])
if (typeof GM_log == "function") GM_log(message)
else if (window.console) console.log(message)
}
}
if (typeof GM_registerMenuCommand == "function") {
GM_registerMenuCommand('Endless Tweets debug mode', function() {
setValue('debugMode', (debugMode = !debugMode))
alert('debug mode ' + (debugMode ? 'ON' : 'OFF'))
})
}
if ($et.timeline) {
var enablePreloading = true,
loading = false
function stopPreloading(text) {
enablePreloading = false
var message = $E('p', { id: 'pagination-message' }, text)
insertAfter(message, $et.timeline)
}
function clearPaginationMessage() {
var message = $('pagination-message')
if (message) removeChild(message)
}
var oldLastRead = $et.lastRead
function processTweet(item) {
var id = Number(item.id.split('_')[1])
if ('home' == $et.page) {
if (id > $et.lastRead) {
// a tweet newer than the last read? mark it as new last read
$et.setLastRead(id)
} else if (id == oldLastRead) {
stopPreloading("You have reached the last read tweet.")
addClassName(item, 'last-read')
} else if (id < oldLastRead && !enablePreloading) {
addClassName(item, 'aready-read')
}
}
}
function processTimeline() {
forEach(select('> li', $et.timeline), processTweet)
}
processTimeline()
function nearingBottom() {
var viewportBottom = window.scrollY + window.innerHeight,
nearNextPageLink = document.body.clientHeight - window.innerHeight/3
return viewportBottom >= nearNextPageLink
}
// core functionality of Endless Tweets: global handler that will
// simulate a click to the "more" link when approaching bottom
window.addEventListener('scroll', function(e) {
if (enablePreloading && !loading && nearingBottom()) {
var moreButton = jQuery('#pagination a[rel=next]')
if (moreButton.length) {
var matches = moreButton.attr('href').match(/\bpage=(\d+)/)
loading = matches[0]
var pageNumber = Number(matches[1])
log('nearing the end of page; loading page %s', pageNumber)
// simulate click by manually invoking cached event handlers
// (jQuery's trigger functionality doesn't work in Greasemonkey sandbox)
var handlers = moreButton.data('events')['click']
for (guid in handlers) handlers[guid].call(moreButton.get(0))
}
}
}, false)
//= polling.js
var dynamicPages = ['/home', '/replies', '/inbox', '/favorites', '/search.html'],
pageSwitched = function() {
enablePreloading = true
clearPaginationMessage()
$et.inspectPage()
}
// listen to jQuery ajax request to do extra processing after they are done
jQuery($et.sidebar).bind('ajaxSuccess', function(e, xhr, ajax){
var url = new URL(ajax.url)
if (ajax.url.indexOf(loading) > -1) {
loading = false
} else if (dynamicPages.indexOf(url.path) != -1) {
$et.trackPageview(url)
// it's hard to detect searches with DOMNodeInserted below, so do it here
if (url.path == '/search.html') pageSwitched()
}
})
find('container', '.columns').addEventListener('DOMNodeInserted', function(event) {
var element = event.target
if (element.nodeType != 1) return
if ('timeline' == element.id) {
// defer the next step to allow for window.location and body.id to update
setTimeout(function(){
pageSwitched()
if ('home' == $et.page) processTimeline()
}, 10)
} else if ('home' == $et.page && $et.timeline == element.parentNode) {
processTweet(element)
} else if ('following' == element.parentNode.id) {
sortFriends()
}
}, false)
} else if ('show' == $et.page) {
//= inline_reply.js
}
var content = $('content')
if (content) {
// catch click to "in reply to ..." links
content.addEventListener('click', function(e) {
var link = up(e.target, 'a', this)
if (link && /^\s*in reply to /.test(link.textContent)) {
var statusID = link.href.match(/(\d+)$/)[1],
statusUrl = '/statuses/show/' + statusID + '.json',
fallback = function(xhr) { window.location = link.href }
twttr.loading()
loadJSON(statusUrl, function(response, xhr) {
if (xhr.status >= 400) { fallback(xhr); return }
onAvatarLoad(response, function() {
var update = buildUpdateFromJSON(response),
currentStatus = up(link, '.status', content)
if (currentStatus) {
// we're in a list of statuses
insertAfter(update, currentStatus)
} else {
// we're on a fresh single tweet page
insertAfter(update.parentNode, $('permalink'))
}
reveal(update)
twttr.loaded()
livequeryRun()
$et.trackPageview(statusUrl)
})
}, { onerror: fallback })
e.preventDefault()
}
}, false)
// catch TAB keypresses in the update form
content.addEventListener('keydown', function(e) {
var textarea = null
if (e.keyCode == 9 && (textarea = up(e.target, 'textarea', this))) {
if (completeName(textarea)) e.preventDefault()
}
}, false)
}
var miniMode = false
function checkViewportWidth() {
if (document.body.clientWidth < 780) {
if (!miniMode) {
addClassName(document.body, 'mini')
miniMode = true
$et.segmentUser('mini layout')
}
}
else if (miniMode) {
removeClassName(document.body, 'mini')
miniMode = false
$et.segmentUser('')
}
}
window.addEventListener('resize', checkViewportWidth, false)
checkViewportWidth()
// *** JSON to HTML markup for a single update *** //
var buildUpdateFromJSON = (function() {
var updateContainer,
updateHTML = //= tweet.haml
function prepareContainer() {
if (!updateContainer || updateContainer.parentNode)
updateContainer = $E('ol', { 'class': 'statuses' })
return updateContainer
}
return function(data) {
var isReply = data.in_reply_to_screen_name,
date = new Date(data.created_at),
preparedData = {
id: data.id,
username: data.user.screen_name, avatar: data.user.profile_image_url, real_name: data.user.name,
created_at: date.toString(), created_ago: Time.agoInWords(date) + ' ago',
text: twitterLinkify(data.text), source: data.source,
in_reply_to: data.in_reply_to_screen_name, in_reply_to_status: data.in_reply_to_status_id,
fav_action: data.favorited ? 'un-favorite' : 'favorite',
fav_class: data.favorited ? 'fav' : 'non-fav',
},
own = preparedData.username == $et.currentUser
prepareContainer()
updateContainer.innerHTML = updateHTML.replace(/[A-Z][A-Z0-9_]+/g, function(key) {
return preparedData[key.toLowerCase()] || key
})
var update = updateContainer.firstChild
// remove excess elements
if (!data.user.protected) removeChild(find(update, '.status-body > img'))
if (!data.in_reply_to_status_id) removeChild(find(update, '.meta > a[last()]'))
removeChild(find(update, '.actions a.' + (own ? 'reply' : 'del')))
return update
}
})()
function onAvatarLoad(data, callback) {
var avatar = new Image()
avatar.addEventListener('load', callback, false)
avatar.src = data.user.profile_image_url
}
//= friends.js
var jQueryOldCookie = jQuery.cookie
jQuery.cookie = function(name, value, options) {
if (value && name == "menus" && !(options && options.expires)) {
if (!options) options = {}
options.expires = 365
}
jQueryOldCookie(name, value, options)
}
// *** iPhone location map *** //
var address = find($et.sidebar, '.vcard .adr')
if (address && /[+-]?\d+\.\d+,[+-]?\d+\.\d+/.test(address.textContent)) {
var API_KEY = 'ABQIAAAAfOaovFhDnVE3QsBZj_YthxSnhvsz13Tv4UkZBHR3eJwOymtuUxT045UEYNAo1HL_pePrMexH4SYngg',
coordinates = RegExp['$&']
// create static map that links to Google Maps
address.innerHTML = '<a class="googlemap" target="_blank" href="http://maps.google.com/maps?q=' + coordinates + '"><img src="http://maps.google.com/staticmap?center=' + coordinates + '&markers=' + coordinates + ',red&zoom=13&size=165x165&key=' + API_KEY + '" alt=""></a>'
$et.trackClicks(down(address), window.location + '/map')
}
//= toolkit/update_notifier.js
var scriptURL = 'http://userscripts.org/scripts/show/24398',
wrapper = find(null, '#content > .wrapper')
if ($et.sidebar) {
var scriptInfo = $E('div', { id: 'endless_tweets', 'class': 'section' }, 'with '),
scriptLink = $E('a', { href: scriptURL, target: '_blank' }, 'Endless Tweets')
scriptInfo.appendChild(scriptLink)
scriptInfo.appendChild(document.createTextNode(' v' + $et.version))
$et.sidebar.appendChild(scriptInfo)
$et.trackClicks(scriptLink, '/endless-tweets/sidebar-link')
}
if (wrapper) checkUserscriptUpdate(scriptURL, $et.scriptSize, function() {
var notice = $E('span', { id: 'userscript_update' },
'“Endless Tweets” user script has updates (you have v' + scriptVersion + '). ')
var install = $E('a', { 'href': scriptURL }, 'Get the upgrade')
notice.appendChild(install)
var topAlert = find('content', '.bulletin.info')
if (!topAlert && 'home' == $et.page) topAlert = insertTop($E('div', { 'class': 'bulletin info' }), find(wrapper, '.section'))
if (topAlert) topAlert.appendChild(notice)
else insertTop(notice, wrapper)
$et.trackClicks(install, '/endless-tweets/update-link')
})
//= toolkit/toolkit.js
function twitterLinkify(text) {
return linkify(text, true).
replace(/(^|\W)@(\w+)/g, '$1@<a href="/$2">$2</a>')
// TODO: active hashtags `isSearchLink("processHashtagLink")`
// replace(/(^|\W)#(\w+)/g, '$1<a href="/search?q=%23$2" title="#$2" class="hashtag">#$2</a>')
}
function reveal(element) {
jQuery(element).hide().slideDown()
}
//= endless_tweets.sass
// get a reference to the jQuery object, even if it requires
// breaking out of the GreaseMonkey sandbox in Firefox
// (we need to trust Twitter.com)
})(typeof jQuery == "undefined" ? unsafeWindow : window)