download.js

downlaods files

بۇ قوليازمىنى بىۋاسىتە قاچىلاشقا بولمايدۇ. بۇ باشقا قوليازمىلارنىڭ ئىشلىتىشى ئۈچۈن تەمىنلەنگەن ئامبار بولۇپ، ئىشلىتىش ئۈچۈن مېتا كۆرسەتمىسىگە قىستۇرىدىغان كود: // @require https://update.greatest.deepsurf.us/scripts/381311/685821/downloadjs.js

  1. (function (root, factory) {
  2. if (typeof define === 'function' && define.amd) {
  3. // AMD. Register as an anonymous module.
  4. define([], factory);
  5. } else if (typeof exports === 'object') {
  6. // Node. Does not work with strict CommonJS, but
  7. // only CommonJS-like environments that support module.exports,
  8. // like Node.
  9. module.exports = factory();
  10. } else {
  11. // Browser globals (root is window)
  12. root.download = factory();
  13. }
  14. }(this, function () {
  15.  
  16. return function download(data, strFileName, strMimeType) {
  17.  
  18. var self = window, // this script is only for browsers anyway...
  19. defaultMime = "application/octet-stream", // this default mime also triggers iframe downloads
  20. mimeType = strMimeType || defaultMime,
  21. payload = data,
  22. url = !strFileName && !strMimeType && payload,
  23. anchor = document.createElement("a"),
  24. toString = function (a) { return String(a); },
  25. myBlob = (self.Blob || self.MozBlob || self.WebKitBlob || toString),
  26. fileName = strFileName || "download",
  27. blob,
  28. reader;
  29. myBlob = myBlob.call ? myBlob.bind(self) : Blob;
  30.  
  31. if (String(this) === "true") { //reverse arguments, allowing download.bind(true, "text/xml", "export.xml") to act as a callback
  32. payload = [payload, mimeType];
  33. mimeType = payload[0];
  34. payload = payload[1];
  35. }
  36.  
  37.  
  38. if (url && url.length < 2048) { // if no filename and no mime, assume a url was passed as the only argument
  39. fileName = url.split("/").pop().split("?")[0];
  40. anchor.href = url; // assign href prop to temp anchor
  41. if (anchor.href.indexOf(url) !== -1) { // if the browser determines that it's a potentially valid url path:
  42. var ajax = new XMLHttpRequest();
  43. ajax.open("GET", url, true);
  44. ajax.responseType = 'blob';
  45. ajax.onload = function (e) {
  46. download(e.target.response, fileName, defaultMime);
  47. };
  48. setTimeout(function () { ajax.send(); }, 0); // allows setting custom ajax headers using the return:
  49. return ajax;
  50. } // end if valid url?
  51. } // end if url?
  52.  
  53.  
  54. //go ahead and download dataURLs right away
  55. if (/^data\:[\w+\-]+\/[\w+\-]+[,;]/.test(payload)) {
  56.  
  57. if (payload.length > (1024 * 1024 * 1.999) && myBlob !== toString) {
  58. payload = dataUrlToBlob(payload);
  59. mimeType = payload.type || defaultMime;
  60. } else {
  61. return navigator.msSaveBlob ? // IE10 can't do a[download], only Blobs:
  62. navigator.msSaveBlob(dataUrlToBlob(payload), fileName) :
  63. saver(payload); // everyone else can save dataURLs un-processed
  64. }
  65.  
  66. }//end if dataURL passed?
  67.  
  68. blob = payload instanceof myBlob ?
  69. payload :
  70. new myBlob([payload], { type: mimeType });
  71.  
  72.  
  73. function dataUrlToBlob(strUrl) {
  74. var parts = strUrl.split(/[:;,]/),
  75. type = parts[1],
  76. decoder = parts[2] == "base64" ? atob : decodeURIComponent,
  77. binData = decoder(parts.pop()),
  78. mx = binData.length,
  79. i = 0,
  80. uiArr = new Uint8Array(mx);
  81.  
  82. for (i; i < mx; ++i) uiArr[i] = binData.charCodeAt(i);
  83.  
  84. return new myBlob([uiArr], { type: type });
  85. }
  86.  
  87. function saver(url, winMode) {
  88.  
  89. if ('download' in anchor) { //html5 A[download]
  90. anchor.href = url;
  91. anchor.setAttribute("download", fileName);
  92. anchor.className = "download-js-link";
  93. anchor.innerHTML = "downloading...";
  94. anchor.style.display = "none";
  95. document.body.appendChild(anchor);
  96. setTimeout(function () {
  97. anchor.click();
  98. document.body.removeChild(anchor);
  99. if (winMode === true) { setTimeout(function () { self.URL.revokeObjectURL(anchor.href); }, 250); }
  100. }, 66);
  101. return true;
  102. }
  103.  
  104. // handle non-a[download] safari as best we can:
  105. if (/(Version)\/(\d+)\.(\d+)(?:\.(\d+))?.*Safari\//.test(navigator.userAgent)) {
  106. url = url.replace(/^data:([\w\/\-\+]+)/, defaultMime);
  107. if (!window.open(url)) { // popup blocked, offer direct download:
  108. if (confirm("Displaying New Document\n\nUse Save As... to download, then click back to return to this page.")) { location.href = url; }
  109. }
  110. return true;
  111. }
  112.  
  113. //do iframe dataURL download (old ch+FF):
  114. var f = document.createElement("iframe");
  115. document.body.appendChild(f);
  116.  
  117. if (!winMode) { // force a mime that will download:
  118. url = "data:" + url.replace(/^data:([\w\/\-\+]+)/, defaultMime);
  119. }
  120. f.src = url;
  121. setTimeout(function () { document.body.removeChild(f); }, 333);
  122.  
  123. }//end saver
  124.  
  125.  
  126.  
  127.  
  128. if (navigator.msSaveBlob) { // IE10+ : (has Blob, but not a[download] or URL)
  129. return navigator.msSaveBlob(blob, fileName);
  130. }
  131.  
  132. if (self.URL) { // simple fast and modern way using Blob and URL:
  133. saver(self.URL.createObjectURL(blob), true);
  134. } else {
  135. // handle non-Blob()+non-URL browsers:
  136. if (typeof blob === "string" || blob.constructor === toString) {
  137. try {
  138. return saver("data:" + mimeType + ";base64," + self.btoa(blob));
  139. } catch (y) {
  140. return saver("data:" + mimeType + "," + encodeURIComponent(blob));
  141. }
  142. }
  143.  
  144. // Blob but not URL support:
  145. reader = new FileReader();
  146. reader.onload = function (e) {
  147. saver(this.result);
  148. };
  149. reader.readAsDataURL(blob);
  150. }
  151. return true;
  152. }; /* end download() */
  153. }));