Skip to content

Instantly share code, notes, and snippets.

@ccloli
Created April 9, 2016 12:47
Show Gist options
  • Save ccloli/9c6c6ab484cfc78bddcaf656afc6cc73 to your computer and use it in GitHub Desktop.
Save ccloli/9c6c6ab484cfc78bddcaf656afc6cc73 to your computer and use it in GitHub Desktop.
Get a large binary file as ArrayBuffer with GM_xhr
// ==UserScript==
// @name GM_xhr test
// @version 1.0
// @description Get a large binary file as ArrayBuffer with GM_xhr
// @include http://localhost
// @author 864907600cc
// @icon https://secure.gravatar.com/avatar/147834caf9ccb0a66b2505c753747867
// @grant GM_xmlhttpRequest
// @connect localhost
// @namespace http://ext.ccloli.com
// ==/UserScript==
// modify url and @connect if needed
var url = 'http://localhost/test.mp3';
GM_xmlhttpRequest({
method: 'GET',
url: url,
responseType: 'arraybuffer',
onload: function(res){
var t = new Date();
console.log('File loaded at ' + t);
var response = res.response;
var t2 = new Date();
console.log('Get ArrayBuffer content at ' + t2);
console.log('File size: ' + response.byteLength);
console.log('Elapsed time: ' + (t2 - t) + ' ms');
// play music, also check if pasted time is correct
var blob = new Blob([response], {type: 'audio/mpeg'});
var blobURL = URL.createObjectURL(blob);
var audio = document.createElement('audio');
audio.src = blobURL;
audio.play();
},
onerror: function(res){
console.log('Can\'t load file.');
}
});
@ccloli
Copy link
Author

ccloli commented Apr 9, 2016

I uploaded the file to tempsend, if you need it, download it or get file url at http://tempsend.com/7DA7B3FA74 http://tempsend.com/2B71E30804 (expired after 1 month).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment