From c307874b10f42de4379c02f4ac6319defdf8a5f5 Mon Sep 17 00:00:00 2001 From: noface Date: Wed, 5 Dec 2012 18:36:11 +0100 Subject: [PATCH] Add Catalog Links toggle. --- 4chan_x.user.js | 68 ++++++++++++++++++++++++++++++++++++++++++++++++- changelog | 2 ++ script.coffee | 48 ++++++++++++++++++++++++++++++++++ 3 files changed, 117 insertions(+), 1 deletion(-) diff --git a/4chan_x.user.js b/4chan_x.user.js index 2f227f015..f5d838b69 100644 --- a/4chan_x.user.js +++ b/4chan_x.user.js @@ -81,12 +81,13 @@ */ (function() { - var $, $$, Anonymize, ArchiveLink, AutoGif, Build, Conf, Config, DeleteLink, DownloadLink, ExpandComment, ExpandThread, Favicon, FileInfo, Filter, Get, ImageExpand, ImageHover, Keybinds, Main, Menu, Nav, Options, QR, QuoteBacklink, QuoteCT, QuoteInline, QuoteOP, QuotePreview, Quotify, Redirect, ReplyHiding, ReportLink, RevealSpoilers, Sauce, StrikethroughQuotes, ThreadHiding, ThreadStats, Time, TitlePost, UI, Unread, Updater, Watcher, d, g, _base; + var $, $$, Anonymize, ArchiveLink, AutoGif, Build, CatalogLinks, Conf, Config, DeleteLink, DownloadLink, ExpandComment, ExpandThread, Favicon, FileInfo, Filter, Get, ImageExpand, ImageHover, Keybinds, Main, Menu, Nav, Options, QR, QuoteBacklink, QuoteCT, QuoteInline, QuoteOP, QuotePreview, Quotify, Redirect, ReplyHiding, ReportLink, RevealSpoilers, Sauce, StrikethroughQuotes, ThreadHiding, ThreadStats, Time, TitlePost, UI, Unread, Updater, Watcher, d, g, _base; Config = { main: { Enhancing: { 'Disable 4chan\'s extension': [true, 'Avoid conflicts between 4chan X and 4chan\'s inline extension.'], + 'Catalog Links': [true, 'Turn Navigation links into links to each board\'s catalog.'], '404 Redirect': [true, 'Redirect dead threads and images'], 'Keybinds': [true, 'Binds actions to keys'], 'Time Formatting': [true, 'Arbitrarily formatted timestamps, using your local time'], @@ -5155,6 +5156,66 @@ } }; + CatalogLinks = { + init: function() { + var el, i, nav, _i, _len, _ref; + el = $.el('span', { + innerHTML: "[Catalog " + (!g.CATALOG ? 'On' : 'Off') + "]", + id: 'toggleCatalog' + }); + _ref = ['boardNavDesktop', 'boardNavDesktopFoot']; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + nav = _ref[_i]; + $.on(el.firstElementChild, 'click', this.toggle); + $.add($.id(nav), el); + el = $.el('span', { + innerHTML: el.innerHTML, + id: 'toggleCatalogFoot' + }); + } + if ($.get('CatalogIsToggled')) { + i = g.CATALOG ? 0 : 1; + while (i < 2) { + this.toggle(); + i++; + } + return; + } + if (g.CATALOG) { + return this.toggle(); + } + }, + toggle: function() { + var a, el, isDead, nav, split, _i, _len, _ref; + _ref = ['boardNavDesktop', 'boardNavDesktopFoot']; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + nav = _ref[_i]; + a = $.id(nav).firstElementChild; + while (a.href && (split = a.href.split('/'))) { + if (!/^rs|status/.test(split[2])) { + if ((isDead = split[3] === 'f') && g.CATALOG || split[4] === 'catalog') { + a.href = a.href.replace(/catalog$/, ''); + a.title = a.title.replace(/\ -\ Catalog$/, ''); + } else if (!isDead) { + a.href += 'catalog'; + a.title += ' - Catalog'; + } + } + a = a.nextElementSibling; + } + if (/On$/.test((el = a.parentNode.lastChild.firstElementChild).textContent)) { + el.textContent = 'Catalog Off'; + el.title = 'Turn Catalog Links off.'; + $.set('CatalogIsToggled', true); + } else { + el.textContent = 'Catalog On'; + el.title = 'Turn Catalog Links on.'; + $["delete"]('CatalogIsToggled'); + } + } + } + }; + Main = { init: function() { var cutoff, hiddenThreads, id, key, now, path, pathname, settings, temp, timestamp, val, _ref; @@ -5165,6 +5226,8 @@ if (temp === 'res') { g.REPLY = true; g.THREAD_ID = pathname[2]; + } else if (temp === 'catalog') { + g.CATALOG = true; } for (key in Conf) { val = Conf[key]; @@ -5341,6 +5404,9 @@ } } Favicon.init(); + if (Conf['Catalog Links']) { + CatalogLinks.init(); + } if (Conf['Quick Reply']) { QR.init(); } diff --git a/changelog b/changelog index c6d68cd70..7ade900b8 100644 --- a/changelog +++ b/changelog @@ -1,4 +1,6 @@ master +- noface + Add Catalog Links toggle. 2.36.3 - Mayhem diff --git a/script.coffee b/script.coffee index 39b22d211..7127af1f3 100644 --- a/script.coffee +++ b/script.coffee @@ -2,6 +2,7 @@ Config = main: Enhancing: 'Disable 4chan\'s extension': [true, 'Avoid conflicts between 4chan X and 4chan\'s inline extension.'] + 'Catalog Links': [true, 'Turn Navigation links into links to each board\'s catalog.'] '404 Redirect': [true, 'Redirect dead threads and images'] 'Keybinds': [true, 'Binds actions to keys'] 'Time Formatting': [true, 'Arbitrarily formatted timestamps, using your local time'] @@ -4171,6 +4172,48 @@ ImageExpand = resize: -> ImageExpand.style.textContent = ".fitheight img[data-md5] + img {max-height:#{d.documentElement.clientHeight}px;}" +CatalogLinks = + init: -> + el = $.el 'span', + innerHTML: + "[Catalog #{unless g.CATALOG then 'On' else 'Off'}]" + id: 'toggleCatalog' + for nav in ['boardNavDesktop', 'boardNavDesktopFoot'] + $.on el.firstElementChild, 'click', @toggle + $.add $.id(nav), el + el = $.el 'span', innerHTML: el.innerHTML, id: 'toggleCatalogFoot' + + if $.get 'CatalogIsToggled' + i = if g.CATALOG then 0 else 1 + while i < 2 + @toggle() + i++ + return + @toggle() if g.CATALOG + + toggle: -> + for nav in ['boardNavDesktop', 'boardNavDesktopFoot'] + a = $.id(nav).firstElementChild + while a.href and split = a.href.split '/' + unless /^rs|status/.test split[2] + if (isDead = split[3] is 'f') and g.CATALOG or split[4] is 'catalog' + a.href = a.href.replace /catalog$/, '' + a.title = a.title.replace /\ -\ Catalog$/, '' + else if not isDead + a.href += 'catalog' + a.title += ' - Catalog' + a = a.nextElementSibling + + if /On$/.test (el = a.parentNode.lastChild.firstElementChild).textContent + el.textContent = 'Catalog Off' + el.title = 'Turn Catalog Links off.' + $.set 'CatalogIsToggled', true + else + el.textContent = 'Catalog On' + el.title = 'Turn Catalog Links on.' + $.delete 'CatalogIsToggled' + return + Main = init: -> Main.flatten null, Config @@ -4181,6 +4224,8 @@ Main = if temp is 'res' g.REPLY = true g.THREAD_ID = pathname[2] + else if temp is 'catalog' + g.CATALOG = true # Load values from localStorage. for key, val of Conf @@ -4335,6 +4380,9 @@ Main = $.addClass a, 'current' Favicon.init() + if Conf['Catalog Links'] + CatalogLinks.init() + # Major features. if Conf['Quick Reply'] QR.init()