From 81b6d97c59423b0fbb1af73384f4136689687d2a Mon Sep 17 00:00:00 2001 From: Nicolas Stepien Date: Sun, 27 May 2012 17:30:14 +0200 Subject: [PATCH] More efficient prettifying. --- 4chan_x.user.js | 26 +++++++++++++++++++------- script.coffee | 18 ++++++++++++------ 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/4chan_x.user.js b/4chan_x.user.js index 843282890..c2e754f12 100644 --- a/4chan_x.user.js +++ b/4chan_x.user.js @@ -747,6 +747,7 @@ } quote.href = "res/" + href; } + Main.prettify(node); post = { el: node, threadId: threadID, @@ -768,8 +769,7 @@ if (Conf['Indicate Cross-thread Quotes']) { QuoteCT.node(post); } - $.replace(a.parentNode.parentNode, node); - return Main.prettify(); + return $.replace(a.parentNode.parentNode, node); } }; @@ -3258,7 +3258,7 @@ return $.off(this, 'mouseout click', QuotePreview.mouseout); }, parse: function(req, id) { - var doc, fileInfo, img, node, post, qp; + var bq, doc, fileInfo, img, node, post, qp; if (!((qp = UI.el) && qp.textContent === ("Loading " + id + "..."))) { return; } @@ -3270,7 +3270,9 @@ doc.documentElement.innerHTML = req.response; node = doc.getElementById("p" + id); qp.innerHTML = node.innerHTML; - Main.prettify(); + bq = $('blockquote', qp); + bq.id += '_qp'; + Main.prettify(bq); post = { el: qp }; @@ -4125,6 +4127,7 @@ threadId: g.THREAD_ID || $.x('ancestor::div[parent::div[@class="board"]]', node).id.slice(1), isInlined: /\binline\b/.test(rootClass), isCrosspost: /\bcrosspost\b/.test(rootClass), + blockquote: el.lastElementChild, quotes: el.getElementsByClassName('quotelink'), backlinks: el.getElementsByClassName('backlink'), fileInfo: false, @@ -4137,6 +4140,7 @@ post.img = img; } } + Main.prettify(post.blockquote); return post; }, node: function(nodes, notify) { @@ -4155,7 +4159,6 @@ } } } - Main.prettify(); }, observer: function(mutations) { var addedNode, mutation, nodes, _i, _j, _len, _len1, _ref; @@ -4181,11 +4184,20 @@ return Main.node([Main.preParse(target)]); } }, - prettify: function() { + prettify: function(bq) { + var code; if (!Main.hasCodeTags) { return; } - return $.globalEval('window.prettyPrint()'); + code = function() { + var pre, _i, _len, _ref; + _ref = document.getElementById('_id_').getElementsByClassName('prettyprint'); + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + pre = _ref[_i]; + pre.innerHTML = prettyPrintOne(pre.innerHTML.replace(/\s/g, ' ')); + } + }; + return $.globalEval(("(" + code + ")()").replace('_id_', bq.id)); }, namespace: '4chan_x.', version: '2.30.4', diff --git a/script.coffee b/script.coffee index 2c0411f8c..3b3ff6103 100644 --- a/script.coffee +++ b/script.coffee @@ -576,6 +576,7 @@ ExpandComment = href = quote.getAttribute 'href' continue if href[0] is '/' # Cross-board quote quote.href = "res/#{href}" # Fix pathnames + Main.prettify node post = el: node threadId: threadID @@ -592,7 +593,6 @@ ExpandComment = if Conf['Indicate Cross-thread Quotes'] QuoteCT.node post $.replace a.parentNode.parentNode, node - Main.prettify() ExpandThread = init: -> @@ -2478,7 +2478,9 @@ QuotePreview = node = doc.getElementById "p#{id}" qp.innerHTML = node.innerHTML - Main.prettify() + bq = $ 'blockquote', qp + bq.id += '_qp' + Main.prettify bq post = el: qp if fileInfo = $ '.fileInfo', qp @@ -3140,6 +3142,7 @@ Main = threadId: g.THREAD_ID or $.x('ancestor::div[parent::div[@class="board"]]', node).id[1..] isInlined: /\binline\b/.test rootClass isCrosspost: /\bcrosspost\b/.test rootClass + blockquote: el.lastElementChild quotes: el.getElementsByClassName 'quotelink' backlinks: el.getElementsByClassName 'backlink' fileInfo: false @@ -3149,6 +3152,7 @@ Main = if img.alt isnt 'File deleted.' post.fileInfo = fileInfo post.img = img + Main.prettify post.blockquote post node: (nodes, notify) -> for callback in Main.callbacks @@ -3156,7 +3160,6 @@ Main = callback node for node in nodes catch err alert "4chan X (#{Main.version}) error: #{err.message}\nhttp://mayhemydg.github.com/4chan-x/#bug-report\n\n#{err.stack}" if notify - Main.prettify() return observer: (mutations) -> nodes = [] @@ -3170,10 +3173,13 @@ Main = if /\bpostContainer\b/.test target.className Main.node [Main.preParse target] - prettify: -> + prettify: (bq) -> return unless Main.hasCodeTags - # window.prettyPrintOne(html) does not preserve indentation - $.globalEval 'window.prettyPrint()' + code = -> + for pre in document.getElementById('_id_').getElementsByClassName 'prettyprint' + pre.innerHTML = prettyPrintOne pre.innerHTML.replace /\s/g, ' ' + return + $.globalEval "(#{code})()".replace '_id_', bq.id namespace: '4chan_x.' version: '2.30.4'