From b059bb7fd1b2d14affe312f628f5b12d7ad4ce68 Mon Sep 17 00:00:00 2001 From: Nicolas Stepien Date: Sun, 27 May 2012 16:22:15 +0200 Subject: [PATCH] Fix #506 --- 4chan_x.user.js | 30 ++++++++++++++++++++++-------- changelog | 1 + script.coffee | 19 +++++++++++++++---- 3 files changed, 38 insertions(+), 12 deletions(-) diff --git a/4chan_x.user.js b/4chan_x.user.js index 1fcc171d9..3a99b4a0a 100644 --- a/4chan_x.user.js +++ b/4chan_x.user.js @@ -449,6 +449,14 @@ }, open: function(url) { return (GM_openInTab || window.open)(location.protocol + url, '_blank'); + }, + globalEval: function(code) { + var script; + script = $.el('script', { + textContent: code + }); + $.add(d.head, script); + return $.rm(script); } }); @@ -760,7 +768,8 @@ if (Conf['Indicate Cross-thread Quotes']) { QuoteCT.node(post); } - return $.replace(a.parentNode.parentNode, node); + $.replace(a.parentNode.parentNode, node); + return Main.prettify(); } }; @@ -1356,7 +1365,7 @@ return setTimeout(this.asyncInit); }, asyncInit: function() { - var link, script; + var link; if (Conf['Hide Original Post Form']) { link = $.el('h1', { innerHTML: "" + (g.REPLY ? 'Quick Reply' : 'New Thread') + "" @@ -1370,11 +1379,7 @@ }); $.before($.id('postForm'), link); } - script = $.el('script', { - textContent: 'Recaptcha.focus_response_field=function(){}' - }); - $.add(d.head, script); - $.rm(script); + $.globalEval('Recaptcha.focus_response_field=function(){}'); if (Conf['Persistent QR']) { QR.dialog(); if (Conf['Auto Hide QR']) { @@ -3282,8 +3287,9 @@ Time.node(post); } if (Conf['File Info Formatting']) { - return FileInfo.node(post); + FileInfo.node(post); } + return Main.prettify(); } }; @@ -4060,6 +4066,7 @@ }); } } + Main.hasCodeTags = !!$('script[src="//static.4chan.org/js/prettify/prettify.js"]'); board = $('.board'); nodes = []; _ref1 = $$('.postContainer', board); @@ -4148,6 +4155,7 @@ } } } + Main.prettify(); }, observer: function(mutations) { var addedNode, mutation, nodes, _i, _j, _len, _len1, _ref; @@ -4173,6 +4181,12 @@ return Main.node([Main.preParse(target)]); } }, + prettify: function() { + if (!Main.hasCodeTags) { + return; + } + return $.globalEval('window.prettyPrint()'); + }, namespace: '4chan_x.', version: '2.30.4', callbacks: [], diff --git a/changelog b/changelog index 11de6b000..0da762354 100644 --- a/changelog +++ b/changelog @@ -1,6 +1,7 @@ master - Mayhem + Fix syntax highlighting in code of fetched posts on /g/. Add quick [code] tags keybind (alt+c). 2.30.4 diff --git a/script.coffee b/script.coffee index 9c876b1df..ae757c7de 100644 --- a/script.coffee +++ b/script.coffee @@ -346,6 +346,10 @@ $.extend $, return open: (url) -> (GM_openInTab or window.open) location.protocol + url, '_blank' + globalEval: (code) -> + script = $.el 'script', textContent: code + $.add d.head, script + $.rm script $.cache.requests = {} @@ -588,6 +592,7 @@ ExpandComment = if Conf['Indicate Cross-thread Quotes'] QuoteCT.node post $.replace a.parentNode.parentNode, node + Main.prettify() ExpandThread = init: -> @@ -1014,10 +1019,7 @@ QR = $.before $.id('postForm'), link # Prevent original captcha input from being focused on reload. - script = $.el 'script', - textContent: 'Recaptcha.focus_response_field=function(){}' - $.add d.head, script - $.rm script + $.globalEval 'Recaptcha.focus_response_field=function(){}' if Conf['Persistent QR'] QR.dialog() @@ -2489,6 +2491,7 @@ QuotePreview = Time.node post if Conf['File Info Formatting'] FileInfo.node post + Main.prettify() QuoteOP = init: -> @@ -3088,6 +3091,8 @@ Main = if Conf['Index Navigation'] setTimeout -> Nav.init() + Main.hasCodeTags = !! $ 'script[src="//static.4chan.org/js/prettify/prettify.js"]' + board = $ '.board' nodes = [] for node in $$ '.postContainer', board @@ -3151,6 +3156,7 @@ 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 = [] @@ -3164,6 +3170,11 @@ Main = if /\bpostContainer\b/.test target.className Main.node [Main.preParse target] + prettify: -> + return unless Main.hasCodeTags + # window.prettyPrintOne(html) does not preserve indentation + $.globalEval 'window.prettyPrint()' + namespace: '4chan_x.' version: '2.30.4' callbacks: []