Add header support to CrossOrigin.ajax

This commit is contained in:
ccd0 2019-03-23 19:07:48 -07:00
parent 996a183abe
commit 7b2dee7279
2 changed files with 53 additions and 22 deletions

View File

@ -25,16 +25,19 @@ handlers =
xhr.open 'GET', request.url, true xhr.open 'GET', request.url, true
xhr.responseType = request.responseType xhr.responseType = request.responseType
xhr.timeout = request.timeout xhr.timeout = request.timeout
for key, value of (request.headers or {})
xhr.setRequestHeader key, value
xhr.addEventListener 'load', -> xhr.addEventListener 'load', ->
{status, statusText, response} = @ {status, statusText, response} = @
responseHeaderString = @getAllResponseHeaders()
if @readyState is @DONE && xhr.status is 200 if @readyState is @DONE && xhr.status is 200
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'
cb {status, statusText, response, contentType, contentDisposition} cb {status, statusText, response, responseHeaderString, contentType, contentDisposition}
else else
cb {status, statusText, response, error: true} cb {status, statusText, response, responseHeaderString, error: true}
, false , false
xhr.addEventListener 'error', -> xhr.addEventListener 'error', ->
cb {error: true} cb {error: true}

View File

@ -68,45 +68,73 @@ CrossOrigin =
blob.name = name blob.name = name
cb blob cb blob
Request: class Request
status: 0
statusText: ''
response: null
responseHeaderString: null
getResponseHeader: (headerName) ->
if !@responseHeaders? and @responseHeaderString?
@responseHeaders = {}
for header in @responseHeaderString.split('\r\n')
if (i = header.indexOf(':')) >= 0
key = header[...i].trim().toLowerCase()
val = header[i+1..].trim()
@responseHeaders[key] = val
(@responseHeaders or {})[headerName.toLowerCase()] ? null
# 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.
# Interface is a subset of that of $.ajax. # Interface is a subset of that of $.ajax.
# Returns an object with `status`, `statusText`, `response` properties, all initially set falsy. # Options:
# On success, populates the properties. # `options.onloadend` - called with the returned object as `this` on success or error/abort/timeout.
# Both on success or error/abort/timeout, calls `options.onloadend` with the returned object as `this`. # `options.timeout` - time limit for request
ajax: (url, options={}) -> # `extra.headers` - request headers
# Returned object properties:
# `status` - HTTP status (0 if connection not successful)
# `statusText` - HTTP status text
# `response` - decoded response body
# `abort` - if present, can be called to abort the request
# `getResponseHeader` - function for reading response headers
ajax: (url, options={}, extra={}) ->
{onloadend, timeout} = options {onloadend, timeout} = options
{headers} = extra
<% if (type === 'userscript') { %> <% if (type === 'userscript') { %>
unless GM?.xmlHttpRequest? or GM_xmlhttpRequest? unless GM?.xmlHttpRequest? or GM_xmlhttpRequest?
return $.ajax url, options return $.ajax url, options
<% } %> <% } %>
req = req = new CrossOrigin.Request()
status: 0 req.onloadend = onloadend
statusText: ''
response: null
<% if (type === 'userscript') { %> <% if (type === 'userscript') { %>
gmReq = (GM?.xmlHttpRequest or GM_xmlhttpRequest) gmReq = (GM?.xmlHttpRequest or GM_xmlhttpRequest) {
method: "GET" method: 'GET'
url: url+'' url
timeout: timeout headers
timeout
onload: (xhr) -> onload: (xhr) ->
try try
response = JSON.parse(xhr.responseText) response = if xhr.responseText then JSON.parse(xhr.responseText) else null
$.extend req, {response, status: xhr.status, statusText: xhr.statusText} $.extend req, {
onloadend.call(req) response
onerror: -> onloadend.call(req) status: xhr.status
onabort: -> onloadend.call(req) statusText: xhr.statusText
ontimeout: -> onloadend.call(req) responseHeaderString: xhr.responseHeaders
}
req.onloadend()
onerror: -> req.onloadend()
onabort: -> req.onloadend()
ontimeout: -> req.onloadend()
}
req.abort = gmReq.abort req.abort = gmReq.abort
<% } %> <% } %>
<% if (type === 'crx') { %> <% if (type === 'crx') { %>
eventPageRequest {type: 'ajax', url, responseType: 'json', timeout}, (result) -> eventPageRequest {type: 'ajax', url, responseType: 'json', headers, timeout}, (result) ->
if result.status if result.status
$.extend req, result $.extend req, result
onloadend.call(req) req.onloadend()
<% } %> <% } %>
req req