From 1ffb8da5b7a208553d7f4bcfbbbbc8f5063e5ff7 Mon Sep 17 00:00:00 2001 From: Zixaphir Date: Thu, 17 Jul 2014 23:29:30 -0700 Subject: [PATCH] Cleanup Post from URL code. --- builds/appchan-x.user.js | 9 +++++---- builds/crx/script.js | 9 ++++----- src/Posting/QR.coffee | 38 +++++++++++++++++++++++--------------- 3 files changed, 32 insertions(+), 24 deletions(-) diff --git a/builds/appchan-x.user.js b/builds/appchan-x.user.js index 5c66946d1..26c890b02 100644 --- a/builds/appchan-x.user.js +++ b/builds/appchan-x.user.js @@ -8971,7 +8971,7 @@ }, handleUrl: function() { var url; - url = prompt("Insert an url:"); + url = prompt("Enter a URL:"); if (url === null) { return; } @@ -8980,16 +8980,17 @@ url: url, overrideMimeType: "text/plain; charset=x-user-defined", onload: function(xhr) { - var contentDisposition, contentType, data, i, r, _ref, _ref1; + var contentDisposition, contentType, data, h, i, r; r = xhr.responseText; + h = xhr.responseHeaders; data = new Uint8Array(r.length); i = 0; while (i < r.length) { data[i] = r.charCodeAt(i); i++; } - contentType = (_ref = xhr.responseHeaders.match(/Content-Type:\s*(.*)/i)) != null ? _ref[1] : void 0; - contentDisposition = (_ref1 = xhr.responseHeaders.match(/Content-Disposition:\s*(.*)/i)) != null ? _ref1[1] : void 0; + contentType = (h.match(/Content-Type:\s*(.*)/i) || [])[1]; + contentDisposition = (h.match(/Content-Disposition:\s*(.*)/i) || [])[1]; return QR.handleBlob(data, contentType, contentDisposition, url); }, onerror: function(xhr) { diff --git a/builds/crx/script.js b/builds/crx/script.js index cec106929..111653d11 100644 --- a/builds/crx/script.js +++ b/builds/crx/script.js @@ -9037,13 +9037,12 @@ xhr.responseType = 'blob'; xhr.onload = function(e) { var contentDisposition, contentType; - if (this.readyState === this.DONE && xhr.status === 200) { - contentType = this.getResponseHeader('Content-Type'); - contentDisposition = this.getResponseHeader('Content-Disposition'); - return QR.handleBlob(this.response, contentType, contentDisposition, url); - } else { + if (!(this.readyState === this.DONE && xhr.status === 200)) { return QR.error("Can't load image."); } + contentType = this.getResponseHeader('Content-Type'); + contentDisposition = this.getResponseHeader('Content-Disposition'); + return QR.handleBlob(this.response, contentType, contentDisposition, url); }; xhr.onerror = function(e) { return QR.error("Can't load image."); diff --git a/src/Posting/QR.coffee b/src/Posting/QR.coffee index f0eb886c1..6440c6608 100644 --- a/src/Posting/QR.coffee +++ b/src/Posting/QR.coffee @@ -300,7 +300,7 @@ QR = QR.handleFiles([blob]) handleUrl: -> - url = prompt("Insert an url:") + url = prompt("Enter a URL:") return if url is null <% if (type === 'crx') { %> @@ -308,34 +308,42 @@ QR = xhr.open('GET', url, true) xhr.responseType = 'blob' xhr.onload = (e) -> - if @readyState is @DONE && xhr.status is 200 - contentType = @getResponseHeader('Content-Type') - contentDisposition = @getResponseHeader('Content-Disposition') - QR.handleBlob @response, contentType, contentDisposition, url - else - QR.error "Can't load image." + return QR.error "Can't load image." unless @readyState is @DONE and xhr.status is 200 + + contentType = @getResponseHeader('Content-Type') + contentDisposition = @getResponseHeader('Content-Disposition') + QR.handleBlob @response, contentType, contentDisposition, url + xhr.onerror = (e) -> QR.error "Can't load image." xhr.send() - <% } %> - <% if (type === 'userscript') { %> + <% } else { %> + GM_xmlhttpRequest method: "GET" url: url + + # FIXME: responseType: 'blob' + # Could do it now, but don't wanna kill off legacy GM versions yet overrideMimeType: "text/plain; charset=x-user-defined" onload: (xhr) -> - r = xhr.responseText - data = new Uint8Array(r.length) - i = 0 + r = xhr.responseText + h = xhr.responseHeaders + data = new Uint8Array r.length + i = 0 + while i < r.length - data[i] = r.charCodeAt(i) + data[i] = r.charCodeAt i i++ - contentType = xhr.responseHeaders.match(/Content-Type:\s*(.*)/i)?[1] - contentDisposition = xhr.responseHeaders.match(/Content-Disposition:\s*(.*)/i)?[1] + + contentType = (h.match(/Content-Type:\s*(.*)/i) or [])[1] + contentDisposition = (h.match(/Content-Disposition:\s*(.*)/i) or [])[1] QR.handleBlob data, contentType, contentDisposition, url + onerror: (xhr) -> QR.error "Can't load image." + <% } %> handleFiles: (files) ->