Replace CrossOrigin.json with simpler CrossOrigin.ajax and a CrossOrigin.cache making use of $.cache.
This commit is contained in:
parent
78a79f1942
commit
188422f1ae
@ -68,7 +68,8 @@ Redirect =
|
||||
continue
|
||||
load(i).call {status: 200, response}
|
||||
else
|
||||
CrossOrigin.json url, load(i), true
|
||||
CrossOrigin.ajax url,
|
||||
onloadend: load(i)
|
||||
else
|
||||
Redirect.parse [], cb
|
||||
return
|
||||
|
||||
@ -111,7 +111,7 @@ Embedding =
|
||||
if service.queue.length >= service.batchSize
|
||||
Embedding.flushTitles service
|
||||
else
|
||||
CrossOrigin.json service.api(uid), (-> Embedding.cb.title @, data)
|
||||
CrossOrigin.cache service.api(uid), (-> Embedding.cb.title @, data)
|
||||
|
||||
flushTitles: (service) ->
|
||||
{queue} = service
|
||||
@ -120,7 +120,7 @@ Embedding =
|
||||
cb = ->
|
||||
Embedding.cb.title @, data for data in queue
|
||||
return
|
||||
CrossOrigin.json service.api(data.uid for data in queue), cb
|
||||
CrossOrigin.cache service.api(data.uid for data in queue), cb
|
||||
|
||||
preview: (data) ->
|
||||
{key, uid, link} = data
|
||||
@ -275,7 +275,7 @@ Embedding =
|
||||
el = $.el 'pre',
|
||||
hidden: true
|
||||
id: "gist-embed-#{counter++}"
|
||||
CrossOrigin.json "https://api.github.com/gists/#{a.dataset.uid}", ->
|
||||
CrossOrigin.cache "https://api.github.com/gists/#{a.dataset.uid}", ->
|
||||
el.textContent = Object.values(@response.files)[0].content
|
||||
el.className = 'prettyprint'
|
||||
$.global ->
|
||||
|
||||
@ -228,10 +228,11 @@ ThreadWatcher =
|
||||
whenModified: if force then false else 'ThreadWatcher'
|
||||
else
|
||||
req = {abort: () -> req.aborted = true}
|
||||
CrossOrigin.json url, ->
|
||||
return if req.aborted
|
||||
ThreadWatcher.parseStatus.call @, thread
|
||||
, true, $.MINUTE
|
||||
CrossOrigin.ajax url,
|
||||
onloadend: ->
|
||||
return if req.aborted
|
||||
ThreadWatcher.parseStatus.call @, thread
|
||||
timeout: $.MINUTE
|
||||
ThreadWatcher.requests.push req
|
||||
|
||||
parseStatus: ({siteID, boardID, threadID, data}) ->
|
||||
|
||||
@ -111,7 +111,7 @@ class Fetcher
|
||||
encryptionOK = /^https:\/\//.test(url) or location.protocol is 'http:'
|
||||
if encryptionOK or Conf['Exempt Archives from Encryption']
|
||||
that = @
|
||||
CrossOrigin.json url, ->
|
||||
CrossOrigin.cache url, ->
|
||||
if !encryptionOK and @response?.media
|
||||
{media} = @response
|
||||
for key of media when /_link$/.test key
|
||||
|
||||
@ -84,7 +84,7 @@ $.ajax = do ->
|
||||
|
||||
do ->
|
||||
reqs = {}
|
||||
$.cache = (url, cb, options={}) ->
|
||||
$.cache = (url, cb, options={}, extra={}) ->
|
||||
if (req = reqs[url])
|
||||
if req.callbacks
|
||||
req.callbacks.push cb
|
||||
@ -97,7 +97,7 @@ do ->
|
||||
for cb in @callbacks
|
||||
do (cb) => $.queueTask => cb.call @, {isCached: false}
|
||||
delete @callbacks
|
||||
req = $.ajax url, options
|
||||
req = (extra.ajax or $.ajax) url, options
|
||||
req.callbacks = [cb]
|
||||
reqs[url] = req
|
||||
$.cleanCache = (testf) ->
|
||||
|
||||
@ -69,65 +69,50 @@ CrossOrigin =
|
||||
cb blob
|
||||
|
||||
# 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/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 = {}
|
||||
results = {}
|
||||
success = (url, result) ->
|
||||
for cb in callbacks[url]
|
||||
$.queueTask -> cb.call result
|
||||
delete callbacks[url]
|
||||
results[url] = result
|
||||
failure = (url) ->
|
||||
for cb in callbacks[url]
|
||||
$.queueTask -> cb.call {}
|
||||
delete callbacks[url]
|
||||
# Interface is a subset of that of $.ajax.
|
||||
# Returns an object with `status`, `statusText`, `response` properties, all initially set falsy.
|
||||
# On success, populates the properties.
|
||||
# Both on success or error/abort/timeout, calls `options.onloadend` with the returned object as `this`.
|
||||
ajax: (url, options={}) ->
|
||||
{onloadend, timeout} = options
|
||||
|
||||
(url, cb, bypassCache, timeout) ->
|
||||
<% if (type === 'userscript') { %>
|
||||
unless GM?.xmlHttpRequest? or GM_xmlhttpRequest?
|
||||
if bypassCache
|
||||
$.cleanCache (url2) -> url2 is url
|
||||
req = $.cache url, cb
|
||||
return
|
||||
<% } %>
|
||||
<% if (type === 'userscript') { %>
|
||||
unless GM?.xmlHttpRequest? or GM_xmlhttpRequest?
|
||||
return $.ajax url, options
|
||||
<% } %>
|
||||
|
||||
if bypassCache
|
||||
delete results[url]
|
||||
else
|
||||
if results[url]
|
||||
cb.call results[url]
|
||||
return
|
||||
if callbacks[url]
|
||||
callbacks[url].push cb
|
||||
return
|
||||
callbacks[url] = [cb]
|
||||
req =
|
||||
status: 0
|
||||
statusText: ''
|
||||
response: null
|
||||
|
||||
<% if (type === 'userscript') { %>
|
||||
(GM?.xmlHttpRequest or GM_xmlhttpRequest)
|
||||
method: "GET"
|
||||
url: url+''
|
||||
timeout: timeout
|
||||
onload: (xhr) ->
|
||||
{status, statusText} = xhr
|
||||
try
|
||||
response = JSON.parse(xhr.responseText)
|
||||
success url, {status, statusText, response}
|
||||
catch
|
||||
failure url
|
||||
onerror: -> failure(url)
|
||||
onabort: -> failure(url)
|
||||
ontimeout: -> failure(url)
|
||||
<% } %>
|
||||
<% if (type === 'crx') { %>
|
||||
eventPageRequest {type: 'ajax', url, responseType: 'json', timeout}, (result) ->
|
||||
if result.status
|
||||
success url, result
|
||||
else
|
||||
failure url
|
||||
<% } %>
|
||||
<% if (type === 'userscript') { %>
|
||||
(GM?.xmlHttpRequest or GM_xmlhttpRequest)
|
||||
method: "GET"
|
||||
url: url+''
|
||||
timeout: timeout
|
||||
onload: (xhr) ->
|
||||
try
|
||||
response = JSON.parse(xhr.responseText)
|
||||
$.extend req, {response, status: xhr.status, statusText: xhr.statusText}
|
||||
onloadend.call(req)
|
||||
onerror: -> onloadend.call(req)
|
||||
onabort: -> onloadend.call(req)
|
||||
ontimeout: -> onloadend.call(req)
|
||||
<% } %>
|
||||
|
||||
<% if (type === 'crx') { %>
|
||||
eventPageRequest {type: 'ajax', url, responseType: 'json', timeout}, (result) ->
|
||||
if result.status
|
||||
$.extend req, result
|
||||
onloadend.call(req)
|
||||
<% } %>
|
||||
|
||||
req
|
||||
|
||||
cache: (url, cb, options={}, extra={}) ->
|
||||
extra.ajax = CrossOrigin.ajax
|
||||
$.cache url, cb, options, extra
|
||||
|
||||
permission: (cb) ->
|
||||
<% if (type === 'crx') { %>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user