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 cb new Uint8Array(response), contentType, contentDisposition
<% } %> <% } %>
<% if (type === 'userscript') { %> <% 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 = $.engine is 'gecko' and GM_info? and /^[0-2]\.|^3\.[01](?!\d)/.test(GM_info.version)
workaround or= GM_info?.script?.includeJSB?
options = options =
method: "GET" method: "GET"
url: url url: url
@ -47,15 +48,19 @@ CrossOrigin = do ->
i++ i++
else else
data = new Uint8Array xhr.response data = new Uint8Array xhr.response
contentType = xhr.responseHeaders.match(/Content-Type:\s*(.*)/i)?[1] if typeof xhr.responseHeaders is 'object'
contentDisposition = xhr.responseHeaders.match(/Content-Disposition:\s*(.*)/i)?[1] 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 cb data, contentType, contentDisposition
onerror: -> onerror: ->
cb null cb null
onabort: -> onabort: ->
cb null cb null
if workaround if workaround
options.overrideMimeType = 'text/plain; charset=x-user-defined' options.overrideMimeType = options.mimeType = 'text/plain; charset=x-user-defined'
else else
options.responseType = 'arraybuffer' options.responseType = 'arraybuffer'
GM_xmlhttpRequest options GM_xmlhttpRequest options
@ -71,6 +76,9 @@ CrossOrigin = do ->
contentType?.match(/\bname\s*=\s*"((\\"|[^"])+)"/i)?[1] contentType?.match(/\bname\s*=\s*"((\\"|[^"])+)"/i)?[1]
if match if match
name = match.replace /\\"/g, '"' 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 = new Blob([data], {type: mime})
blob.name = name blob.name = name
cb blob cb blob

View File

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