Use d.implementation.createHTMLDocument() to parse ajax thread responses.

Doing so prevents the browser from loading every single elements such as images/scripts/css/etc,
which means less network/cache IO, which is good.
This commit is contained in:
Nicolas Stepien 2012-03-12 21:04:00 +01:00
parent 07e313b5cb
commit 9c00ad71a4
2 changed files with 22 additions and 22 deletions

View File

@ -838,21 +838,21 @@
}
},
parse: function(req, pathname, thread, a) {
var body, href, link, next, nodes, quote, reply, _i, _j, _len, _len2, _ref, _ref2;
var doc, href, link, next, nodes, quote, reply, table, _i, _j, _len, _len2, _ref, _ref2;
if (req.status !== 200) {
a.textContent = "" + req.status + " " + req.statusText;
$.off(a, 'click', ExpandThread.cb.toggle);
return;
}
a.textContent = a.textContent.replace('\u00d7 Loading...', '-');
body = $.el('body', {
innerHTML: req.responseText
});
doc = d.implementation.createHTMLDocument(null);
doc.documentElement.innerHTML = req.responseText;
nodes = [];
_ref = $$('.reply', body);
_ref = $$('.reply', doc);
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
reply = _ref[_i];
_ref2 = $$('.quotelink', reply);
table = d.importNode(reply.parentNode.parentNode.parentNode);
_ref2 = $$('.quotelink', table);
for (_j = 0, _len2 = _ref2.length; _j < _len2; _j++) {
quote = _ref2[_j];
if ((href = quote.getAttribute('href')) === quote.hash) {
@ -861,10 +861,10 @@
quote.href = "res/" + href;
}
}
link = $('.quotejs', reply);
link = $('.quotejs', table);
link.href = "res/" + thread.firstChild.id + "#" + reply.id;
link.nextSibling.href = "res/" + thread.firstChild.id + "#q" + reply.id;
nodes.push(reply.parentNode.parentNode.parentNode);
nodes.push(table);
}
while ((next = a.nextSibling) && !next.clear) {
$.rm(next);
@ -2562,7 +2562,7 @@
};
},
update: function() {
var body, id, newPosts, nodes, reply, scroll, _i, _len, _ref;
var doc, id, newPosts, nodes, reply, scroll, _i, _len, _ref;
if (this.status === 404) {
Updater.timer.textContent = '';
Updater.count.textContent = 404;
@ -2598,12 +2598,11 @@
return;
}
Updater.lastModified = this.getResponseHeader('Last-Modified');
body = $.el('body', {
innerHTML: this.responseText
});
doc = d.implementation.createHTMLDocument(null);
doc.documentElement.innerHTML = this.responseText;
id = $('input', Updater.br.previousElementSibling).name;
nodes = [];
_ref = $$('.reply', body).reverse();
_ref = $$('.reply', doc).reverse();
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
reply = _ref[_i];
if (reply.id <= id) break;

View File

@ -721,20 +721,21 @@ ExpandThread =
a.textContent = a.textContent.replace '\u00d7 Loading...', '-'
body = $.el 'body',
innerHTML: req.responseText
doc = d.implementation.createHTMLDocument null
doc.documentElement.innerHTML = req.responseText
nodes = []
for reply in $$ '.reply', body
for quote in $$ '.quotelink', reply
for reply in $$ '.reply', doc
table = d.importNode reply.parentNode.parentNode.parentNode
for quote in $$ '.quotelink', table
if (href = quote.getAttribute('href')) is quote.hash #add pathname to normal quotes
quote.pathname = pathname
else if href isnt quote.href #fix x-thread links, not x-board ones
quote.href = "res/#{href}"
link = $ '.quotejs', reply
link = $ '.quotejs', table
link.href = "res/#{thread.firstChild.id}##{reply.id}"
link.nextSibling.href = "res/#{thread.firstChild.id}#q#{reply.id}"
nodes.push reply.parentNode.parentNode.parentNode
nodes.push table
# eat everything, then replace with fresh full posts
while (next = a.nextSibling) and not next.clear #br[clear]
$.rm next
@ -2201,12 +2202,12 @@ Updater =
return
Updater.lastModified = @getResponseHeader 'Last-Modified'
body = $.el 'body',
innerHTML: @responseText
doc = d.implementation.createHTMLDocument null
doc.documentElement.innerHTML = @responseText
id = $('input', Updater.br.previousElementSibling).name
nodes = []
for reply in $$('.reply', body).reverse()
for reply in $$('.reply', doc).reverse()
break if reply.id <= id #make sure to not insert older posts
nodes.push reply.parentNode.parentNode.parentNode #table