Use CrossOrigin.json for fetching archive list.
This commit is contained in:
parent
898816d211
commit
680fc3384f
@ -46,13 +46,12 @@ Redirect =
|
|||||||
url = url.trim()
|
url = url.trim()
|
||||||
urls.push url if url
|
urls.push url if url
|
||||||
|
|
||||||
|
fail = (url, action, msg) ->
|
||||||
|
new Notice 'warning', "Error #{action} archive data from\n#{url}\n#{msg}", 20
|
||||||
|
|
||||||
load = (i) -> ->
|
load = (i) -> ->
|
||||||
fail = (action, msg) -> new Notice 'warning', "Error #{action} archive data from\n#{urls[i]}\n#{msg}", 20
|
return fail urls[i], 'fetching', (if @status then "Error #{@statusText} (#{@status})" else 'Connection Error') unless @status is 200
|
||||||
return fail 'fetching', (if @status then "Error #{@statusText} (#{@status})" else 'Connection Error') unless @status is 200
|
{response} = @
|
||||||
try
|
|
||||||
response = JSON.parse @response
|
|
||||||
catch err
|
|
||||||
return fail 'parsing', err.message
|
|
||||||
response = [response] unless response instanceof Array
|
response = [response] unless response instanceof Array
|
||||||
responses[i] = response
|
responses[i] = response
|
||||||
nloaded++
|
nloaded++
|
||||||
@ -62,13 +61,14 @@ Redirect =
|
|||||||
if urls.length
|
if urls.length
|
||||||
for url, i in urls
|
for url, i in urls
|
||||||
if url[0] in ['[', '{']
|
if url[0] in ['[', '{']
|
||||||
load(i).call
|
try
|
||||||
status: 200
|
response = JSON.parse url
|
||||||
response: url
|
catch err
|
||||||
|
fail url, 'parsing', err.message
|
||||||
|
continue
|
||||||
|
load(i).call {status: 200, response}
|
||||||
else
|
else
|
||||||
$.ajax url,
|
CrossOrigin.json url, load(i), true
|
||||||
responseType: 'text'
|
|
||||||
onloadend: load(i)
|
|
||||||
else
|
else
|
||||||
Redirect.parse [], cb
|
Redirect.parse [], cb
|
||||||
return
|
return
|
||||||
|
|||||||
@ -36,6 +36,7 @@
|
|||||||
%>
|
%>
|
||||||
// @connect 4chan.org
|
// @connect 4chan.org
|
||||||
// @connect 4cdn.org
|
// @connect 4cdn.org
|
||||||
|
// @connect mayhemydg.github.io
|
||||||
<%=
|
<%=
|
||||||
readJSON('/src/Archive/archives.json').map(function(archive) {
|
readJSON('/src/Archive/archives.json').map(function(archive) {
|
||||||
return '// @connect ' + archive.domain;
|
return '// @connect ' + archive.domain;
|
||||||
|
|||||||
@ -86,6 +86,7 @@ CrossOrigin =
|
|||||||
# Attempts to fetch `url` in JSON format using cross-origin privileges, if available.
|
# 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 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, calls `cb` with a `this` of `{}`.
|
||||||
|
# If `bypassCache` is true, ignores previously cached results.
|
||||||
json: do ->
|
json: do ->
|
||||||
callbacks = {}
|
callbacks = {}
|
||||||
results = {}
|
results = {}
|
||||||
@ -99,7 +100,7 @@ CrossOrigin =
|
|||||||
$.queueTask -> cb.call {}
|
$.queueTask -> cb.call {}
|
||||||
delete callbacks[url]
|
delete callbacks[url]
|
||||||
|
|
||||||
(url, cb) ->
|
(url, cb, bypassCache) ->
|
||||||
<% if (type === 'crx') { %>
|
<% if (type === 'crx') { %>
|
||||||
plainAJAX = (/^https:\/\//.test(url) or location.protocol is 'http:')
|
plainAJAX = (/^https:\/\//.test(url) or location.protocol is 'http:')
|
||||||
<% } %>
|
<% } %>
|
||||||
@ -107,12 +108,16 @@ CrossOrigin =
|
|||||||
plainAJAX = not (GM?.xmlHttpRequest? or GM_xmlhttpRequest?)
|
plainAJAX = not (GM?.xmlHttpRequest? or GM_xmlhttpRequest?)
|
||||||
<% } %>
|
<% } %>
|
||||||
if plainAJAX
|
if plainAJAX
|
||||||
|
if bypassCache
|
||||||
|
$.cleanCache (url2) -> url2 is url
|
||||||
if (req = $.cache url, cb, responseType: 'json')
|
if (req = $.cache url, cb, responseType: 'json')
|
||||||
$.on req, 'abort error', -> cb.call({})
|
$.on req, 'abort error', -> cb.call({})
|
||||||
else
|
else
|
||||||
cb.call {}
|
cb.call {}
|
||||||
return
|
return
|
||||||
|
|
||||||
|
if bypassCache
|
||||||
|
delete results[url]
|
||||||
if results[url]
|
if results[url]
|
||||||
cb.call results[url]
|
cb.call results[url]
|
||||||
return
|
return
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user