From 5b9b7e380251ba5d7c9638e3da04746e2d91384c Mon Sep 17 00:00:00 2001 From: Kabir Sala Date: Mon, 17 Feb 2014 01:21:02 +0100 Subject: [PATCH] Handle edge cases, aka why name is not a separate header --- builds/4chan-X.user.js | 39 +++++++++++++++++++++++---------------- builds/crx/script.js | 31 ++++++++++++++++++++++--------- 2 files changed, 45 insertions(+), 25 deletions(-) diff --git a/builds/4chan-X.user.js b/builds/4chan-X.user.js index 5456ed885..113d4dfe9 100644 --- a/builds/4chan-X.user.js +++ b/builds/4chan-X.user.js @@ -23,7 +23,7 @@ // ==/UserScript== /* -* 4chan X - Version 1.3.7 - 2014-02-15 +* 4chan X - Version 1.3.7 - 2014-02-17 * * Licensed under the MIT license. * https://github.com/Spittie/4chan-x/blob/master/LICENSE @@ -5663,8 +5663,26 @@ QR.handleFiles(files); return $.addClass(QR.nodes.el, 'dump'); }, - handleBlob: function(blob) { - var _ref; + handleBlob: function(urlBlob, header, url) { + var blob, end, endnl, endsc, mime, name, name_end, name_start, start, _ref; + name = url.substr(url.lastIndexOf('/') + 1, url.length); + 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 && endsc < endnl) { + end = endsc; + } + mime = header.substr(start, end); + 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); + } if (blob.type === null) { return QR.error("Unsupported file type."); } @@ -5684,7 +5702,7 @@ url: url, overrideMimeType: "text/plain; charset=x-user-defined", onload: function(xhr) { - var data, end, header, i, mime, r, start, urlBlob; + var data, i, r; r = xhr.responseText; data = new Uint8Array(r.length); i = 0; @@ -5692,18 +5710,7 @@ data[i] = r.charCodeAt(i); i++; } - header = xhr.responseHeaders; - start = header.indexOf("Content-Type: ") + 14; - end = header.substr(start, header.length).indexOf("\n") - 1; - mime = header.substr(start, end); - if (mime === null) { - return; - } - urlBlob = new Blob([data], { - type: mime - }); - urlBlob.name = url.substr(url.lastIndexOf('/') + 1, url.length); - QR.handleBlob(urlBlob); + QR.handleBlob(data, xhr.responseHeaders, url); return; return { onerror: function(xhr) { diff --git a/builds/crx/script.js b/builds/crx/script.js index d776f0984..2b89ab336 100644 --- a/builds/crx/script.js +++ b/builds/crx/script.js @@ -1,6 +1,6 @@ // Generated by CoffeeScript /* -* 4chan X - Version 1.3.7 - 2014-02-15 +* 4chan X - Version 1.3.7 - 2014-02-17 * * Licensed under the MIT license. * https://github.com/Spittie/4chan-x/blob/master/LICENSE @@ -5675,8 +5675,26 @@ QR.handleFiles(files); return $.addClass(QR.nodes.el, 'dump'); }, - handleBlob: function(blob) { - var _ref; + handleBlob: function(urlBlob, header, url) { + var blob, end, endnl, endsc, mime, name, name_end, name_start, start, _ref; + name = url.substr(url.lastIndexOf('/') + 1, url.length); + 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 && endsc < endnl) { + end = endsc; + } + mime = header.substr(start, end); + 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); + } if (blob.type === null) { return QR.error("Unsupported file type."); } @@ -5695,13 +5713,8 @@ xhr.open('GET', url, true); xhr.responseType = 'blob'; xhr.onload = function(e) { - var urlBlob; if (this.readyState === this.DONE && xhr.status === 200) { - urlBlob = new Blob([this.response], { - type: this.getResponseHeader('content-type') - }); - urlBlob.name = url.substr(url.lastIndexOf('/') + 1, url.length); - QR.handleBlob(urlBlob); + QR.handleBlob(urlBlob, this.getResponseHeaders('Content-Type'), url); } else { QR.error("Can't load image."); }