Use CrossOrigin.json for fetching archive list.

This commit is contained in:
ccd0 2018-09-18 00:49:23 -07:00
parent 898816d211
commit 680fc3384f
3 changed files with 19 additions and 13 deletions

View File

@ -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

View File

@ -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;

View File

@ -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