Binary library

Lets you download files as a binary stream, and upload them as multipart/form-data. Userscript library.

Version vom 20.12.2014. Aktuellste Version

Dieses Skript sollte nicht direkt installiert werden. Es handelt sich hier um eine Bibliothek für andere Skripte, welche über folgenden Befehl in den Metadaten eines Skriptes eingebunden wird // @require https://update.greatest.deepsurf.us/scripts/7079/28929/Binary%20library.js

  1. // ==UserScript==
  2. // @name Binary library
  3. // @namespace shoecream@luelinks.net
  4. // @description Lets you download files as a binary stream, and upload them as multipart/form-data. Userscript library.
  5. // @include *
  6. // @grant GM_xmlhttpRequest
  7. // @license cc-by-sa
  8. // ==/UserScript==
  9.  
  10. BinaryRes = {};
  11.  
  12. BinaryRes.get = function (object) {
  13. var request = {
  14. method: 'GET',
  15. url: object.url,
  16. overrideMimeType: 'text/plain; charset=x-user-defined',
  17. binary: true,
  18. onload: object.callback,
  19. };
  20.  
  21. if (object.override) {
  22. for (var attr in object.override) request[attr] = object.override[attr]
  23. }
  24.  
  25. GM_xmlhttpRequest(request);
  26. }
  27.  
  28. BinaryRes._clean = function (bytes) {
  29. var binarray = [];
  30. [].forEach.call(bytes, function (byte) {
  31. binarray.push(String.fromCharCode(byte.charCodeAt(0) & 0xff));
  32. });
  33. return binarray.join('');
  34. }
  35.  
  36. BinaryRes._bound = function() {
  37. var z = 98729145294692; var a = 12400722650394;
  38. return Array(28).join('-') + Math.floor(Math.random() * (z - a)) + a;
  39. }
  40.  
  41. BinaryRes._typeof = function(value) {
  42. // from crockford
  43. var s = typeof value;
  44. if (s === 'object') {
  45. if (value) {
  46. if (typeof value.length === 'number' &&
  47. !(value.propertyIsEnumerable('length')) &&
  48. typeof value.splice === 'function') {
  49. s = 'array';
  50. }
  51. } else {
  52. s = 'null';
  53. }
  54. }
  55. return s;
  56. }
  57.  
  58. BinaryRes.post = function (obj) {
  59. // url, callback, data
  60. var build = [];
  61. var boundary = BinaryRes._bound();
  62. for (var name in obj.data) {
  63. if (obj.data.hasOwnProperty(name)) {
  64. build.push('--' + boundary);
  65. var disp = 'Content-Disposition: form-data; name="' + name + '"';
  66. if (BinaryRes._typeof(obj.data[name]) == 'object') {
  67. var value = BinaryRes._clean(obj.data[name].value);
  68. if (obj.data[name].filename)
  69. disp += '; filename="' + obj.data[name].filename + '"';
  70. build.push(disp);
  71. build.push('Content-type: ' + obj.data[name].type);
  72. } else {
  73. var value = obj.data[name];
  74. build.push(disp);
  75. }
  76. build.push('');
  77. build.push(value);
  78. }
  79. }
  80. build.push('--' + boundary + '--');
  81. var data = build.join('\r\n');
  82. var request = {
  83. method: 'POST',
  84. url: obj.url,
  85. binary: true,
  86. headers: {
  87. "Content-Type": 'multipart/form-data; boundary=' + boundary,
  88. "Content-Length": data.length
  89. },
  90. data: data,
  91. onload: obj.callback
  92. };
  93. if (obj.override) {
  94. for (var attr in obj.override) request[attr] = obj.override[attr]
  95. }
  96. GM_xmlhttpRequest(request);
  97. }
  98.  
  99. BinaryRes.guessType = function(stream) {
  100. if (!stream) return;
  101. var dict = [
  102. [0, 3, '\xFF\xD8\xFF', 'image/jpeg'],
  103. [1, 4, 'PNG', 'image/png'],
  104. [0, 3, 'GIF', 'image/gif'],
  105. [0, 4, '%PDF', 'application/pdf'],
  106. ]
  107. stream = BinaryRes._clean(stream);
  108. for (var ii = 0; ii < dict.length; ii++) {
  109. if (stream.slice(dict[ii][0], dict[ii][1]) === dict[ii][2])
  110. return dict[ii][3];
  111. }
  112. }