Main.preParse: Get rid of a loop for parsing nodes except at Main.ready.

This commit is contained in:
Nicolas Stepien 2012-03-05 03:55:55 +01:00
parent 07fce347f6
commit 29227f7ed3
2 changed files with 56 additions and 49 deletions

View File

@ -3862,7 +3862,7 @@
return $.ready(Main.ready); return $.ready(Main.ready);
}, },
ready: function() { ready: function() {
var MutationObserver, form, navList, nodes, observer, _i, _len, _ref; var MutationObserver, form, navList, node, nodes, observer, _i, _j, _len, _len2, _ref, _ref2;
if (d.title === '4chan - 404') { if (d.title === '4chan - 404') {
redirect.init(); redirect.init();
return; return;
@ -3894,7 +3894,12 @@
if (conf['Comment Expansion']) expandComment.init(); if (conf['Comment Expansion']) expandComment.init();
if (conf['Index Navigation']) nav.init(); if (conf['Index Navigation']) nav.init();
} }
nodes = $$('.op, a + table', form); nodes = [];
_ref2 = $$('.op, a + table', form);
for (_j = 0, _len2 = _ref2.length; _j < _len2; _j++) {
node = _ref2[_j];
nodes.push(Main.preParse(node));
}
Main.node(nodes, true); Main.node(nodes, true);
if (MutationObserver = window.WebKitMutationObserver || window.MozMutationObserver || window.OMutationObserver || window.MutationObserver) { if (MutationObserver = window.WebKitMutationObserver || window.MozMutationObserver || window.OMutationObserver || window.MutationObserver) {
observer = new MutationObserver(Main.observer); observer = new MutationObserver(Main.observer);
@ -3926,33 +3931,33 @@
return window.location = "https://raw.github.com/mayhemydg/4chan-x/" + version + "/4chan_x.user.js"; return window.location = "https://raw.github.com/mayhemydg/4chan-x/" + version + "/4chan_x.user.js";
} }
}, },
preParse: function(node) {
var klass, post;
klass = node.className;
post = {
root: node,
el: klass === 'op' ? node : node.firstChild.firstChild.lastChild,
"class": klass,
threadId: g.THREAD_ID || $.x('ancestor::div[contains(@class,"thread")]', node).firstChild.id,
isOP: klass === 'op',
isInlined: /\binline\b/.test(klass),
filesize: $('.filesize', node),
img: $('img[md5]', node),
quotes: $$('.quotelink', node),
backlinks: $$('.backlink', node)
};
post.id = post.el.id;
return post;
},
node: function(nodes, notify) { node: function(nodes, notify) {
var callback, klass, node, post, posts, _i, _j, _k, _len, _len2, _len3, _ref; var callback, node, _i, _j, _len, _len2, _ref;
posts = [];
for (_i = 0, _len = nodes.length; _i < _len; _i++) {
node = nodes[_i];
klass = node.className;
posts.push({
root: node,
el: klass === 'op' ? node : node.firstChild.firstChild.lastChild,
"class": klass,
id: $('input', node).name,
threadId: g.THREAD_ID || $.x('ancestor::div[contains(@class,"thread")]', node).firstChild.id,
isOP: klass === 'op',
isInlined: /\binline\b/.test(klass),
filesize: $('.filesize', node),
img: $('img[md5]', node),
quotes: $$('.quotelink', node),
backlinks: $$('.backlink', node)
});
}
_ref = g.callbacks; _ref = g.callbacks;
for (_j = 0, _len2 = _ref.length; _j < _len2; _j++) { for (_i = 0, _len = _ref.length; _i < _len; _i++) {
callback = _ref[_j]; callback = _ref[_i];
try { try {
for (_k = 0, _len3 = posts.length; _k < _len3; _k++) { for (_j = 0, _len2 = nodes.length; _j < _len2; _j++) {
post = posts[_k]; node = nodes[_j];
callback(post); callback(node);
} }
} catch (err) { } catch (err) {
if (notify) { if (notify) {
@ -3969,15 +3974,15 @@
_ref = mutation.addedNodes; _ref = mutation.addedNodes;
for (_j = 0, _len2 = _ref.length; _j < _len2; _j++) { for (_j = 0, _len2 = _ref.length; _j < _len2; _j++) {
addedNode = _ref[_j]; addedNode = _ref[_j];
if (addedNode.nodeName === 'TABLE') nodes.push(addedNode); if (addedNode.nodeName === 'TABLE') nodes.push(Main.preParse(addedNode));
} }
} }
if (nodes.length) return Main.node(nodes); if (posts.length) return Main.node(nodes);
}, },
listener: function(e) { listener: function(e) {
var target; var target;
target = e.target; target = e.target;
if (target.nodeName === 'TABLE') return Main.node([target]); if (target.nodeName === 'TABLE') return Main.node([Main.preParse(target)]);
}, },
css: '\ css: '\
/* dialog styling */\ /* dialog styling */\

View File

@ -3209,7 +3209,9 @@ Main =
if conf['Index Navigation'] if conf['Index Navigation']
nav.init() nav.init()
nodes = $$ '.op, a + table', form nodes = []
for node in $$ '.op, a + table', form
nodes.push Main.preParse node
Main.node nodes, true Main.node nodes, true
if MutationObserver = window.WebKitMutationObserver or window.MozMutationObserver or window.OMutationObserver or window.MutationObserver if MutationObserver = window.WebKitMutationObserver or window.MozMutationObserver or window.OMutationObserver or window.MutationObserver
@ -3236,25 +3238,25 @@ Main =
if version and version isnt VERSION and confirm 'An updated version of 4chan X is available, would you like to install it now?' if version and version isnt VERSION and confirm 'An updated version of 4chan X is available, would you like to install it now?'
window.location = "https://raw.github.com/mayhemydg/4chan-x/#{version}/4chan_x.user.js" window.location = "https://raw.github.com/mayhemydg/4chan-x/#{version}/4chan_x.user.js"
preParse: (node) ->
klass = node.className
post =
root: node
el: if klass is 'op' then node else node.firstChild.firstChild.lastChild
class: klass
threadId: g.THREAD_ID or $.x('ancestor::div[contains(@class,"thread")]', node).firstChild.id
isOP: klass is 'op'
isInlined: /\binline\b/.test klass
filesize: $ '.filesize', node
img: $ 'img[md5]', node
quotes: $$ '.quotelink', node
backlinks: $$ '.backlink', node
post.id = post.el.id
post
node: (nodes, notify) -> node: (nodes, notify) ->
posts = []
for node in nodes
klass = node.className
posts.push
root: node
el: if klass is 'op' then node else node.firstChild.firstChild.lastChild
class: klass
id: $('input', node).name
threadId: g.THREAD_ID or $.x('ancestor::div[contains(@class,"thread")]', node).firstChild.id
isOP: klass is 'op'
isInlined: /\binline\b/.test klass
filesize: $ '.filesize', node
img: $ 'img[md5]', node
quotes: $$ '.quotelink', node
backlinks: $$ '.backlink', node
for callback in g.callbacks for callback in g.callbacks
try try
callback post for post in posts callback node for node in nodes
catch err catch err
alert "4chan X error: #{err.message}\nhttp://mayhemydg.github.com/4chan-x/#bug-report\n\n#{err.stack}" if notify alert "4chan X error: #{err.message}\nhttp://mayhemydg.github.com/4chan-x/#bug-report\n\n#{err.stack}" if notify
return return
@ -3262,11 +3264,11 @@ Main =
nodes = [] nodes = []
for mutation in mutations for mutation in mutations
for addedNode in mutation.addedNodes for addedNode in mutation.addedNodes
nodes.push addedNode if addedNode.nodeName is 'TABLE' nodes.push Main.preParse addedNode if addedNode.nodeName is 'TABLE'
Main.node nodes if nodes.length Main.node nodes if posts.length
listener: (e) -> listener: (e) ->
{target} = e {target} = e
Main.node [target] if target.nodeName is 'TABLE' Main.node [Main.preParse target] if target.nodeName is 'TABLE'
css: ' css: '
/* dialog styling */ /* dialog styling */