From 03fdfa8c682dbd931fb175b08992873ad19c2fa0 Mon Sep 17 00:00:00 2001 From: Nicolas Stepien Date: Sat, 16 Feb 2013 16:42:15 +0100 Subject: [PATCH] Add Thread Excerpt. --- 4chan_x.user.js | 26 ++++++++++++++++++++++++-- src/config.coffee | 2 +- src/features.coffee | 17 +++++++++++++++++ src/main.coffee | 1 + 4 files changed, 43 insertions(+), 3 deletions(-) diff --git a/4chan_x.user.js b/4chan_x.user.js index 963c003e3..b3253a578 100644 --- a/4chan_x.user.js +++ b/4chan_x.user.js @@ -43,7 +43,7 @@ */ (function() { - var $, $$, Anonymize, ArchiveLink, AutoGIF, Board, Build, Clone, Conf, Config, DeleteLink, DownloadLink, FileInfo, Filter, Get, Header, ImageExpand, ImageHover, Main, Menu, Notification, Polyfill, Post, QR, QuoteBacklink, QuoteCT, QuoteInline, QuoteOP, QuotePreview, Quotify, Recursive, Redirect, RelativeDates, ReplyHiding, ReportLink, RevealSpoilers, Sauce, Settings, Thread, ThreadHiding, ThreadStats, ThreadUpdater, Time, UI, d, doc, g, + var $, $$, Anonymize, ArchiveLink, AutoGIF, Board, Build, Clone, Conf, Config, DeleteLink, DownloadLink, FileInfo, Filter, Get, Header, ImageExpand, ImageHover, Main, Menu, Notification, Polyfill, Post, QR, QuoteBacklink, QuoteCT, QuoteInline, QuoteOP, QuotePreview, Quotify, Recursive, Redirect, RelativeDates, ReplyHiding, ReportLink, RevealSpoilers, Sauce, Settings, Thread, ThreadExcerpt, ThreadHiding, ThreadStats, ThreadUpdater, Time, UI, d, doc, g, __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }, __hasProp = {}.hasOwnProperty, __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; @@ -90,7 +90,7 @@ 'Thread Updater': [true, 'Fetch and insert new replies. Has more options in its own dialog.'], 'Unread Count': [true, 'Show the unread posts count in the tab title.'], 'Unread Favicon': [true, 'Show a different favicon when there are unread posts.'], - 'Post in Title': [true, 'Show the thread\'s subject in the tab title.'], + 'Thread Excerpt': [true, 'Show an excerpt of the thread in the tab title.'], 'Thread Stats': [true, 'Display reply and image count.'], 'Thread Watcher': [true, 'Bookmark threads.'], 'Auto Watch': [true, 'Automatically watch threads that you start.'], @@ -2533,6 +2533,12 @@ }; Get = { + threadExcerpt: function(thread) { + var excerpt, op, _ref; + op = thread.posts[thread]; + excerpt = ((_ref = op.info.subject) != null ? _ref.trim() : void 0) || op.info.comment.replace(/\n+/g, ' // ') || Conf['Anonymize'] && 'Anonymous' || $('.nameBlock', op.nodes.info).textContent.trim(); + return "/" + thread.board + "/ - " + excerpt; + }, postFromRoot: function(root) { var board, index, link, post, postID; link = $('a[title="Highlight this post"]', root); @@ -3886,6 +3892,21 @@ } }; + ThreadExcerpt = { + init: function() { + if (g.VIEW !== 'thread' || !Conf['Thread Excerpt']) { + return; + } + return Thread.prototype.callbacks.push({ + name: 'Thread Excerpt', + cb: this.node + }); + }, + node: function() { + return d.title = Get.threadExcerpt(this); + } + }; + ThreadStats = { init: function() { if (g.VIEW !== 'thread' || !Conf['Thread Stats']) { @@ -5596,6 +5617,7 @@ initFeature('Reveal Spoilers', RevealSpoilers); initFeature('Auto-GIF', AutoGIF); initFeature('Image Hover', ImageHover); + initFeature('Thread Excerpt', ThreadExcerpt); initFeature('Thread Stats', ThreadStats); initFeature('Thread Updater', ThreadUpdater); console.timeEnd('All initializations'); diff --git a/src/config.coffee b/src/config.coffee index a01d81942..56ea5e27b 100644 --- a/src/config.coffee +++ b/src/config.coffee @@ -36,7 +36,7 @@ Config = 'Thread Updater': [true, 'Fetch and insert new replies. Has more options in its own dialog.'] 'Unread Count': [true, 'Show the unread posts count in the tab title.'] 'Unread Favicon': [true, 'Show a different favicon when there are unread posts.'] - 'Post in Title': [true, 'Show the thread\'s subject in the tab title.'] + 'Thread Excerpt': [true, 'Show an excerpt of the thread in the tab title.'] 'Thread Stats': [true, 'Display reply and image count.'] 'Thread Watcher': [true, 'Bookmark threads.'] 'Auto Watch': [true, 'Automatically watch threads that you start.'] diff --git a/src/features.coffee b/src/features.coffee index 449b0c07a..3dd3f6d61 100644 --- a/src/features.coffee +++ b/src/features.coffee @@ -1366,6 +1366,13 @@ Build = container Get = + threadExcerpt: (thread) -> + op = thread.posts[thread] + excerpt = op.info.subject?.trim() or + op.info.comment.replace(/\n+/g, ' // ') or + Conf['Anonymize'] and 'Anonymous' or + $('.nameBlock', op.nodes.info).textContent.trim() + "/#{thread.board}/ - #{excerpt}" postFromRoot: (root) -> link = $ 'a[title="Highlight this post"]', root board = link.pathname.split('/')[1] @@ -2413,6 +2420,16 @@ ImageHover = $.ajax URL, onreadystatechange: (-> clearTimeout timeoutID if @status is 404), type: 'head' +ThreadExcerpt = + init: -> + return if g.VIEW isnt 'thread' or !Conf['Thread Excerpt'] + + Thread::callbacks.push + name: 'Thread Excerpt' + cb: @node + node: -> + d.title = Get.threadExcerpt @ + ThreadStats = init: -> return if g.VIEW isnt 'thread' or !Conf['Thread Stats'] diff --git a/src/main.coffee b/src/main.coffee index a9effae01..59bd8cefe 100644 --- a/src/main.coffee +++ b/src/main.coffee @@ -333,6 +333,7 @@ Main = initFeature 'Reveal Spoilers', RevealSpoilers initFeature 'Auto-GIF', AutoGIF initFeature 'Image Hover', ImageHover + initFeature 'Thread Excerpt', ThreadExcerpt initFeature 'Thread Stats', ThreadStats initFeature 'Thread Updater', ThreadUpdater console.timeEnd 'All initializations'