fix issues in image from URL code
This commit is contained in:
parent
4cb318120b
commit
fada31bb78
@ -282,57 +282,43 @@ QR =
|
||||
QR.handleFiles files
|
||||
$.addClass QR.nodes.el, 'dump'
|
||||
|
||||
handleBlob: (urlBlob, header, url) ->
|
||||
name = url.substr(url.lastIndexOf('/')+1, url.length)
|
||||
#QUALITY coding at work
|
||||
start = header.indexOf("Content-Type: ") + 14
|
||||
endsc = header.substr(start, header.length).indexOf(";")
|
||||
endnl = header.substr(start, header.length).indexOf("\n") - 1
|
||||
end = endnl
|
||||
if (endsc != -1 and endsc < endnl)
|
||||
end = endsc
|
||||
mime = header.substr(start, end)
|
||||
handleBlob: (urlBlob, contentType, contentDisposition, url) ->
|
||||
name = url.match(/([^\/]+)\/*$/)?[1]
|
||||
mime = contentType?.match(/[^;]*/)[0] or 'application/octet-stream'
|
||||
match =
|
||||
contentDisposition?.match(/\bfilename\s*=\s*"((\\"|[^"])+)"/i)?[1] or
|
||||
contentType?.match(/\bname\s*=\s*"((\\"|[^"])+)"/i)?[1]
|
||||
if match
|
||||
name = match.replace /\\"/g, '"'
|
||||
blob = new Blob([urlBlob], {type: mime})
|
||||
blob.name = url.substr(url.lastIndexOf('/')+1, url.length)
|
||||
name_start = header.indexOf('name="') + 6
|
||||
if (name_start - 6 != -1)
|
||||
name_end = header.substr(name_start, header.length).indexOf('"')
|
||||
blob.name = header.substr(name_start, name_end)
|
||||
|
||||
return if blob.type is null
|
||||
QR.error "Unsupported file type."
|
||||
return unless blob.type in QR.mimeTypes
|
||||
QR.error "Unsupported file type."
|
||||
blob.name = name
|
||||
QR.handleFiles([blob])
|
||||
|
||||
handleUrl: ->
|
||||
url = prompt("Insert an url:")
|
||||
return if url is null
|
||||
|
||||
<% if (type === 'crx') { %>
|
||||
xhr = new XMLHttpRequest();
|
||||
xhr.open('GET', url, true)
|
||||
xhr.responseType = 'blob'
|
||||
xhr.onload = (e) ->
|
||||
if @readyState is @DONE && xhr.status is 200
|
||||
QR.handleBlob(@response, @getResponseHeader('Content-Type'), url)
|
||||
return
|
||||
contentType = @getResponseHeader('Content-Type')
|
||||
contentDisposition = @getResponseHeader('Content-Disposition')
|
||||
QR.handleBlob @response, contentType, contentDisposition, url
|
||||
else
|
||||
QR.error "Can't load image."
|
||||
return
|
||||
|
||||
xhr.onerror = (e) ->
|
||||
QR.error "Can't load image."
|
||||
return
|
||||
|
||||
xhr.send()
|
||||
return
|
||||
<% } %>
|
||||
|
||||
<% if (type === 'userscript') { %>
|
||||
GM_xmlhttpRequest {
|
||||
method: "GET",
|
||||
url: url,
|
||||
overrideMimeType: "text/plain; charset=x-user-defined",
|
||||
GM_xmlhttpRequest
|
||||
method: "GET"
|
||||
url: url
|
||||
overrideMimeType: "text/plain; charset=x-user-defined"
|
||||
onload: (xhr) ->
|
||||
r = xhr.responseText
|
||||
data = new Uint8Array(r.length)
|
||||
@ -340,14 +326,11 @@ QR =
|
||||
while i < r.length
|
||||
data[i] = r.charCodeAt(i)
|
||||
i++
|
||||
|
||||
QR.handleBlob(data, xhr.responseHeaders, url)
|
||||
return
|
||||
|
||||
onerror: (xhr) ->
|
||||
QR.error "Can't load image."
|
||||
}
|
||||
return
|
||||
contentType = xhr.responseHeaders.match(/Content-Type:\s*(.*)/i)?[1]
|
||||
contentDisposition = xhr.responseHeaders.match(/Content-Disposition:\s*(.*)/i)?[1]
|
||||
QR.handleBlob data, contentType, contentDisposition, url
|
||||
onerror: (xhr) ->
|
||||
QR.error "Can't load image."
|
||||
<% } %>
|
||||
|
||||
handleFiles: (files) ->
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user