Support cross-origin binary downloads in Safari.

This commit is contained in:
ccd0 2015-07-05 02:32:44 -07:00
parent ddd0365cc8
commit a97aaab79f
2 changed files with 21 additions and 4 deletions

View File

@ -31,8 +31,9 @@ CrossOrigin = do ->
cb new Uint8Array(response), contentType, contentDisposition
<% } %>
<% if (type === 'userscript') { %>
# Use workaround for binary data in Greasemonkey versions < 3.2
# Use workaround for binary data in Greasemonkey versions < 3.2 and in JS Blocker (Safari)
workaround = $.engine is 'gecko' and GM_info? and /^[0-2]\.|^3\.[01](?!\d)/.test(GM_info.version)
workaround or= GM_info?.script?.includeJSB?
options =
method: "GET"
url: url
@ -47,15 +48,19 @@ CrossOrigin = do ->
i++
else
data = new Uint8Array xhr.response
contentType = xhr.responseHeaders.match(/Content-Type:\s*(.*)/i)?[1]
contentDisposition = xhr.responseHeaders.match(/Content-Disposition:\s*(.*)/i)?[1]
if typeof xhr.responseHeaders is 'object'
contentType = xhr.responseHeaders['Content-Type']
contentDisposition = xhr.responseHeaders['Content-Disposition']
else
contentType = xhr.responseHeaders.match(/Content-Type:\s*(.*)/i)?[1]
contentDisposition = xhr.responseHeaders.match(/Content-Disposition:\s*(.*)/i)?[1]
cb data, contentType, contentDisposition
onerror: ->
cb null
onabort: ->
cb null
if workaround
options.overrideMimeType = 'text/plain; charset=x-user-defined'
options.overrideMimeType = options.mimeType = 'text/plain; charset=x-user-defined'
else
options.responseType = 'arraybuffer'
GM_xmlhttpRequest options
@ -71,6 +76,9 @@ CrossOrigin = do ->
contentType?.match(/\bname\s*=\s*"((\\"|[^"])+)"/i)?[1]
if match
name = match.replace /\\"/g, '"'
if GM_info?.script?.includeJSB?
# Content type comes back as 'text/plain; charset=x-user-defined'; guess from filename instead.
mime = QR.typeFromExtension[name.match(/[^.]*$/)[0].toLowerCase()] or 'application/octet-stream'
blob = new Blob([data], {type: mime})
blob.name = name
cb blob

View File

@ -1,6 +1,15 @@
QR =
mimeTypes: ['image/jpeg', 'image/png', 'image/gif', 'application/pdf', 'application/vnd.adobe.flash.movie', 'application/x-shockwave-flash', 'video/webm']
typeFromExtension:
'jpg': 'image/jpeg'
'jpeg': 'image/jpeg'
'png': 'image/png'
'gif': 'image/gif'
'pdf': 'application/pdf'
'swf': 'application/vnd.adobe.flash.movie'
'webm': 'video/webm'
init: ->
return unless Conf['Quick Reply']