From 691395b627ed21b6e2b14dd3bbbe2f0dd30016dd Mon Sep 17 00:00:00 2001 From: Nicolas Stepien Date: Tue, 27 Dec 2011 06:30:11 +0100 Subject: [PATCH] Update foolz archives redirection, close #71. Get #79 done, will need testing and foolz archive's support. --- 4chan_x.user.js | 120 +++++++++++++++++++++++++++--------------------- Cakefile | 1 + changelog | 4 +- script.coffee | 55 ++++++++++++++-------- 4 files changed, 107 insertions(+), 73 deletions(-) diff --git a/4chan_x.user.js b/4chan_x.user.js index d391c0cc8..a0975a19a 100644 --- a/4chan_x.user.js +++ b/4chan_x.user.js @@ -6,6 +6,7 @@ // @copyright 2009-2011 James Campos // @license MIT; http://en.wikipedia.org/wiki/Mit_license // @include http://boards.4chan.org/* +// @include http://images.4chan.org/* // @include http://sys.4chan.org/* // @run-at document-start // @updateURL https://raw.github.com/MayhemYDG/4chan-x/stable/4chan_x.user.js @@ -70,7 +71,7 @@ config = { main: { Enhancing: { - '404 Redirect': [true, 'Redirect dead threads'], + '404 Redirect': [true, 'Redirect dead threads and images'], 'Keybinds': [true, 'Binds actions to keys'], 'Time Formatting': [true, 'Arbitrarily formatted timestamps, using your local time'], 'Report Button': [true, 'Add report buttons'], @@ -2819,49 +2820,61 @@ } }; - redirect = function() { - var url; - switch (g.BOARD) { - case 'a': - case 'jp': - case 'm': - case 'tg': - case 'tv': - url = "http://oldarchive.foolz.us/" + g.BOARD + "/thread/" + g.THREAD_ID; - break; - case 'diy': - case 'g': - case 'sci': - url = "http://archive.installgentoo.net/" + g.BOARD + "/thread/" + g.THREAD_ID; - break; - case '3': - case 'adv': - case 'an': - case 'ck': - case 'co': - case 'fa': - case 'fit': - case 'int': - case 'k': - case 'mu': - case 'n': - case 'o': - case 'p': - case 'po': - case 'pol': - case 'soc': - case 'sp': - case 'toy': - case 'trv': - case 'v': - case 'vp': - case 'x': - url = "http://archive.no-ip.org/" + g.BOARD + "/thread/" + g.THREAD_ID; - break; - default: - url = "http://boards.4chan.org/" + g.BOARD; + redirect = { + init: function() { + var url; + url = location.hostname === 'images.4chan.org' ? redirect.image(g.BOARD, location.pathname.split('/')[3]) : /^\d+$/.test(g.THREAD_ID) ? redirect.thread() : void 0; + if (url) return location.href = url; + }, + image: function(board, filename) { + switch (board) { + case 'a': + case 'jp': + case 'm': + case 'tg': + case 'tv': + return "http://archivethumb.foolz.us/board/" + board + "/img/" + filename; + } + }, + thread: function() { + switch (g.BOARD) { + case 'a': + case 'jp': + case 'm': + case 'tg': + case 'tv': + return "http://archive.foolz.us/" + g.BOARD + "/thread/" + g.THREAD_ID; + case 'diy': + case 'g': + case 'sci': + return "http://archive.installgentoo.net/" + g.BOARD + "/thread/" + g.THREAD_ID; + case '3': + case 'adv': + case 'an': + case 'ck': + case 'co': + case 'fa': + case 'fit': + case 'int': + case 'k': + case 'mu': + case 'n': + case 'o': + case 'p': + case 'po': + case 'pol': + case 'soc': + case 'sp': + case 'toy': + case 'trv': + case 'v': + case 'vp': + case 'x': + return "http://archive.no-ip.org/" + g.BOARD + "/thread/" + g.THREAD_ID; + default: + return "http://boards.4chan.org/" + g.BOARD; + } } - return location.href = url; }; imgHover = { @@ -2973,26 +2986,29 @@ thumb.hidden = false; return $.rm(thumb.nextSibling); }, - expand: function(thumb) { + expand: function(thumb, url) { var a, filesize, img, max; a = thumb.parentNode; img = $.el('img', { - src: a.href + src: url ? url : a.href }); if (engine === 'gecko' && a.parentNode.className !== 'op') { filesize = $.x('preceding-sibling::span[@class="filesize"]', a).textContent; max = filesize.match(/(\d+)x/); img.style.maxWidth = "" + max[1] + "px"; } - $.on(img, 'error', imgExpand.error); + if (conf['404 Redirect']) $.on(img, 'error', imgExpand.error); thumb.hidden = true; return $.add(a, img); }, error: function() { - var req, thumb; + var req, src, thumb, url; thumb = this.previousSibling; imgExpand.contract(thumb); - if (engine === 'webkit') { + src = this.src.split('/'); + if (url = redirect.image(src[3], src[5])) { + return imgExpand.expand(thumb, url); + } else if (engine === 'webkit') { return req = $.ajax(this.src, (function() { if (this.status !== 404) { return setTimeout(imgExpand.retry, 10000, thumb); @@ -3042,7 +3058,7 @@ pathname = location.pathname.slice(1).split('/'); g.BOARD = pathname[0], temp = pathname[1]; if (temp === 'res') { - g.REPLY = temp; + g.REPLY = true; g.THREAD_ID = pathname[2]; } else { g.PAGENUM = parseInt(temp) || 0; @@ -3111,11 +3127,11 @@ onLoad: function() { var callback, canPost, form, node, nodes, _i, _j, _len, _len2, _ref; $.off(d, 'DOMContentLoaded', Main.onLoad); - if (conf['404 Redirect'] && d.title === '4chan - 404' && /^\d+$/.test(g.THREAD_ID)) { - redirect(); + if (conf['404 Redirect'] && d.title === '4chan - 404') { + redirect.init(); return; } - if (!$('#navtopr')) return; + if (!$('#navtopr') || location.hostname === 'images.4chan.org') return; $.addClass(d.body, engine); $.addStyle(Main.css); threading.init(); diff --git a/Cakefile b/Cakefile index 43bb0c87f..195cbb959 100644 --- a/Cakefile +++ b/Cakefile @@ -13,6 +13,7 @@ HEADER = """ // @copyright 2009-2011 James Campos // @license MIT; http://en.wikipedia.org/wiki/Mit_license // @include http://boards.4chan.org/* +// @include http://images.4chan.org/* // @include http://sys.4chan.org/* // @run-at document-start // @updateURL https://raw.github.com/MayhemYDG/4chan-x/stable/4chan_x.user.js diff --git a/changelog b/changelog index 00ded2b35..dc57939e7 100644 --- a/changelog +++ b/changelog @@ -1,7 +1,9 @@ master - mayhem - the report button can open multiple popups + redirect 404'd pictures to archives when possible + the report button can open multiple popups again add omploader to the list of optional flavors (http://ompldr.org/upload?url1=) + update archive redirections 2.23.7 - mayhem diff --git a/script.coffee b/script.coffee index 719ae450d..ab4dfa06b 100644 --- a/script.coffee +++ b/script.coffee @@ -1,7 +1,7 @@ config = main: Enhancing: - '404 Redirect': [true, 'Redirect dead threads'] + '404 Redirect': [true, 'Redirect dead threads and images'] 'Keybinds': [true, 'Binds actions to keys'] 'Time Formatting': [true, 'Arbitrarily formatted timestamps, using your local time'] 'Report Button': [true, 'Add report buttons'] @@ -2216,17 +2216,29 @@ Favicon = favicon.href = null $.replace favicon, clone -redirect = -> - switch g.BOARD - when 'a', 'jp', 'm', 'tg', 'tv' - url = "http://oldarchive.foolz.us/#{g.BOARD}/thread/#{g.THREAD_ID}" - when 'diy', 'g', 'sci' - url = "http://archive.installgentoo.net/#{g.BOARD}/thread/#{g.THREAD_ID}" - when '3', 'adv', 'an', 'ck', 'co', 'fa', 'fit', 'int', 'k', 'mu', 'n', 'o', 'p', 'po', 'pol', 'soc', 'sp', 'toy', 'trv', 'v', 'vp', 'x' - url = "http://archive.no-ip.org/#{g.BOARD}/thread/#{g.THREAD_ID}" - else - url = "http://boards.4chan.org/#{g.BOARD}" - location.href = url +redirect = + init: -> + url = + # waiting for https://github.com/FoOlRulez/FoOlFuuka/issues/11 + if location.hostname is 'images.4chan.org' + redirect.image g.BOARD, location.pathname.split('/')[3] + else if /^\d+$/.test g.THREAD_ID + redirect.thread() + location.href = url if url + image: (board, filename) -> #board must be given, the image can originate from a cross-quote + switch board + when 'a', 'jp', 'm', 'tg', 'tv' + "http://archivethumb.foolz.us/board/#{board}/img/#{filename}" + thread: -> + switch g.BOARD + when 'a', 'jp', 'm', 'tg', 'tv' + "http://archive.foolz.us/#{g.BOARD}/thread/#{g.THREAD_ID}" + when 'diy', 'g', 'sci' + "http://archive.installgentoo.net/#{g.BOARD}/thread/#{g.THREAD_ID}" + when '3', 'adv', 'an', 'ck', 'co', 'fa', 'fit', 'int', 'k', 'mu', 'n', 'o', 'p', 'po', 'pol', 'soc', 'sp', 'toy', 'trv', 'v', 'vp', 'x' + "http://archive.no-ip.org/#{g.BOARD}/thread/#{g.THREAD_ID}" + else + "http://boards.4chan.org/#{g.BOARD}" imgHover = init: -> @@ -2303,23 +2315,26 @@ imgExpand = thumb.hidden = false $.rm thumb.nextSibling - expand: (thumb) -> + expand: (thumb, url) -> a = thumb.parentNode img = $.el 'img', - src: a.href + src: if url then url else a.href if engine is 'gecko' and a.parentNode.className isnt 'op' filesize = $.x('preceding-sibling::span[@class="filesize"]', a).textContent max = filesize.match /(\d+)x/ img.style.maxWidth = "#{max[1]}px" - $.on img, 'error', imgExpand.error + $.on img, 'error', imgExpand.error if conf['404 Redirect'] thumb.hidden = true $.add a, img error: -> thumb = @previousSibling imgExpand.contract thumb + src = @src.split '/' + if url = redirect.image src[3], src[5] + imgExpand.expand thumb, url #navigator.online is not x-browser/os yet - if engine is 'webkit' + else if engine is 'webkit' req = $.ajax @src, (-> setTimeout imgExpand.retry, 10000, thumb if @status isnt 404 ), type: 'head', event: 'onreadystatechange' @@ -2358,7 +2373,7 @@ Main = pathname = location.pathname[1..].split('/') [g.BOARD, temp] = pathname if temp is 'res' - g.REPLY = temp + g.REPLY = true g.THREAD_ID = pathname[2] else g.PAGENUM = parseInt(temp) or 0 @@ -2453,10 +2468,10 @@ Main = onLoad: -> $.off d, 'DOMContentLoaded', Main.onLoad - if conf['404 Redirect'] and d.title is '4chan - 404' and /^\d+$/.test g.THREAD_ID - redirect() + if conf['404 Redirect'] and d.title is '4chan - 404' + redirect.init() return - if not $ '#navtopr' + if not $('#navtopr') or location.hostname is 'images.4chan.org' return $.addClass d.body, engine $.addStyle Main.css