Use 4chan's API for thread expansion.

This commit is contained in:
Nicolas Stepien 2012-09-07 23:12:37 +02:00
parent d94d09ebaa
commit 0fcddef30f
3 changed files with 29 additions and 44 deletions

View File

@ -938,19 +938,19 @@
return _results; return _results;
}, },
toggle: function(thread) { toggle: function(thread) {
var a, num, pathname, replies, reply, _i, _len; var a, num, replies, reply, url, _i, _len;
pathname = "/" + g.BOARD + "/res/" + thread.id.slice(1); url = "//api.4chan.org/" + g.BOARD + "/res/" + thread.id.slice(1) + ".json";
a = $('.summary', thread); a = $('.summary', thread);
switch (a.textContent[0]) { switch (a.textContent[0]) {
case '+': case '+':
a.textContent = a.textContent.replace('+', '× Loading...'); a.textContent = a.textContent.replace('+', '× Loading...');
$.cache(pathname, function() { $.cache(url, function() {
return ExpandThread.parse(this, thread, a); return ExpandThread.parse(this, thread, a);
}); });
break; break;
case '×': case '×':
a.textContent = a.textContent.replace('× Loading...', '+'); a.textContent = a.textContent.replace('× Loading...', '+');
$.cache.requests[pathname].abort(); $.cache.requests[url].abort();
break; break;
case '-': case '-':
a.textContent = a.textContent.replace('-', '+'); a.textContent = a.textContent.replace('-', '+');
@ -975,44 +975,33 @@
} }
}, },
parse: function(req, thread, a) { parse: function(req, thread, a) {
var backlink, doc, href, id, link, nodes, post, quote, reply, threadID, _i, _j, _k, _l, _len, _len1, _len2, _len3, _ref, _ref1, _ref2, _ref3; var backlink, id, link, nodes, post, replies, reply, threadID, _i, _j, _k, _len, _len1, _len2, _ref, _ref1;
if (req.status !== 200) { if (req.status !== 200) {
a.textContent = "" + req.status + " " + req.statusText; a.textContent = "" + req.status + " " + req.statusText;
$.off(a, 'click', ExpandThread.cb.toggle); $.off(a, 'click', ExpandThread.cb.toggle);
return; return;
} }
a.textContent = a.textContent.replace('× Loading...', '-'); a.textContent = a.textContent.replace('× Loading...', '-');
doc = d.implementation.createHTMLDocument(''); replies = JSON.parse(req.response).posts.slice(1);
doc.documentElement.innerHTML = req.response;
threadID = thread.id.slice(1); threadID = thread.id.slice(1);
nodes = []; nodes = [];
_ref = $$('.replyContainer', doc); for (_i = 0, _len = replies.length; _i < _len; _i++) {
for (_i = 0, _len = _ref.length; _i < _len; _i++) { reply = replies[_i];
reply = _ref[_i]; post = Build.postFromObject(reply, g.BOARD);
reply = d.importNode(reply, true); id = reply.no;
_ref1 = $$('.quotelink', reply); link = $('a[title="Highlight this post"]', post);
for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
quote = _ref1[_j];
href = quote.getAttribute('href');
if (href[0] === '/') {
continue;
}
quote.href = "res/" + href;
}
id = reply.id.slice(2);
link = $('a[title="Highlight this post"]', reply);
link.href = "res/" + threadID + "#p" + id; link.href = "res/" + threadID + "#p" + id;
link.nextSibling.href = "res/" + threadID + "#q" + id; link.nextSibling.href = "res/" + threadID + "#q" + id;
nodes.push(reply); nodes.push(post);
} }
_ref2 = $$('.summary ~ .replyContainer', a.parentNode); _ref = $$('.summary ~ .replyContainer', a.parentNode);
for (_k = 0, _len2 = _ref2.length; _k < _len2; _k++) { for (_j = 0, _len1 = _ref.length; _j < _len1; _j++) {
post = _ref2[_k]; post = _ref[_j];
$.rm(post); $.rm(post);
} }
_ref3 = $$('.backlink', a.previousElementSibling); _ref1 = $$('.backlink', a.previousElementSibling);
for (_l = 0, _len3 = _ref3.length; _l < _len3; _l++) { for (_k = 0, _len2 = _ref1.length; _k < _len2; _k++) {
backlink = _ref3[_l]; backlink = _ref1[_k];
if (!$.id(backlink.hash.slice(1))) { if (!$.id(backlink.hash.slice(1))) {
$.rm(backlink); $.rm(backlink);
} }

View File

@ -4,6 +4,7 @@ master
- Thread Updater. - Thread Updater.
- Quote Inlining. - Quote Inlining.
- Quote Previewing. - Quote Previewing.
- Thread Expansion.
- Comment Expansion. - Comment Expansion.
2.34.10 2.34.10

View File

@ -758,17 +758,17 @@ ExpandThread =
$.replace span, a $.replace span, a
toggle: (thread) -> toggle: (thread) ->
pathname = "/#{g.BOARD}/res/#{thread.id[1..]}" url = "//api.4chan.org/#{g.BOARD}/res/#{thread.id[1..]}.json"
a = $ '.summary', thread a = $ '.summary', thread
switch a.textContent[0] switch a.textContent[0]
when '+' when '+'
a.textContent = a.textContent.replace '+', '× Loading...' a.textContent = a.textContent.replace '+', '× Loading...'
$.cache pathname, -> ExpandThread.parse @, thread, a $.cache url, -> ExpandThread.parse @, thread, a
when '×' when '×'
a.textContent = a.textContent.replace '× Loading...', '+' a.textContent = a.textContent.replace '× Loading...', '+'
$.cache.requests[pathname].abort() $.cache.requests[url].abort()
when '-' when '-'
a.textContent = a.textContent.replace '-', '+' a.textContent = a.textContent.replace '-', '+'
@ -791,22 +791,17 @@ ExpandThread =
a.textContent = a.textContent.replace '× Loading...', '-' a.textContent = a.textContent.replace '× Loading...', '-'
doc = d.implementation.createHTMLDocument '' replies = JSON.parse(req.response).posts[1..]
doc.documentElement.innerHTML = req.response
threadID = thread.id[1..] threadID = thread.id[1..]
nodes = [] nodes = []
for reply in $$ '.replyContainer', doc for reply in replies
reply = d.importNode reply, true post = Build.postFromObject reply, g.BOARD
for quote in $$ '.quotelink', reply id = reply.no
href = quote.getAttribute 'href' link = $ 'a[title="Highlight this post"]', post
continue if href[0] is '/' # Cross-board quote
quote.href = "res/#{href}" # Fix pathnames
id = reply.id[2..]
link = $ 'a[title="Highlight this post"]', reply
link.href = "res/#{threadID}#p#{id}" link.href = "res/#{threadID}#p#{id}"
link.nextSibling.href = "res/#{threadID}#q#{id}" link.nextSibling.href = "res/#{threadID}#q#{id}"
nodes.push reply nodes.push post
# eat everything, then replace with fresh full posts # eat everything, then replace with fresh full posts
for post in $$ '.summary ~ .replyContainer', a.parentNode for post in $$ '.summary ~ .replyContainer', a.parentNode
$.rm post $.rm post