Binary library

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

このスクリプトは単体で利用できません。右のようなメタデータを含むスクリプトから、ライブラリとして読み込まれます: // @require https://update.greatest.deepsurf.us/scripts/7079/28932/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. {
  14. var request =
  15. {
  16. method: 'GET',
  17. url: object.url,
  18. overrideMimeType: 'text/plain; charset=x-user-defined',
  19. binary: true,
  20. onload: object.callback,
  21. };
  22.  
  23. if (object.override)
  24. {
  25. for (var attr in object.override) request[attr] = object.override[attr]
  26. }
  27.  
  28. GM_xmlhttpRequest(request);
  29. }
  30.  
  31. BinaryRes._clean = function (bytes)
  32. {
  33. var binarray = [];
  34. [].forEach.call(bytes, function (byte)
  35. {
  36. binarray.push(String.fromCharCode(byte.charCodeAt(0) & 0xff));
  37. });
  38. return binarray.join('');
  39. }
  40.  
  41. BinaryRes._bound = function()
  42. {
  43. var z = 98729145294692;
  44. var a = 12400722650394;
  45. return Array(28).join('-') + Math.floor(Math.random() * (z - a)) + a;
  46. }
  47.  
  48. BinaryRes._typeof = function(value) {
  49. // from crockford
  50. var s = typeof value;
  51. if (s === 'object') {
  52. if (value) {
  53. if (typeof value.length === 'number' &&
  54. !(value.propertyIsEnumerable('length')) &&
  55. typeof value.splice === 'function') {
  56. s = 'array';
  57. }
  58. } else {
  59. s = 'null';
  60. }
  61. }
  62. return s;
  63. }
  64.  
  65. BinaryRes.post = function (obj)
  66. {
  67. // url, callback, data
  68. var build = [];
  69. var boundary = BinaryRes._bound();
  70. for (var name in obj.data)
  71. {
  72. if (obj.data.hasOwnProperty(name))
  73. {
  74. build.push('--' + boundary);
  75. var disp = 'Content-Disposition: form-data; name="' + name + '"';
  76. if (BinaryRes._typeof(obj.data[name]) == 'object')
  77. {
  78. var value = BinaryRes._clean(obj.data[name].value);
  79. if (obj.data[name].filename)
  80. disp += '; filename="' + obj.data[name].filename + '"';
  81. build.push(disp);
  82. build.push('Content-type: ' + obj.data[name].type);
  83. } else
  84. {
  85. var value = obj.data[name];
  86. build.push(disp);
  87. }
  88. build.push('');
  89. build.push(value);
  90. }
  91. }
  92. build.push('--' + boundary + '--');
  93. var data = build.join('\r\n');
  94. var request =
  95. {
  96. method: 'POST',
  97. url: obj.url,
  98. binary: true,
  99. headers:
  100. {
  101. "Content-Type": 'multipart/form-data; boundary=' + boundary,
  102. "Content-Length": data.length
  103. },
  104. data: data,
  105. onload: obj.callback
  106. };
  107. if (obj.override)
  108. {
  109. for (var attr in obj.override) request[attr] = obj.override[attr]
  110. }
  111. GM_xmlhttpRequest(request);
  112. }
  113.  
  114. BinaryRes.guessType = function(stream)
  115. {
  116. if (!stream) return;
  117. var dict =
  118. [
  119. [0, 3, '\xFF\xD8\xFF', 'image/jpeg'],
  120. [1, 4, 'PNG', 'image/png'],
  121. [0, 3, 'GIF', 'image/gif'],
  122. [0, 4, '%PDF', 'application/pdf'],
  123. ]
  124. stream = BinaryRes._clean(stream);
  125. for (var ii = 0; ii < dict.length; ii++)
  126. {
  127. if (stream.slice(dict[ii][0], dict[ii][1]) === dict[ii][2])
  128. return dict[ii][3];
  129. }
  130. }