Revise CrossOrigin.json interface.

This commit is contained in:
ccd0 2018-09-18 00:26:43 -07:00
parent 1f7846502d
commit 898816d211
3 changed files with 50 additions and 31 deletions

View File

@ -106,13 +106,15 @@ class Fetcher
archive = Redirect.data.post[@boardID] archive = Redirect.data.post[@boardID]
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']
CrossOrigin.json url, (response) => that = @
{media} = response CrossOrigin.json url, ->
if !encryptionOK and media then for key of media when /_link$/.test key if !encryptionOK and @response?.media
# Image/thumbnail URLs loaded over HTTP can be modified in transit. {media} = @response
# Require them to be from an HTTP host so that no referrer is sent to them from an HTTPS page. for key of media when /_link$/.test key
delete media[key] unless media[key]?.match /^http:\/\// # Image/thumbnail URLs loaded over HTTP can be modified in transit.
@parseArchivedPost response, url, archive # Require them to be from an HTTP host so that no referrer is sent to them from an HTTPS page.
delete media[key] unless media[key]?.match /^http:\/\//
that.parseArchivedPost @response, url, archive
return true return true
return false return false

View File

@ -27,15 +27,15 @@ ajax = (request, sender, sendResponse) ->
xhr.open 'GET', request.url, true xhr.open 'GET', request.url, true
xhr.responseType = request.responseType xhr.responseType = request.responseType
xhr.addEventListener 'load', -> xhr.addEventListener 'load', ->
{status, statusText, response} = @
if @readyState is @DONE && xhr.status is 200 if @readyState is @DONE && xhr.status is 200
{response} = @
if request.responseType is 'arraybuffer' if request.responseType is 'arraybuffer'
response = [new Uint8Array(response)...] response = [new Uint8Array(response)...]
contentType = @getResponseHeader 'Content-Type' contentType = @getResponseHeader 'Content-Type'
contentDisposition = @getResponseHeader 'Content-Disposition' contentDisposition = @getResponseHeader 'Content-Disposition'
chrome.tabs.sendMessage sender.tab.id, {id, response, contentType, contentDisposition} chrome.tabs.sendMessage sender.tab.id, {id, status, statusText, response, contentType, contentDisposition}
else else
chrome.tabs.sendMessage sender.tab.id, {id, error: true} chrome.tabs.sendMessage sender.tab.id, {id, status, statusText, response, error: true}
, false , false
xhr.addEventListener 'error', -> xhr.addEventListener 'error', ->
chrome.tabs.sendMessage sender.tab.id, {id, error: true} chrome.tabs.sendMessage sender.tab.id, {id, error: true}

View File

@ -83,45 +83,62 @@ CrossOrigin =
blob.name = name blob.name = name
cb blob 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, calls `cb` with a `this` of `{}`.
json: do -> json: do ->
callbacks = {} callbacks = {}
responses = {} 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]
(url, cb) -> (url, cb) ->
<% if (type === 'crx') { %> <% if (type === 'crx') { %>
if /^https:\/\//.test(url) or location.protocol is 'http:' plainAJAX = (/^https:\/\//.test(url) or location.protocol is 'http:')
return $.cache url, (-> cb @response), responseType: 'json'
<% } %> <% } %>
<% if (type === 'userscript') { %> <% if (type === 'userscript') { %>
unless GM?.xmlHttpRequest? or GM_xmlhttpRequest? plainAJAX = not (GM?.xmlHttpRequest? or GM_xmlhttpRequest?)
return $.cache url, (-> cb @response), responseType: 'json'
<% } %> <% } %>
if responses[url] if plainAJAX
cb responses[url] if (req = $.cache url, cb, responseType: 'json')
$.on req, 'abort error', -> cb.call({})
else
cb.call {}
return
if results[url]
cb.call results[url]
return return
if callbacks[url] if callbacks[url]
callbacks[url].push cb callbacks[url].push cb
return return
callbacks[url] = [cb] 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+''
onload: (xhr) -> onload: (xhr) ->
response = JSON.parse xhr.responseText {status, statusText} = xhr
cb response for cb in callbacks[url] try
delete callbacks[url] response = JSON.parse(xhr.responseText)
responses[url] = response success url, {status, statusText, response}
onerror: -> catch
delete callbacks[url] failure url
onabort: -> onerror: -> failure(url)
delete callbacks[url] onabort: -> failure(url)
<% } %> <% } %>
<% if (type === 'crx') { %> <% if (type === 'crx') { %>
eventPageRequest url, 'json', ({response, error}) -> eventPageRequest url, 'json', (result) ->
if error if result.status
delete callbacks[url] success url, result
else else
cb response for cb in callbacks[url] failure url
delete callbacks[url]
responses[url] = response
<% } %> <% } %>