From 680fc3384fb67c32947e74601555339981226197 Mon Sep 17 00:00:00 2001 From: ccd0 Date: Tue, 18 Sep 2018 00:49:23 -0700 Subject: [PATCH] Use CrossOrigin.json for fetching archive list. --- src/Archive/Redirect.coffee | 24 ++++++++++++------------ src/meta/metadata.js | 1 + src/platform/CrossOrigin.coffee | 7 ++++++- 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/Archive/Redirect.coffee b/src/Archive/Redirect.coffee index 92c7bf465..03d20cfab 100644 --- a/src/Archive/Redirect.coffee +++ b/src/Archive/Redirect.coffee @@ -46,13 +46,12 @@ Redirect = url = url.trim() urls.push url if url + fail = (url, action, msg) -> + new Notice 'warning', "Error #{action} archive data from\n#{url}\n#{msg}", 20 + load = (i) -> -> - fail = (action, msg) -> new Notice 'warning', "Error #{action} archive data from\n#{urls[i]}\n#{msg}", 20 - return fail 'fetching', (if @status then "Error #{@statusText} (#{@status})" else 'Connection Error') unless @status is 200 - try - response = JSON.parse @response - catch err - return fail 'parsing', err.message + return fail urls[i], 'fetching', (if @status then "Error #{@statusText} (#{@status})" else 'Connection Error') unless @status is 200 + {response} = @ response = [response] unless response instanceof Array responses[i] = response nloaded++ @@ -62,13 +61,14 @@ Redirect = if urls.length for url, i in urls if url[0] in ['[', '{'] - load(i).call - status: 200 - response: url + try + response = JSON.parse url + catch err + fail url, 'parsing', err.message + continue + load(i).call {status: 200, response} else - $.ajax url, - responseType: 'text' - onloadend: load(i) + CrossOrigin.json url, load(i), true else Redirect.parse [], cb return diff --git a/src/meta/metadata.js b/src/meta/metadata.js index 81f9e8481..22cf4596b 100644 --- a/src/meta/metadata.js +++ b/src/meta/metadata.js @@ -36,6 +36,7 @@ %> // @connect 4chan.org // @connect 4cdn.org +// @connect mayhemydg.github.io <%= readJSON('/src/Archive/archives.json').map(function(archive) { return '// @connect ' + archive.domain; diff --git a/src/platform/CrossOrigin.coffee b/src/platform/CrossOrigin.coffee index e4390c8a6..b817a168d 100644 --- a/src/platform/CrossOrigin.coffee +++ b/src/platform/CrossOrigin.coffee @@ -86,6 +86,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 `{}`. + # If `bypassCache` is true, ignores previously cached results. json: do -> callbacks = {} results = {} @@ -99,7 +100,7 @@ CrossOrigin = $.queueTask -> cb.call {} delete callbacks[url] - (url, cb) -> + (url, cb, bypassCache) -> <% if (type === 'crx') { %> plainAJAX = (/^https:\/\//.test(url) or location.protocol is 'http:') <% } %> @@ -107,12 +108,16 @@ CrossOrigin = plainAJAX = not (GM?.xmlHttpRequest? or GM_xmlhttpRequest?) <% } %> if plainAJAX + if bypassCache + $.cleanCache (url2) -> url2 is url if (req = $.cache url, cb, responseType: 'json') $.on req, 'abort error', -> cb.call({}) else cb.call {} return + if bypassCache + delete results[url] if results[url] cb.call results[url] return