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,9 +2933,9 @@
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'; Updater.count.className = 'warning';
@ -2948,56 +2948,61 @@
} }
Unread.update(true); Unread.update(true);
QR.abort(); QR.abort();
return; break;
} case 0:
if ((_ref = this.status) !== 0 && _ref !== 200 && _ref !== 304) { case 304:
if (Conf['Verbose']) {
Updater.set('count', this.statusText);
Updater.count.className = 'warning';
}
Updater.unsuccessfulFetchCount++;
return;
}
Updater.unsuccessfulFetchCount++;
Updater.set('timer', -Updater.getInterval());
/* /*
Status Code 304: Not modified Status Code 304: Not modified
By sending the `If-Modified-Since` header we get a proper status code, and no response. 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, This saves bandwidth for both the user and the servers and avoid unnecessary computation.
and won't load images and scripts when parsing the response.
*/ */
if ((_ref1 = this.status) === 0 || _ref1 === 304) { Updater.unsuccessfulFetchCount++;
Updater.set('timer', -Updater.getInterval());
if (Conf['Verbose']) { if (Conf['Verbose']) {
Updater.set('count', '+0'); Updater.set('count', '+0');
Updater.count.className = null; Updater.count.className = null;
} }
return; break;
} case 200:
Updater.lastModified = this.getResponseHeader('Last-Modified'); Updater.lastModified = this.getResponseHeader('Last-Modified');
doc = d.implementation.createHTMLDocument(''); Updater.cb.update(JSON.parse(this.response).posts);
doc.documentElement.innerHTML = this.response; 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';
}
}
return delete Updater.request;
},
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,8 +2343,9 @@ 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
when 404
Updater.set 'timer', '' Updater.set 'timer', ''
Updater.set 'count', 404 Updater.set 'count', 404
Updater.count.className = 'warning' Updater.count.className = 'warning'
@ -2356,51 +2357,48 @@ Updater =
d.title = d.title.match(/^.+-/)[0] + ' 404' d.title = d.title.match(/^.+-/)[0] + ' 404'
Unread.update true Unread.update true
QR.abort() QR.abort()
return
unless @status in [0, 200, 304]
# XXX 304 -> 0 in Opera # XXX 304 -> 0 in Opera
if Conf['Verbose'] when 0, 304
Updater.set 'count', @statusText
Updater.count.className = 'warning'
Updater.unsuccessfulFetchCount++
return
Updater.unsuccessfulFetchCount++
Updater.set 'timer', -Updater.getInterval()
### ###
Status Code 304: Not modified Status Code 304: Not modified
By sending the `If-Modified-Since` header we get a proper status code, and no response. 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, This saves bandwidth for both the user and the servers and avoid unnecessary computation.
and won't load images and scripts when parsing the response.
### ###
if @status in [0, 304] Updater.unsuccessfulFetchCount++
# XXX 304 -> 0 in Opera Updater.set 'timer', -Updater.getInterval()
if Conf['Verbose'] if Conf['Verbose']
Updater.set 'count', '+0' Updater.set 'count', '+0'
Updater.count.className = null Updater.count.className = null
return when 200
Updater.lastModified = @getResponseHeader 'Last-Modified' Updater.lastModified = @getResponseHeader 'Last-Modified'
Updater.cb.update JSON.parse(@response).posts
doc = d.implementation.createHTMLDocument '' Updater.set 'timer', -Updater.getInterval()
doc.documentElement.innerHTML = @response 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 Updater.unsuccessfulFetchCount = 0
Updater.set 'timer', -Updater.getInterval() else
Updater.unsuccessfulFetchCount++
return
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 =