funcelot

io.js

Feb 6th, 2020
709
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // `a.click()` doesn't work for all browsers (#465)
  2.  
  3. // The one and only way of getting global scope in all environments
  4. // https://stackoverflow.com/q/3277182/1008999
  5. var _global =
  6.   typeof window === "object" && window.window === window
  7.     ? window
  8.     : typeof window.self === "object" && window.self.self === window.self
  9.     ? window.self
  10.     : typeof global === "object" && global.global === global
  11.     ? global
  12.     : this;
  13. function bom(blob, opts) {
  14.   if (typeof opts === "undefined") opts = { autoBom: false };
  15.   else if (typeof opts !== "object") {
  16.     console.warn("Deprecated: Expected third argument to be a object");
  17.     opts = { autoBom: !opts };
  18.   }
  19.   // prepend BOM for UTF-8 XML and text/* types (including HTML)
  20.   // note: your browser will automatically convert UTF-16 U+FEFF to EF BB BF
  21.   if (
  22.     opts.autoBom &&
  23.     /^\s*(?:text\/\S*|application\/xml|\S*\/\S*\+xml)\s*;.*charset\s*=\s*utf-8/i.test(
  24.       blob.type
  25.     )
  26.   ) {
  27.     return new Blob([String.fromCharCode(0xfeff), blob], { type: blob.type });
  28.   }
  29.   return blob;
  30. }
  31. function download(url, name, opts) {
  32.   var xhr = new XMLHttpRequest();
  33.   xhr.open("GET", url);
  34.   xhr.responseType = "blob";
  35.   xhr.onload = function() {
  36.     saveAs(xhr.response, name, opts);
  37.   };
  38.   xhr.onerror = function() {
  39.     console.error("could not download file");
  40.   };
  41.   xhr.send();
  42. }
  43.  
  44. function corsEnabled(url) {
  45.   var xhr = new XMLHttpRequest();
  46.   // use sync to avoid popup blocker
  47.   xhr.open("HEAD", url, false);
  48.   try {
  49.     xhr.send();
  50.   } catch (e) {}
  51.   return xhr.status >= 200 && xhr.status <= 299;
  52. }
  53.  
  54. export function click(node) {
  55.   try {
  56.     node.dispatchEvent(new MouseEvent("click"));
  57.   } catch (e) {
  58.     var evt = document.createEvent("MouseEvents");
  59.     evt.initMouseEvent(
  60.       "click",
  61.       true,
  62.       true,
  63.       window,
  64.       0,
  65.       0,
  66.       0,
  67.       80,
  68.       20,
  69.       false,
  70.       false,
  71.       false,
  72.       false,
  73.       0,
  74.       null
  75.     );
  76.     node.dispatchEvent(evt);
  77.   }
  78. }
  79.  
  80. export var saveAs =
  81.   _global.saveAs ||
  82.   // probably in some web worker
  83.   (typeof window !== "object" || window !== _global
  84.     ? function saveAs() {
  85.         /* noop */
  86.       }
  87.     : // Use download attribute first if possible (#193 Lumia mobile)
  88.     "download" in HTMLAnchorElement.prototype
  89.     ? function saveAs(blob, name, opts) {
  90.         var URL = _global.URL || _global.webkitURL;
  91.         var a = document.createElement("a");
  92.         name = name || blob.name || "download";
  93.         a.download = name;
  94.         a.rel = "noopener"; // tabnabbing
  95.         // TODO: detect chrome extensions & packaged apps
  96.         // a.target = '_blank'
  97.         if (typeof blob === "string") {
  98.           // Support regular links
  99.           a.href = blob;
  100.           if (a.origin !== window.location.origin) {
  101.             corsEnabled(a.href)
  102.               ? download(blob, name, opts)
  103.               : click(a, (a.target = "_blank"));
  104.           } else {
  105.             click(a);
  106.           }
  107.         } else {
  108.           // Support blobs
  109.           a.href = URL.createObjectURL(blob);
  110.           setTimeout(function() {
  111.             URL.revokeObjectURL(a.href);
  112.           }, 4e4); // 40s
  113.           setTimeout(function() {
  114.             click(a);
  115.           }, 0);
  116.         }
  117.       }
  118.     : // Use msSaveOrOpenBlob as a second approach
  119.     "msSaveOrOpenBlob" in navigator
  120.     ? function saveAs(blob, name, opts) {
  121.         name = name || blob.name || "download";
  122.         if (typeof blob === "string") {
  123.           if (corsEnabled(blob)) {
  124.             download(blob, name, opts);
  125.           } else {
  126.             var a = document.createElement("a");
  127.             a.href = blob;
  128.             a.target = "_blank";
  129.             setTimeout(function() {
  130.               click(a);
  131.             });
  132.           }
  133.         } else {
  134.           navigator.msSaveOrOpenBlob(bom(blob, opts), name);
  135.         }
  136.       }
  137.     : // Fallback to using FileReader and a popup
  138.       function saveAs(blob, name, opts, popup) {
  139.         // Open a popup immediately do go around popup blocker
  140.         // Mostly only available on user interaction and the fileReader is async so...
  141.         popup = popup || window.open("", "_blank");
  142.         if (popup) {
  143.           popup.document.title = popup.document.body.innerText =
  144.             "downloading...";
  145.         }
  146.         if (typeof blob === "string") return download(blob, name, opts);
  147.         var force = blob.type === "application/octet-stream";
  148.         var isSafari =
  149.           /constructor/i.test(_global.HTMLElement) || _global.safari;
  150.         var isChromeIOS = /CriOS\/[\d]+/.test(navigator.userAgent);
  151.         if (
  152.           (isChromeIOS || (force && isSafari)) &&
  153.           typeof FileReader !== "undefined"
  154.         ) {
  155.           // Safari doesn't allow downloading of blob URLs
  156.           var reader = new FileReader();
  157.           reader.onloadend = function() {
  158.             var url = reader.result;
  159.             url = isChromeIOS
  160.               ? url
  161.               : url.replace(/^data:[^;]*;/, "data:attachment/file;");
  162.             if (popup) popup.location.href = url;
  163.             else window.location = url;
  164.             popup = null; // reverse-tabnabbing #460
  165.           };
  166.           reader.readAsDataURL(blob);
  167.         } else {
  168.           var URL = _global.URL || _global.webkitURL;
  169.           var url = URL.createObjectURL(blob);
  170.           if (popup) popup.location = url;
  171.           else window.location.href = url;
  172.           popup = null; // reverse-tabnabbing #460
  173.           setTimeout(function() {
  174.             URL.revokeObjectURL(url);
  175.           }, 4e4); // 40s
  176.         }
  177.       });
  178. _global.saveAs = saveAs.saveAs = saveAs;
Advertisement
Add Comment
Please, Sign In to add comment