From 0f6a98e5e9ff76af0339f32f614c0281524a5539 Mon Sep 17 00:00:00 2001 From: ccd0 Date: Thu, 5 Jan 2017 19:12:42 -0800 Subject: [PATCH] Use is.4chan.org for building posts if 'Use Faster Image Host' is off. Also consolidate the image host related code. --- src/General/Build.coffee | 6 +++--- src/General/Settings.coffee | 2 +- src/Images/ImageCommon.coffee | 2 +- src/Images/ImageHost.coffee | 16 ++++++++++++---- src/Linkification/Embedding.coffee | 4 ++-- src/Linkification/Linkify.coffee | 4 ++-- src/classes/Fetcher.coffee | 6 +++--- src/classes/Post.coffee | 4 +++- src/main/Main.coffee | 5 +---- src/platform/CrossOrigin.coffee | 2 +- 10 files changed, 29 insertions(+), 22 deletions(-) diff --git a/src/General/Build.coffee b/src/General/Build.coffee index 7e46941b0..d230ce396 100644 --- a/src/General/Build.coffee +++ b/src/General/Build.coffee @@ -64,14 +64,14 @@ Build = o.file = name: (Build.unescape data.filename) + data.ext url: if boardID is 'f' - "#{location.protocol}//i.4cdn.org/#{boardID}/#{encodeURIComponent data.filename}#{data.ext}" + "#{location.protocol}//#{ImageHost.host()}/#{boardID}/#{encodeURIComponent data.filename}#{data.ext}" else - "#{location.protocol}//i.4cdn.org/#{boardID}/#{data.tim}#{data.ext}" + "#{location.protocol}//#{ImageHost.host()}/#{boardID}/#{data.tim}#{data.ext}" height: data.h width: data.w MD5: data.md5 size: $.bytesToString data.fsize - thumbURL: "#{location.protocol}//i.4cdn.org/#{boardID}/#{data.tim}s.jpg" + thumbURL: "#{location.protocol}//#{ImageHost.thumbHost()}/#{boardID}/#{data.tim}s.jpg" theight: data.tn_h twidth: data.tn_w isSpoiler: !!data.spoiler diff --git a/src/General/Settings.coffee b/src/General/Settings.coffee index dcbd94c1f..4d124e1ec 100644 --- a/src/General/Settings.coffee +++ b/src/General/Settings.coffee @@ -634,7 +634,7 @@ Settings = data = isReply: true file: - url: '//i.4cdn.org/g/1334437723720.jpg' + url: "//#{ImageHost.host()}/g/1334437723720.jpg" name: 'd9bb2efc98dd0df141a94399ff5880b7.jpg' size: '276 KB' sizeInBytes: 276 * 1024 diff --git a/src/Images/ImageCommon.coffee b/src/Images/ImageCommon.coffee index 626eb429c..7ec92690e 100644 --- a/src/Images/ImageCommon.coffee +++ b/src/Images/ImageCommon.coffee @@ -34,7 +34,7 @@ ImageCommon = return true isFromArchive: (file) -> - !Main.isImageHost(file.src.split('/')[2]) + !ImageHost.test(file.src.split('/')[2]) error: (file, post, delay, cb) -> src = post.file.url.split '/' diff --git a/src/Images/ImageHost.coffee b/src/Images/ImageHost.coffee index 2bad8f56b..0a4d985a1 100644 --- a/src/Images/ImageHost.coffee +++ b/src/Images/ImageHost.coffee @@ -1,21 +1,29 @@ ImageHost = init: -> - return unless (@enabled = Conf['Use Faster Image Host'] and g.VIEW in ['index', 'thread']) + return unless (@useFaster = Conf['Use Faster Image Host']) and g.VIEW in ['index', 'thread'] Callbacks.Post.push name: 'Image Host Rewriting' cb: @node + host: -> + if @useFaster then 'i.4cdn.org' else 'is.4chan.org' + thumbHost: -> + 'i.4cdn.org' + test: (hostname) -> + hostname is 'i.4cdn.org' or ImageHost.regex.test(hostname) + regex: /^is\d*\.4chan\.org$/ node: -> return if @isClone + host = ImageHost.host() if @file and ImageHost.regex.test(@file.url.split('/')[2]) - @file.link.hostname = 'i.4cdn.org' - @file.thumbLink.hostname = 'i.4cdn.org' if @file.thumbLink + @file.link.hostname = host + @file.thumbLink.hostname = host if @file.thumbLink @file.url = @file.link.href ImageHost.fixLinks $$('a', @nodes.comment) fixLinks: (links) -> for link in links when ImageHost.regex.test(link.hostname) - link.hostname = 'i.4cdn.org' + link.hostname = ImageHost.host() return diff --git a/src/Linkification/Embedding.coffee b/src/Linkification/Embedding.coffee index 867dfb4c4..055ae1767 100644 --- a/src/Linkification/Embedding.coffee +++ b/src/Linkification/Embedding.coffee @@ -229,7 +229,7 @@ Embedding = controls: true preload: 'auto' src: a.dataset.href - loop: Main.isImageHost a.dataset.href.split('/')[2] + loop: ImageHost.test a.dataset.href.split('/')[2] $.on el, 'loadedmetadata', -> if el.videoHeight is 0 and el.parentNode $.replace el, Embedding.types.audio.el(a) @@ -337,7 +337,7 @@ Embedding = when 'pi' then ["https://u.pomf.is/#{base}"] when 'wl' then ["http://webm.land/media/#{base}"] when 'ko' then ["https://kordy.kastden.org/loopvid/#{base}"] - when 'fc' then ["//i.4cdn.org/#{base}.webm"] + when 'fc' then ["//#{ImageHost.host()}/#{base}.webm"] when 'gc' then ["https://#{type}.gfycat.com/#{name}.webm"] for url in urls $.add el, $.el 'source', src: url diff --git a/src/Linkification/Linkify.coffee b/src/Linkification/Linkify.coffee index 68e66f961..ba93fd902 100644 --- a/src/Linkification/Linkify.coffee +++ b/src/Linkification/Linkify.coffee @@ -14,11 +14,11 @@ Linkify = node: -> return Embedding.events @ if @isClone return unless Linkify.regString.test @info.comment - for link in $$ 'a', @nodes.comment when Main.isImageHost link.hostname + for link in $$ 'a', @nodes.comment when ImageHost.test link.hostname $.addClass link, 'linkify' Embedding.process link, @ links = Linkify.process @nodes.comment - ImageHost.fixLinks links if ImageHost.enabled + ImageHost.fixLinks links if ImageHost.useFaster Embedding.process link, @ for link in links return diff --git a/src/classes/Fetcher.coffee b/src/classes/Fetcher.coffee index 29206215d..6364605cb 100644 --- a/src/classes/Fetcher.coffee +++ b/src/classes/Fetcher.coffee @@ -176,14 +176,14 @@ class Fetcher name: data.media.media_filename url: data.media.media_link or data.media.remote_media_link or if @boardID is 'f' - "#{location.protocol}//i.4cdn.org/#{@boardID}/#{encodeURIComponent data.media.media_filename}" + "#{location.protocol}//#{ImageHost.host()}/#{@boardID}/#{encodeURIComponent data.media.media_filename}" else - "#{location.protocol}//i.4cdn.org/#{@boardID}/#{encodeURIComponent data.media.media_orig}" + "#{location.protocol}//#{ImageHost.host()}/#{@boardID}/#{encodeURIComponent data.media.media_orig}" height: data.media.media_h width: data.media.media_w MD5: data.media.media_hash size: $.bytesToString data.media.media_size - thumbURL: data.media.thumb_link or "#{location.protocol}//i.4cdn.org/#{@boardID}/#{data.media.preview_orig}" + thumbURL: data.media.thumb_link or "#{location.protocol}//#{ImageHost.thumbHost()}/#{@boardID}/#{data.media.preview_orig}" theight: data.media.preview_h twidth: data.media.preview_w isSpoiler: data.media.spoiler is '1' diff --git a/src/classes/Post.coffee b/src/classes/Post.coffee index 4419f3500..b143c47e9 100644 --- a/src/classes/Post.coffee +++ b/src/classes/Post.coffee @@ -207,9 +207,11 @@ class Post $.extend @file, thumb: thumb thumbLink: thumb.parentNode - thumbURL: if m = link.href.match(/\d+(?=\.\w+$)/) then "#{location.protocol}//i.4cdn.org/#{@board}/#{m[0]}s.jpg" + thumbURL: thumb.src MD5: thumb.dataset.md5 isSpoiler: $.hasClass thumb.parentNode, 'imgspoiler' + if @file.isSpoiler + @file.thumbURL = if (m = link.href.match /\d+(?=\.\w+$)/) then "#{location.protocol}//#{ImageHost.thumbHost()}/#{@board}/#{m[0]}s.jpg" @deadMark = # \u00A0 is nbsp diff --git a/src/main/Main.coffee b/src/main/Main.coffee index cc34f8edb..d3f5f3240 100644 --- a/src/main/Main.coffee +++ b/src/main/Main.coffee @@ -130,7 +130,7 @@ Main = PostSuccessful.init() return - if Main.isImageHost hostname + if ImageHost.test hostname return unless pathname[2] and not /[sm]\.jpg$/.test(pathname[2]) $.asap (-> d.readyState isnt 'loading'), -> if Conf['404 Redirect'] and d.title in ['4chan - Temporarily Offline', '4chan - 404 Not Found'] @@ -411,9 +411,6 @@ Main = url = "<%= meta.newIssue.replace('%title', '#{encodeURIComponent title}').replace('%details', '#{encodeURIComponent details}') %>" <%= html(' [report]') %> - isImageHost: (hostname) -> - hostname is 'i.4cdn.org' or /^is\d*\.4chan\.org$/.test(hostname) - isThisPageLegit: -> # 404 error page or similar. unless 'thisPageIsLegit' of Main diff --git a/src/platform/CrossOrigin.coffee b/src/platform/CrossOrigin.coffee index aa8973358..fdfa1cf65 100644 --- a/src/platform/CrossOrigin.coffee +++ b/src/platform/CrossOrigin.coffee @@ -15,7 +15,7 @@ CrossOrigin = url = url.replace /^((?:https?:)?\/\/(?:\w+\.)?4c(?:ha|d)n\.org)\/adv\//, '$1//adv/' <% if (type === 'crx') { %> parts = url.split '/' - if parts[0] is location.protocol and parts[1] is '' and Main.isImageHost(parts[2]) + if parts[0] is location.protocol and parts[1] is '' and ImageHost.test(parts[2]) xhr = new XMLHttpRequest() xhr.open 'GET', url, true xhr.setRequestHeader key, value for key, value of headers