From 29add2ff1988528a77f1c67446b6f07703f1e9cb Mon Sep 17 00:00:00 2001 From: Nicolas Stepien Date: Sat, 1 Sep 2012 00:51:07 +0200 Subject: [PATCH] Add most of Quote Backlinking. --- 4chan_x.user.js | 56 +++++++++++++++++++++++++++++++++++++++++++-- script.coffee | 60 +++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 112 insertions(+), 4 deletions(-) diff --git a/4chan_x.user.js b/4chan_x.user.js index b05ad01b0..522a3ef46 100644 --- a/4chan_x.user.js +++ b/4chan_x.user.js @@ -73,7 +73,7 @@ */ (function() { - var $, $$, Board, Conf, Config, Main, Post, Quotify, Thread, Time, UI, d, g; + var $, $$, Board, Conf, Config, Main, Post, QuoteBacklink, Quotify, Thread, Time, UI, d, g; Config = { main: { @@ -825,6 +825,13 @@ $.log(err, 'Resurrect Quotes'); } } + if (Conf['Quote Backlinks']) { + try { + QuoteBacklink.init(); + } catch (err) { + $.log(err, 'Quote Backlinks'); + } + } if (Conf['Time Formatting']) { try { Time.init(); @@ -927,7 +934,6 @@ textContent: quote }); this.nodes.quotelinks.push(a); - $.log(this.nodes.quotelinks, this.quotes); nodes.push(a); data = data.slice(index + quote.length); } @@ -939,6 +945,52 @@ } }; + QuoteBacklink = { + init: function() { + var format; + format = Conf['backlink'].replace(/%id/g, "' + id + '"); + this.funk = Function('id', "return '" + format + "'"); + this.containers = {}; + Post.prototype.callbacks.push({ + name: 'Quote Backlinking Part 1', + cb: this.firstNode + }); + return Post.prototype.callbacks.push({ + name: 'Quote Backlinking Part 2', + cb: this.secondNode + }); + }, + firstNode: function() { + var a, link, quote, _i, _len, _ref; + if (!this.quotes.length) { + return; + } + a = $.el('a', { + href: "/" + this.board + "/res/" + this.thread + "#p" + this, + className: 'backlink', + textContent: QuoteBacklink.funk(this.ID) + }); + _ref = this.quotes; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + quote = _ref[_i]; + link = a.cloneNode(true); + $.add(QuoteBacklink.getContainer(quote), [$.tn(' '), link]); + } + }, + secondNode: function() { + if (!(Conf['OP Backlinks'] || this.isReply)) { + return; + } + return $.add(this.nodes.info, QuoteBacklink.getContainer("" + this.board + "." + this)); + }, + getContainer: function(id) { + var _base; + return (_base = this.containers)[id] || (_base[id] = $.el('span', { + className: 'container' + })); + } + }; + Time = { init: function() { this.funk = this.createFunc(); diff --git a/script.coffee b/script.coffee index bdcbf1a3e..96749de8d 100644 --- a/script.coffee +++ b/script.coffee @@ -661,6 +661,13 @@ Main = # XXX handle error $.log err, 'Resurrect Quotes' + if Conf['Quote Backlinks'] + try + QuoteBacklink.init() + catch err + # XXX handle error + $.log err, 'Quote Backlinks' + if Conf['Time Formatting'] try Time.init() @@ -770,7 +777,7 @@ Quotify = name: 'Resurrect Quotes' cb: @node node: -> - # return if post.isInlined and not post.isCrosspost + # XXX return if post.isInlined and not post.isCrosspost # XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE is 6 # Get all the text nodes that are not inside an anchor. @@ -825,7 +832,6 @@ Quotify = # a.setAttribute 'data-id', ID @nodes.quotelinks.push a - $.log @nodes.quotelinks, @quotes nodes.push a data = data[index + quote.length..] @@ -836,6 +842,56 @@ Quotify = $.replace node, nodes return +QuoteBacklink = + # Backlinks appending need to work for: + # - previous, same, and following posts. + # - existing and yet-to-exist posts. + # - newly fetched posts. + # - in copies. + # XXX what about order for fetched posts? + # XXX need to work on post copying first before appending inside copies too + # + # First callback creates backlinks and add them to relevant containers. + # Second callback adds relevant containers into posts. + # This is is so that fetched posts can get their backlinks, + # and that as much backlinks are appended in the background as possible. + init: -> + format = Conf['backlink'].replace /%id/g, "' + id + '" + @funk = Function 'id', "return '#{format}'" + @containers = {} + Post::callbacks.push + name: 'Quote Backlinking Part 1' + cb: @firstNode + Post::callbacks.push + name: 'Quote Backlinking Part 2' + cb: @secondNode + firstNode: -> + # XXX return if post.isInlined + return unless @quotes.length + a = $.el 'a', + href: "/#{@board}/res/#{@thread}#p#{@}" + # XXX className: if post.el.hidden then 'filtered backlink' else 'backlink' + className: 'backlink' + textContent: QuoteBacklink.funk @ID + for quote in @quotes + link = a.cloneNode true + # XXX + # if Conf['Quote Preview'] + # $.on link, 'mouseover', QuotePreview.mouseover + # if Conf['Quote Inline'] + # $.on link, 'click', QuoteInline.toggle + # else + # link.setAttribute 'onclick', "replyhl('#{post.ID}');" + $.add QuoteBacklink.getContainer(quote), [$.tn(' '), link] + return + secondNode: -> + # Don't backlink the OP. + return unless Conf['OP Backlinks'] or @isReply + $.add @nodes.info, QuoteBacklink.getContainer "#{@board}.#{@}" + getContainer: (id) -> + @containers[id] or= + $.el 'span', className: 'container' + Time = init: -> @funk = @createFunc()