From 41d6c922a43ce3075ce6363b381b7c25bc3d50dd Mon Sep 17 00:00:00 2001 From: ccd0 Date: Sat, 14 Mar 2015 22:37:50 -0700 Subject: [PATCH] Make code highlighting more robust against race conditions. #357 --- src/Miscellaneous/Fourchan.coffee | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/Miscellaneous/Fourchan.coffee b/src/Miscellaneous/Fourchan.coffee index 098ce1d16..a2cfc9fbf 100755 --- a/src/Miscellaneous/Fourchan.coffee +++ b/src/Miscellaneous/Fourchan.coffee @@ -3,10 +3,20 @@ Fourchan = return unless g.VIEW in ['index', 'thread'] if g.BOARD.ID is 'g' + $.on window, 'prettyprint:cb', (e) -> + return unless post = g.posts[e.detail.ID] + return unless pre = $$('.prettyprint', post.nodes.comment)[e.detail.i] + unless $.hasClass pre, 'prettyprinted' + pre.innerHTML = e.detail.html + $.addClass pre, 'prettyprinted' $.globalEval ''' window.addEventListener('prettyprint', function(e) { window.dispatchEvent(new CustomEvent('prettyprint:cb', { - detail: prettyPrintOne(e.detail) + detail: { + ID: e.detail.ID, + i: e.detail.i, + html: prettyPrintOne(e.detail.html) + } })); }, false); ''' @@ -50,13 +60,8 @@ Fourchan = code: -> return if @isClone - apply = (e) -> - pre.innerHTML = e.detail - $.addClass pre, 'prettyprinted' - $.on window, 'prettyprint:cb', apply - for pre in $$ '.prettyprint:not(.prettyprinted)', @nodes.comment - $.event 'prettyprint', pre.innerHTML, window - $.off window, 'prettyprint:cb', apply + for pre, i in $$('.prettyprint', @nodes.comment) when not $.hasClass(pre, 'prettyprinted') + $.event 'prettyprint', {ID: @fullID, i: i, html: pre.innerHTML}, window return math: ->