Replace CrossOrigin.json with simpler CrossOrigin.ajax and a CrossOrigin.cache making use of $.cache.

This commit is contained in:
ccd0 2019-03-10 21:11:43 -07:00
parent 78a79f1942
commit 188422f1ae
6 changed files with 54 additions and 67 deletions

View File

@ -68,7 +68,8 @@ Redirect =
continue continue
load(i).call {status: 200, response} load(i).call {status: 200, response}
else else
CrossOrigin.json url, load(i), true CrossOrigin.ajax url,
onloadend: load(i)
else else
Redirect.parse [], cb Redirect.parse [], cb
return return

View File

@ -111,7 +111,7 @@ Embedding =
if service.queue.length >= service.batchSize if service.queue.length >= service.batchSize
Embedding.flushTitles service Embedding.flushTitles service
else else
CrossOrigin.json service.api(uid), (-> Embedding.cb.title @, data) CrossOrigin.cache service.api(uid), (-> Embedding.cb.title @, data)
flushTitles: (service) -> flushTitles: (service) ->
{queue} = service {queue} = service
@ -120,7 +120,7 @@ Embedding =
cb = -> cb = ->
Embedding.cb.title @, data for data in queue Embedding.cb.title @, data for data in queue
return 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) -> preview: (data) ->
{key, uid, link} = data {key, uid, link} = data
@ -275,7 +275,7 @@ Embedding =
el = $.el 'pre', el = $.el 'pre',
hidden: true hidden: true
id: "gist-embed-#{counter++}" 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.textContent = Object.values(@response.files)[0].content
el.className = 'prettyprint' el.className = 'prettyprint'
$.global -> $.global ->

View File

@ -228,10 +228,11 @@ ThreadWatcher =
whenModified: if force then false else 'ThreadWatcher' whenModified: if force then false else 'ThreadWatcher'
else else
req = {abort: () -> req.aborted = true} req = {abort: () -> req.aborted = true}
CrossOrigin.json url, -> CrossOrigin.ajax url,
return if req.aborted onloadend: ->
ThreadWatcher.parseStatus.call @, thread return if req.aborted
, true, $.MINUTE ThreadWatcher.parseStatus.call @, thread
timeout: $.MINUTE
ThreadWatcher.requests.push req ThreadWatcher.requests.push req
parseStatus: ({siteID, boardID, threadID, data}) -> parseStatus: ({siteID, boardID, threadID, data}) ->

View File

@ -111,7 +111,7 @@ class Fetcher
encryptionOK = /^https:\/\//.test(url) or location.protocol is 'http:' encryptionOK = /^https:\/\//.test(url) or location.protocol is 'http:'
if encryptionOK or Conf['Exempt Archives from Encryption'] if encryptionOK or Conf['Exempt Archives from Encryption']
that = @ that = @
CrossOrigin.json url, -> CrossOrigin.cache url, ->
if !encryptionOK and @response?.media if !encryptionOK and @response?.media
{media} = @response {media} = @response
for key of media when /_link$/.test key for key of media when /_link$/.test key

View File

@ -84,7 +84,7 @@ $.ajax = do ->
do -> do ->
reqs = {} reqs = {}
$.cache = (url, cb, options={}) -> $.cache = (url, cb, options={}, extra={}) ->
if (req = reqs[url]) if (req = reqs[url])
if req.callbacks if req.callbacks
req.callbacks.push cb req.callbacks.push cb
@ -97,7 +97,7 @@ do ->
for cb in @callbacks for cb in @callbacks
do (cb) => $.queueTask => cb.call @, {isCached: false} do (cb) => $.queueTask => cb.call @, {isCached: false}
delete @callbacks delete @callbacks
req = $.ajax url, options req = (extra.ajax or $.ajax) url, options
req.callbacks = [cb] req.callbacks = [cb]
reqs[url] = req reqs[url] = req
$.cleanCache = (testf) -> $.cleanCache = (testf) ->

View File

@ -69,65 +69,50 @@ CrossOrigin =
cb blob cb blob
# 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. # Interface is a subset of that of $.ajax.
# On error/abort/timeout, calls `cb` with a `this` of `{}` or in some cases an XMLHttpRequest reflecting the error. # Returns an object with `status`, `statusText`, `response` properties, all initially set falsy.
# If `bypassCache` is true, ignores previously cached results. # On success, populates the properties.
json: do -> # Both on success or error/abort/timeout, calls `options.onloadend` with the returned object as `this`.
callbacks = {} ajax: (url, options={}) ->
results = {} {onloadend, timeout} = options
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]
(url, cb, bypassCache, timeout) -> <% if (type === 'userscript') { %>
<% if (type === 'userscript') { %> unless GM?.xmlHttpRequest? or GM_xmlhttpRequest?
unless GM?.xmlHttpRequest? or GM_xmlhttpRequest? return $.ajax url, options
if bypassCache <% } %>
$.cleanCache (url2) -> url2 is url
req = $.cache url, cb
return
<% } %>
if bypassCache req =
delete results[url] status: 0
else statusText: ''
if results[url] response: null
cb.call results[url]
return
if callbacks[url]
callbacks[url].push cb
return
callbacks[url] = [cb]
<% if (type === 'userscript') { %> <% if (type === 'userscript') { %>
(GM?.xmlHttpRequest or GM_xmlhttpRequest) (GM?.xmlHttpRequest or GM_xmlhttpRequest)
method: "GET" method: "GET"
url: url+'' url: url+''
timeout: timeout timeout: timeout
onload: (xhr) -> onload: (xhr) ->
{status, statusText} = xhr try
try response = JSON.parse(xhr.responseText)
response = JSON.parse(xhr.responseText) $.extend req, {response, status: xhr.status, statusText: xhr.statusText}
success url, {status, statusText, response} onloadend.call(req)
catch onerror: -> onloadend.call(req)
failure url onabort: -> onloadend.call(req)
onerror: -> failure(url) ontimeout: -> onloadend.call(req)
onabort: -> failure(url) <% } %>
ontimeout: -> failure(url)
<% } %> <% if (type === 'crx') { %>
<% if (type === 'crx') { %> eventPageRequest {type: 'ajax', url, responseType: 'json', timeout}, (result) ->
eventPageRequest {type: 'ajax', url, responseType: 'json', timeout}, (result) -> if result.status
if result.status $.extend req, result
success url, result onloadend.call(req)
else <% } %>
failure url
<% } %> req
cache: (url, cb, options={}, extra={}) ->
extra.ajax = CrossOrigin.ajax
$.cache url, cb, options, extra
permission: (cb) -> permission: (cb) ->
<% if (type === 'crx') { %> <% if (type === 'crx') { %>