From 78a79f1942a6344a0e984dc6fbb2d01805ca684a Mon Sep 17 00:00:00 2001 From: ccd0 Date: Sat, 9 Mar 2019 02:13:19 -0800 Subject: [PATCH] Let $.cache report connection errors. --- src/General/Build.Test.coffee | 1 + src/Miscellaneous/ExpandComment.coffee | 2 +- src/Miscellaneous/ExpandThread.coffee | 2 +- src/classes/Fetcher.coffee | 4 +++- src/platform/$.coffee | 9 ++++----- src/platform/CrossOrigin.coffee | 7 ++----- 6 files changed, 12 insertions(+), 13 deletions(-) diff --git a/src/General/Build.Test.coffee b/src/General/Build.Test.coffee index cc015ac11..12945f106 100644 --- a/src/General/Build.Test.coffee +++ b/src/General/Build.Test.coffee @@ -66,6 +66,7 @@ Build.Test = testOne: (post) -> Build.Test.postsRemaining++ $.cache "#{location.protocol}//a.4cdn.org/#{post.board.ID}/thread/#{post.thread.ID}.json", -> + return unless @response {posts} = @response Build.spoilerRange[post.board.ID] = posts[0].custom_spoiler for postData in posts diff --git a/src/Miscellaneous/ExpandComment.coffee b/src/Miscellaneous/ExpandComment.coffee index 09d596774..4adf5eda4 100644 --- a/src/Miscellaneous/ExpandComment.coffee +++ b/src/Miscellaneous/ExpandComment.coffee @@ -38,7 +38,7 @@ ExpandComment = parse: (req, a, post) -> {status} = req unless status in [200, 304] - a.textContent = "Error #{req.statusText} (#{status})" + a.textContent = if status then "Error #{req.statusText} (#{status})" else 'Connection Error' return posts = req.response.posts diff --git a/src/Miscellaneous/ExpandThread.coffee b/src/Miscellaneous/ExpandThread.coffee index d86451371..31bef3bfb 100644 --- a/src/Miscellaneous/ExpandThread.coffee +++ b/src/Miscellaneous/ExpandThread.coffee @@ -89,7 +89,7 @@ ExpandThread = parse: (req, thread, a) -> if req.status not in [200, 304] - a.textContent = "Error #{req.statusText} (#{req.status})" + a.textContent = if req.status then "Error #{req.statusText} (#{req.status})" else 'Connection Error' return Build.spoilerRange[thread.board] = req.response.posts[0].custom_spoiler diff --git a/src/classes/Fetcher.coffee b/src/classes/Fetcher.coffee index b130c36a3..472d1f8de 100644 --- a/src/classes/Fetcher.coffee +++ b/src/classes/Fetcher.coffee @@ -61,12 +61,14 @@ class Fetcher {status} = req unless status is 200 # The thread can die by the time we check a quote. - return if @archivedPost() + return if status and @archivedPost() $.addClass @root, 'warning' @root.textContent = if status is 404 "Thread No.#{@threadID} 404'd." + else if !status + 'Connection Error' else "Error #{req.statusText} (#{req.status})." return diff --git a/src/platform/$.coffee b/src/platform/$.coffee index 078248731..dc19a2e7f 100644 --- a/src/platform/$.coffee +++ b/src/platform/$.coffee @@ -92,12 +92,11 @@ do -> $.queueTask -> cb.call req, {isCached: true} return req options.onloadend = -> - if @status - for cb in @callbacks - do (cb) => $.queueTask => cb.call @, {isCached: false} - delete @callbacks - else + unless @status delete reqs[url] + for cb in @callbacks + do (cb) => $.queueTask => cb.call @, {isCached: false} + delete @callbacks req = $.ajax url, options req.callbacks = [cb] reqs[url] = req diff --git a/src/platform/CrossOrigin.coffee b/src/platform/CrossOrigin.coffee index cb9bc899a..aa73888c5 100644 --- a/src/platform/CrossOrigin.coffee +++ b/src/platform/CrossOrigin.coffee @@ -70,7 +70,7 @@ CrossOrigin = # Attempts to fetch `url` in JSON format using cross-origin privileges, if available. # On success, calls `cb` with a `this` containing properties `status`, `statusText`, `response` and caches result. - # On error/abort, calls `cb` with a `this` of `{}`. + # On error/abort/timeout, calls `cb` with a `this` of `{}` or in some cases an XMLHttpRequest reflecting the error. # If `bypassCache` is true, ignores previously cached results. json: do -> callbacks = {} @@ -90,10 +90,7 @@ CrossOrigin = unless GM?.xmlHttpRequest? or GM_xmlhttpRequest? if bypassCache $.cleanCache (url2) -> url2 is url - if (req = $.cache url, cb) - $.on req, 'abort error', -> cb.call({}) - else - cb.call {} + req = $.cache url, cb return <% } %>