From c182e680a98314d5e144fd026c650e336000e806 Mon Sep 17 00:00:00 2001 From: Nicolas Stepien Date: Wed, 7 Mar 2012 03:06:47 +0100 Subject: [PATCH] Start working on quote resurrection. See #253. --- 4chan_x.user.js | 38 +++++++++++++++++++++++++++++++++----- script.coffee | 39 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 70 insertions(+), 7 deletions(-) diff --git a/4chan_x.user.js b/4chan_x.user.js index 27179305b..6c432454c 100644 --- a/4chan_x.user.js +++ b/4chan_x.user.js @@ -73,7 +73,7 @@ */ (function() { - var $, $$, Anonymize, AutoGif, DAY, ExpandComment, ExpandThread, Favicon, FileInfo, Filter, GetTitle, HOUR, ImageExpand, ImageHover, Keybinds, MINUTE, Main, NAMESPACE, Nav, Options, QuoteBacklink, QuoteCT, QuoteInline, QuoteOP, QuotePreview, Redirect, ReplyHiding, ReportButton, RevealSpoilers, SECOND, Sauce, StrikethroughQuotes, ThreadHiding, ThreadStats, Threading, Time, TitlePost, Unread, Updater, VERSION, Watcher, conf, config, d, engine, flatten, g, key, log, qr, ui, val, _base; + var $, $$, Anonymize, AutoGif, DAY, DeadQuotes, ExpandComment, ExpandThread, Favicon, FileInfo, Filter, GetTitle, HOUR, ImageExpand, ImageHover, Keybinds, MINUTE, Main, NAMESPACE, Nav, Options, QuoteBacklink, QuoteCT, QuoteInline, QuoteOP, QuotePreview, Redirect, ReplyHiding, ReportButton, RevealSpoilers, SECOND, Sauce, StrikethroughQuotes, ThreadHiding, ThreadStats, Threading, Time, TitlePost, Unread, Updater, VERSION, Watcher, conf, config, d, engine, flatten, g, key, log, qr, ui, val, _base; config = { main: { @@ -132,6 +132,7 @@ 'Quote Highlighting': [true, 'Highlight the previewed post'], 'Quote Inline': [true, 'Show quoted post inline on quote click'], 'Quote Preview': [true, 'Show quote content on hover'], + 'Resurrect Quotes': [true, 'Bring dead links back to life'], 'Indicate OP quote': [true, 'Add \'(OP)\' to OP quotes'], 'Indicate Cross-thread Quotes': [true, 'Add \'(Cross-thread)\' to cross-threads quotes'], 'Forward Hiding': [true, 'Hide original posts of inlined backlinks'] @@ -402,9 +403,6 @@ if (root == null) root = d.body; return d.evaluate(path, root, null, 8, null).singleNodeValue; }, - replace: function(root, el) { - return root.parentNode.replaceChild(el, root); - }, addClass: function(el, className) { return el.classList.add(className); }, @@ -439,6 +437,9 @@ before: function(root, el) { return root.parentNode.insertBefore($.nodes(el), root); }, + replace: function(root, el) { + return root.parentNode.replaceChild($.nodes(el), root); + }, el: function(tag, properties) { var el; el = d.createElement(tag); @@ -776,7 +777,8 @@ if (conf['Quote Preview']) QuotePreview.node(post); if (conf['Quote Inline']) QuoteInline.node(post); if (conf['Indicate OP quote']) QuoteOP.node(post); - if (conf['Indicate Cross-thread Quotes']) return QuoteCT.node(post); + if (conf['Indicate Cross-thread Quotes']) QuoteCT.node(post); + if (conf['Resurrect Quotes']) return DeadQuotes.node(post); } }; @@ -3417,6 +3419,31 @@ } }; + DeadQuotes = { + init: function() { + return g.callbacks.push(this.node); + }, + node: function(post) { + var data, i, index, node, nodes, quote, snapshot, text, _ref; + if (post["class"] === 'inline') return; + snapshot = d.evaluate('.//*[not(self::a) and contains(text(),">>")]/text()', post.el.lastChild, null, 7, null); + for (i = 0, _ref = snapshot.snapshotLength; 0 <= _ref ? i < _ref : i > _ref; 0 <= _ref ? i++ : i--) { + node = snapshot.snapshotItem(i); + data = node.data; + if (!(quote = data.match(/>>(\d+)/))) continue; + index = data.indexOf(quote[0]); + nodes = []; + if (text = data.slice(0, index)) nodes.push($.tn(text)); + nodes.push($.el('a', { + textContent: "" + quote[0] + "\u00A0(Dead)", + href: "#" + quote[1] + })); + if (text = data.slice(index + quote[0].length)) nodes.push($.tn(text)); + $.replace(node, nodes); + } + } + }; + ReportButton = { init: function() { this.a = $.el('a', { @@ -3913,6 +3940,7 @@ if (conf['Quote Backlinks']) QuoteBacklink.init(); if (conf['Indicate OP quote']) QuoteOP.init(); if (conf['Indicate Cross-thread Quotes']) QuoteCT.init(); + if (conf['Resurrect Quotes']) DeadQuotes.init(); return $.ready(Main.ready); }, ready: function() { diff --git a/script.coffee b/script.coffee index 3833522cd..c79f21e07 100644 --- a/script.coffee +++ b/script.coffee @@ -50,6 +50,7 @@ config = 'Quote Highlighting': [true, 'Highlight the previewed post'] 'Quote Inline': [true, 'Show quoted post inline on quote click'] 'Quote Preview': [true, 'Show quote content on hover'] + 'Resurrect Quotes': [true, 'Bring dead links back to life'] 'Indicate OP quote': [true, 'Add \'(OP)\' to OP quotes'] 'Indicate Cross-thread Quotes': [true, 'Add \'(Cross-thread)\' to cross-threads quotes'] 'Forward Hiding': [true, 'Hide original posts of inlined backlinks'] @@ -324,8 +325,6 @@ $.extend $, # XPathResult.ANY_UNORDERED_NODE_TYPE is 8 d.evaluate(path, root, null, 8, null). singleNodeValue - replace: (root, el) -> - root.parentNode.replaceChild el, root addClass: (el, className) -> el.classList.add className removeClass: (el, className) -> @@ -349,6 +348,8 @@ $.extend $, root.parentNode.insertBefore $.nodes(el), root.nextSibling before: (root, el) -> root.parentNode.insertBefore $.nodes(el), root + replace: (root, el) -> + root.parentNode.replaceChild $.nodes(el), root el: (tag, properties) -> el = d.createElement tag $.extend el, properties if properties @@ -669,6 +670,8 @@ ExpandComment = QuoteOP.node post if conf['Indicate Cross-thread Quotes'] QuoteCT.node post + if conf['Resurrect Quotes'] + DeadQuotes.node post ExpandThread = init: -> @@ -2806,6 +2809,35 @@ QuoteCT = $.add quote, $.tn '\u00A0(Cross-thread)' return +DeadQuotes = + init: -> + g.callbacks.push @node + node: (post) -> + return if post.class is 'inline' + # XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE is 7 + + # We need to make sure we can quotify multiple links per text node + # We need to make sure that we don't try to quotify text like `>>text` + + snapshot = d.evaluate './/*[not(self::a) and contains(text(),">>")]/text()', post.el.lastChild, null, 7, null + for i in [0...snapshot.snapshotLength] + node = snapshot.snapshotItem i + data = node.data + unless quote = data.match />>(\d+)/ + continue + index = data.indexOf quote[0] + nodes = [] + if text = data[0...index] + nodes.push $.tn text + nodes.push $.el 'a', + # \u00A0 is nbsp + textContent: "#{quote[0]}\u00A0(Dead)" + href: "##{quote[1]}" # Here be archive link + if text = data[index + quote[0].length...] + nodes.push $.tn text + $.replace node, nodes + return + ReportButton = init: -> @a = $.el 'a', @@ -3230,6 +3262,9 @@ Main = if conf['Indicate Cross-thread Quotes'] QuoteCT.init() + if conf['Resurrect Quotes'] + DeadQuotes.init() + $.ready Main.ready ready: ->