From d7913e1005adb7172e9f5d6f8b1abe963249541d Mon Sep 17 00:00:00 2001 From: ccd0 Date: Sat, 4 Jul 2015 14:54:53 -0700 Subject: [PATCH] Only use workaround for binary data in Greasemonkey versions < 3.2. --- Gruntfile.coffee | 1 + src/General/CrossOrigin.coffee | 25 +++++++++++++++++-------- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/Gruntfile.coffee b/Gruntfile.coffee index d440a4b40..e8e7461b6 100755 --- a/Gruntfile.coffee +++ b/Gruntfile.coffee @@ -293,6 +293,7 @@ module.exports = (grunt) -> GM_addValueChangeListener: true GM_openInTab: true GM_xmlhttpRequest: true + GM_info: true cloneInto: true chrome: true script: 'tmp-<%= pkg.type %>/*.js' diff --git a/src/General/CrossOrigin.coffee b/src/General/CrossOrigin.coffee index e70ad2744..20080c030 100644 --- a/src/General/CrossOrigin.coffee +++ b/src/General/CrossOrigin.coffee @@ -31,18 +31,22 @@ CrossOrigin = do -> cb new Uint8Array(response), contentType, contentDisposition <% } %> <% if (type === 'userscript') { %> - GM_xmlhttpRequest + # Use workaround for binary data in Greasemonkey versions < 3.2 + workaround = !GM_info.scriptHandler and /^[12]\.|^3\.[01](?!\d)/.test(GM_info.version) + options = method: "GET" url: url headers: headers - overrideMimeType: "text/plain; charset=x-user-defined" onload: (xhr) -> - r = xhr.responseText - data = new Uint8Array r.length - i = 0 - while i < r.length - data[i] = r.charCodeAt i - i++ + if workaround + r = xhr.responseText + data = new Uint8Array r.length + i = 0 + while i < r.length + data[i] = r.charCodeAt i + 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] cb data, contentType, contentDisposition @@ -50,6 +54,11 @@ CrossOrigin = do -> cb null onabort: -> cb null + if workaround + options.overrideMimeType = 'text/plain; charset=x-user-defined' + else + options.responseType = 'arraybuffer' + GM_xmlhttpRequest options <% } %> file: (url, cb) ->