Reorganize CrossOrigin.binary.

This commit is contained in:
ccd0 2019-04-11 05:33:39 -07:00
parent 0b341ce2d2
commit 2120c93bf0
2 changed files with 10 additions and 15 deletions

View File

@ -30,14 +30,9 @@ handlers =
xhr.addEventListener 'load', -> xhr.addEventListener 'load', ->
{status, statusText, response} = @ {status, statusText, response} = @
responseHeaderString = @getAllResponseHeaders() responseHeaderString = @getAllResponseHeaders()
if @readyState is @DONE && xhr.status is 200 if response and request.responseType is 'arraybuffer'
if request.responseType is 'arraybuffer' response = [new Uint8Array(response)...]
response = [new Uint8Array(response)...] cb {status, statusText, response, responseHeaderString}
contentType = @getResponseHeader 'Content-Type'
contentDisposition = @getResponseHeader 'Content-Disposition'
cb {status, statusText, response, responseHeaderString, contentType, contentDisposition}
else
cb {status, statusText, response, responseHeaderString, error: true}
, false , false
xhr.addEventListener 'error', -> xhr.addEventListener 'error', ->
cb {error: true} cb {error: true}

View File

@ -14,9 +14,9 @@ CrossOrigin =
# XXX https://forums.lanik.us/viewtopic.php?f=64&t=24173&p=78310 # XXX https://forums.lanik.us/viewtopic.php?f=64&t=24173&p=78310
url = url.replace /^((?:https?:)?\/\/(?:\w+\.)?4c(?:ha|d)n\.org)\/adv\//, '$1//adv/' url = url.replace /^((?:https?:)?\/\/(?:\w+\.)?4c(?:ha|d)n\.org)\/adv\//, '$1//adv/'
<% if (type === 'crx') { %> <% if (type === 'crx') { %>
eventPageRequest {type: 'ajax', url, responseType: 'arraybuffer'}, ({response, contentType, contentDisposition, error}) -> eventPageRequest {type: 'ajax', url, responseType: 'arraybuffer'}, ({response, responseHeaderString}) ->
return cb null if error response = new Uint8Array(response) if response
cb new Uint8Array(response), contentType, contentDisposition cb response, responseHeaderString
<% } %> <% } %>
<% if (type === 'userscript') { %> <% if (type === 'userscript') { %>
# Use workaround for binary data in Greasemonkey versions < 3.2, in Pale Moon for all GM versions, and in JS Blocker (Safari). # Use workaround for binary data in Greasemonkey versions < 3.2, in Pale Moon for all GM versions, and in JS Blocker (Safari).
@ -37,9 +37,7 @@ CrossOrigin =
i++ i++
else else
data = new Uint8Array xhr.response data = new Uint8Array xhr.response
contentType = xhr.responseHeaders.match(/Content-Type:\s*(.*)/i)?[1] cb data, xhr.responseHeaders
contentDisposition = xhr.responseHeaders.match(/Content-Disposition:\s*(.*)/i)?[1]
cb data, contentType, contentDisposition
onerror: -> onerror: ->
cb null cb null
onabort: -> onabort: ->
@ -52,9 +50,11 @@ CrossOrigin =
<% } %> <% } %>
file: (url, cb) -> file: (url, cb) ->
CrossOrigin.binary url, (data, contentType, contentDisposition) -> CrossOrigin.binary url, (data, headers) ->
return cb null unless data? return cb null unless data?
name = url.match(/([^\/]+)\/*$/)?[1] name = url.match(/([^\/]+)\/*$/)?[1]
contentType = headers.match(/Content-Type:\s*(.*)/i)?[1]
contentDisposition = headers.match(/Content-Disposition:\s*(.*)/i)?[1]
mime = contentType?.match(/[^;]*/)[0] or 'application/octet-stream' mime = contentType?.match(/[^;]*/)[0] or 'application/octet-stream'
match = match =
contentDisposition?.match(/\bfilename\s*=\s*"((\\"|[^"])+)"/i)?[1] or contentDisposition?.match(/\bfilename\s*=\s*"((\\"|[^"])+)"/i)?[1] or