Use 4chan's API for the Thread Updater. Can't test right now but it should be working.

This commit is contained in:
Nicolas Stepien 2012-09-07 22:02:27 +02:00
parent 8063fe76db
commit fddbd989b3
3 changed files with 107 additions and 101 deletions

View File

@ -2933,71 +2933,76 @@
return !(d.hidden || d.oHidden || d.mozHidden || d.webkitHidden); return !(d.hidden || d.oHidden || d.mozHidden || d.webkitHidden);
}; };
}, },
update: function() { load: function() {
var count, doc, id, lastPost, nodes, reply, scroll, _i, _len, _ref, _ref1, _ref2; switch (this.status) {
if (this.status === 404) { case 404:
Updater.set('timer', ''); Updater.set('timer', '');
Updater.set('count', 404); Updater.set('count', 404);
Updater.count.className = 'warning';
clearTimeout(Updater.timeoutID);
g.dead = true;
if (Conf['Unread Count']) {
Unread.title = Unread.title.match(/^.+-/)[0] + ' 404';
} else {
d.title = d.title.match(/^.+-/)[0] + ' 404';
}
Unread.update(true);
QR.abort();
return;
}
if ((_ref = this.status) !== 0 && _ref !== 200 && _ref !== 304) {
if (Conf['Verbose']) {
Updater.set('count', this.statusText);
Updater.count.className = 'warning'; Updater.count.className = 'warning';
} clearTimeout(Updater.timeoutID);
Updater.unsuccessfulFetchCount++; g.dead = true;
return; if (Conf['Unread Count']) {
} Unread.title = Unread.title.match(/^.+-/)[0] + ' 404';
Updater.unsuccessfulFetchCount++; } else {
Updater.set('timer', -Updater.getInterval()); d.title = d.title.match(/^.+-/)[0] + ' 404';
/* }
Status Code 304: Not modified Unread.update(true);
By sending the `If-Modified-Since` header we get a proper status code, and no response. QR.abort();
This saves bandwidth for both the user and the servers, avoid unnecessary computation, break;
and won't load images and scripts when parsing the response. case 0:
*/ case 304:
/*
Status Code 304: Not modified
By sending the `If-Modified-Since` header we get a proper status code, and no response.
This saves bandwidth for both the user and the servers and avoid unnecessary computation.
*/
if ((_ref1 = this.status) === 0 || _ref1 === 304) { Updater.unsuccessfulFetchCount++;
if (Conf['Verbose']) { Updater.set('timer', -Updater.getInterval());
Updater.set('count', '+0'); if (Conf['Verbose']) {
Updater.count.className = null; Updater.set('count', '+0');
} Updater.count.className = null;
return; }
break;
case 200:
Updater.lastModified = this.getResponseHeader('Last-Modified');
Updater.cb.update(JSON.parse(this.response).posts);
Updater.set('timer', -Updater.getInterval());
break;
default:
Updater.unsuccessfulFetchCount++;
Updater.set('timer', -Updater.getInterval());
if (Conf['Verbose']) {
Updater.set('count', this.statusText);
Updater.count.className = 'warning';
}
} }
Updater.lastModified = this.getResponseHeader('Last-Modified'); return delete Updater.request;
doc = d.implementation.createHTMLDocument(''); },
doc.documentElement.innerHTML = this.response; update: function(posts) {
var count, id, lastPost, nodes, post, scroll, _i, _len, _ref;
lastPost = Updater.thread.lastElementChild; lastPost = Updater.thread.lastElementChild;
id = lastPost.id.slice(2); id = +lastPost.id.slice(2);
nodes = []; nodes = [];
_ref2 = $$('.replyContainer', doc).reverse(); _ref = posts.reverse();
for (_i = 0, _len = _ref2.length; _i < _len; _i++) { for (_i = 0, _len = _ref.length; _i < _len; _i++) {
reply = _ref2[_i]; post = _ref[_i];
if (reply.id.slice(2) <= id) { if (post.no <= id) {
break; break;
} }
nodes.push(reply); nodes.push(Build.postFromObject(post));
} }
count = nodes.length; count = nodes.length;
if (Conf['Verbose']) { if (Conf['Verbose']) {
Updater.set('count', "+" + count); Updater.set('count', "+" + count);
Updater.count.className = count ? 'new' : null; Updater.count.className = count ? 'new' : null;
} }
if (!count) { if (count) {
Updater.unsuccessfulFetchCount = 0;
} else {
Updater.unsuccessfulFetchCount++;
return; return;
} }
Updater.unsuccessfulFetchCount = 0;
Updater.set('timer', -Updater.getInterval());
scroll = Conf['Scrolling'] && Updater.scrollBG() && lastPost.getBoundingClientRect().bottom - d.documentElement.clientHeight < 25; scroll = Conf['Scrolling'] && Updater.scrollBG() && lastPost.getBoundingClientRect().bottom - d.documentElement.clientHeight < 25;
$.add(Updater.thread, nodes.reverse()); $.add(Updater.thread, nodes.reverse());
if (scroll) { if (scroll) {
@ -3044,9 +3049,9 @@
if ((_ref = Updater.request) != null) { if ((_ref = Updater.request) != null) {
_ref.abort(); _ref.abort();
} }
url = location.pathname + '?' + Date.now(); url = "//api.4chan.org/" + g.BOARD + "/res/" + g.THREAD_ID + ".json";
return Updater.request = $.ajax(url, { return Updater.request = $.ajax(url, {
onload: Updater.cb.update onload: Updater.cb.load
}, { }, {
headers: { headers: {
'If-Modified-Since': Updater.lastModified 'If-Modified-Since': Updater.lastModified

View File

@ -1,6 +1,7 @@
master master
- Mayhem - Mayhem
Use 4chan's API to fetch posts for: Use 4chan's API to fetch posts for:
- Thread Updater.
- Quote Inlining. - Quote Inlining.
- Quote Previewing. - Quote Previewing.

View File

@ -2343,64 +2343,62 @@ Updater =
-> true -> true
else else
-> !(d.hidden or d.oHidden or d.mozHidden or d.webkitHidden) -> !(d.hidden or d.oHidden or d.mozHidden or d.webkitHidden)
update: -> load: ->
if @status is 404 switch @status
Updater.set 'timer', '' when 404
Updater.set 'count', 404 Updater.set 'timer', ''
Updater.count.className = 'warning' Updater.set 'count', 404
clearTimeout Updater.timeoutID
g.dead = true
if Conf['Unread Count']
Unread.title = Unread.title.match(/^.+-/)[0] + ' 404'
else
d.title = d.title.match(/^.+-/)[0] + ' 404'
Unread.update true
QR.abort()
return
unless @status in [0, 200, 304]
# XXX 304 -> 0 in Opera
if Conf['Verbose']
Updater.set 'count', @statusText
Updater.count.className = 'warning' Updater.count.className = 'warning'
Updater.unsuccessfulFetchCount++ clearTimeout Updater.timeoutID
return g.dead = true
if Conf['Unread Count']
Updater.unsuccessfulFetchCount++ Unread.title = Unread.title.match(/^.+-/)[0] + ' 404'
Updater.set 'timer', -Updater.getInterval() else
d.title = d.title.match(/^.+-/)[0] + ' 404'
### Unread.update true
Status Code 304: Not modified QR.abort()
By sending the `If-Modified-Since` header we get a proper status code, and no response.
This saves bandwidth for both the user and the servers, avoid unnecessary computation,
and won't load images and scripts when parsing the response.
###
if @status in [0, 304]
# XXX 304 -> 0 in Opera # XXX 304 -> 0 in Opera
if Conf['Verbose'] when 0, 304
Updater.set 'count', '+0' ###
Updater.count.className = null Status Code 304: Not modified
return By sending the `If-Modified-Since` header we get a proper status code, and no response.
Updater.lastModified = @getResponseHeader 'Last-Modified' This saves bandwidth for both the user and the servers and avoid unnecessary computation.
###
doc = d.implementation.createHTMLDocument '' Updater.unsuccessfulFetchCount++
doc.documentElement.innerHTML = @response Updater.set 'timer', -Updater.getInterval()
if Conf['Verbose']
Updater.set 'count', '+0'
Updater.count.className = null
when 200
Updater.lastModified = @getResponseHeader 'Last-Modified'
Updater.cb.update JSON.parse(@response).posts
Updater.set 'timer', -Updater.getInterval()
else
Updater.unsuccessfulFetchCount++
Updater.set 'timer', -Updater.getInterval()
if Conf['Verbose']
Updater.set 'count', @statusText
Updater.count.className = 'warning'
delete Updater.request
update: (posts) ->
lastPost = Updater.thread.lastElementChild lastPost = Updater.thread.lastElementChild
id = lastPost.id[2..] id = +lastPost.id[2..]
nodes = [] nodes = []
for reply in $$('.replyContainer', doc).reverse() for post in posts.reverse()
break if reply.id[2..] <= id #make sure to not insert older posts break if post.no <= id # Make sure to not insert older posts.
nodes.push reply nodes.push Build.postFromObject post
count = nodes.length count = nodes.length
if Conf['Verbose'] if Conf['Verbose']
Updater.set 'count', "+#{count}" Updater.set 'count', "+#{count}"
Updater.count.className = if count then 'new' else null Updater.count.className = if count then 'new' else null
return unless count if count
Updater.unsuccessfulFetchCount = 0
else
Updater.unsuccessfulFetchCount++
return
Updater.unsuccessfulFetchCount = 0
Updater.set 'timer', -Updater.getInterval()
scroll = Conf['Scrolling'] and Updater.scrollBG() and scroll = Conf['Scrolling'] and Updater.scrollBG() and
lastPost.getBoundingClientRect().bottom - d.documentElement.clientHeight < 25 lastPost.getBoundingClientRect().bottom - d.documentElement.clientHeight < 25
$.add Updater.thread, nodes.reverse() $.add Updater.thread, nodes.reverse()
@ -2442,8 +2440,10 @@ Updater =
Updater.set 'timer', 0 Updater.set 'timer', 0
Updater.request?.abort() Updater.request?.abort()
# Fool the cache. # Fool the cache.
url = location.pathname + '?' + Date.now() # XXX is fooling the cache still necessary?
Updater.request = $.ajax url, onload: Updater.cb.update, # url = "//api.4chan.org/#{g.BOARD}/res/#{g.THREAD_ID}.json?{Date.now()}"
url = "//api.4chan.org/#{g.BOARD}/res/#{g.THREAD_ID}.json"
Updater.request = $.ajax url, onload: Updater.cb.load,
headers: 'If-Modified-Since': Updater.lastModified headers: 'If-Modified-Since': Updater.lastModified
Watcher = Watcher =