From 188422f1aee69dfa45cf31ebd035c3029c3b32d1 Mon Sep 17 00:00:00 2001 From: ccd0 Date: Sun, 10 Mar 2019 21:11:43 -0700 Subject: [PATCH] Replace CrossOrigin.json with simpler CrossOrigin.ajax and a CrossOrigin.cache making use of $.cache. --- src/Archive/Redirect.coffee | 3 +- src/Linkification/Embedding.coffee | 6 +- src/Monitoring/ThreadWatcher.coffee | 9 +-- src/classes/Fetcher.coffee | 2 +- src/platform/$.coffee | 4 +- src/platform/CrossOrigin.coffee | 97 ++++++++++++----------------- 6 files changed, 54 insertions(+), 67 deletions(-) diff --git a/src/Archive/Redirect.coffee b/src/Archive/Redirect.coffee index 03d20cfab..c7ec71f3d 100644 --- a/src/Archive/Redirect.coffee +++ b/src/Archive/Redirect.coffee @@ -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 diff --git a/src/Linkification/Embedding.coffee b/src/Linkification/Embedding.coffee index 7de873103..98084a655 100644 --- a/src/Linkification/Embedding.coffee +++ b/src/Linkification/Embedding.coffee @@ -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 -> diff --git a/src/Monitoring/ThreadWatcher.coffee b/src/Monitoring/ThreadWatcher.coffee index d63002850..5e3409219 100644 --- a/src/Monitoring/ThreadWatcher.coffee +++ b/src/Monitoring/ThreadWatcher.coffee @@ -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}) -> diff --git a/src/classes/Fetcher.coffee b/src/classes/Fetcher.coffee index 472d1f8de..a396bbca1 100644 --- a/src/classes/Fetcher.coffee +++ b/src/classes/Fetcher.coffee @@ -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 diff --git a/src/platform/$.coffee b/src/platform/$.coffee index dc19a2e7f..dd961b27a 100644 --- a/src/platform/$.coffee +++ b/src/platform/$.coffee @@ -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) -> diff --git a/src/platform/CrossOrigin.coffee b/src/platform/CrossOrigin.coffee index aa73888c5..691105786 100644 --- a/src/platform/CrossOrigin.coffee +++ b/src/platform/CrossOrigin.coffee @@ -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') { %>