Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

173 рядки
5.6 KiB

  1. /*
  2. * FileSaver.js
  3. * A saveAs() FileSaver implementation.
  4. *
  5. * By Eli Grey, http://eligrey.com
  6. *
  7. * License : https://github.com/eligrey/FileSaver.js/blob/master/LICENSE.md (MIT)
  8. * source : http://purl.eligrey.com/github/FileSaver.js
  9. */
  10. // The one and only way of getting global scope in all environments
  11. // https://stackoverflow.com/q/3277182/1008999
  12. var _global = typeof window === 'object' && window.window === window
  13. ? window : typeof self === 'object' && self.self === self
  14. ? self : typeof global === 'object' && global.global === global
  15. ? global
  16. : this
  17. function bom (blob, opts) {
  18. if (typeof opts === 'undefined') opts = { autoBom: false }
  19. else if (typeof opts !== 'object') {
  20. console.warn('Deprecated: Expected third argument to be a object')
  21. opts = { autoBom: !opts }
  22. }
  23. // prepend BOM for UTF-8 XML and text/* types (including HTML)
  24. // note: your browser will automatically convert UTF-16 U+FEFF to EF BB BF
  25. if (opts.autoBom && /^\s*(?:text\/\S*|application\/xml|\S*\/\S*\+xml)\s*;.*charset\s*=\s*utf-8/i.test(blob.type)) {
  26. return new Blob([String.fromCharCode(0xFEFF), blob], { type: blob.type })
  27. }
  28. return blob
  29. }
  30. function download (url, name, opts) {
  31. var xhr = new XMLHttpRequest()
  32. xhr.open('GET', url)
  33. xhr.responseType = 'blob'
  34. xhr.onload = function () {
  35. saveAs(xhr.response, name, opts)
  36. }
  37. xhr.onerror = function () {
  38. console.error('could not download file')
  39. }
  40. xhr.send()
  41. }
  42. function corsEnabled (url) {
  43. var xhr = new XMLHttpRequest()
  44. // use sync to avoid popup blocker
  45. xhr.open('HEAD', url, false)
  46. try {
  47. xhr.send()
  48. } catch (e) {}
  49. return xhr.status >= 200 && xhr.status <= 299
  50. }
  51. // `a.click()` doesn't work for all browsers (#465)
  52. function click (node) {
  53. try {
  54. node.dispatchEvent(new MouseEvent('click'))
  55. } catch (e) {
  56. var evt = document.createEvent('MouseEvents')
  57. evt.initMouseEvent('click', true, true, window, 0, 0, 0, 80,
  58. 20, false, false, false, false, 0, null)
  59. node.dispatchEvent(evt)
  60. }
  61. }
  62. // Detect WebView inside a native macOS app by ruling out all browsers
  63. // We just need to check for 'Safari' because all other browsers (besides Firefox) include that too
  64. // https://www.whatismybrowser.com/guides/the-latest-user-agent/macos
  65. var isMacOSWebView = _global.navigator && /Macintosh/.test(navigator.userAgent) && /AppleWebKit/.test(navigator.userAgent) && !/Safari/.test(navigator.userAgent)
  66. var saveAs = _global.saveAs || (
  67. // probably in some web worker
  68. (typeof window !== 'object' || window !== _global)
  69. ? function saveAs () { /* noop */ }
  70. // Use download attribute first if possible (#193 Lumia mobile) unless this is a macOS WebView
  71. : ('download' in HTMLAnchorElement.prototype && !isMacOSWebView)
  72. ? function saveAs (blob, name, opts) {
  73. var URL = _global.URL || _global.webkitURL
  74. // Namespace is used to prevent conflict w/ Chrome Poper Blocker extension (Issue #561)
  75. var a = document.createElementNS('http://www.w3.org/1999/xhtml', 'a')
  76. name = name || blob.name || 'download'
  77. a.download = name
  78. a.rel = 'noopener' // tabnabbing
  79. // TODO: detect chrome extensions & packaged apps
  80. // a.target = '_blank'
  81. if (typeof blob === 'string') {
  82. // Support regular links
  83. a.href = blob
  84. if (a.origin !== location.origin) {
  85. corsEnabled(a.href)
  86. ? download(blob, name, opts)
  87. : click(a, a.target = '_blank')
  88. } else {
  89. click(a)
  90. }
  91. } else {
  92. // Support blobs
  93. a.href = URL.createObjectURL(blob)
  94. setTimeout(function () { URL.revokeObjectURL(a.href) }, 4E4) // 40s
  95. setTimeout(function () { click(a) }, 0)
  96. }
  97. }
  98. // Use msSaveOrOpenBlob as a second approach
  99. : 'msSaveOrOpenBlob' in navigator
  100. ? function saveAs (blob, name, opts) {
  101. name = name || blob.name || 'download'
  102. if (typeof blob === 'string') {
  103. if (corsEnabled(blob)) {
  104. download(blob, name, opts)
  105. } else {
  106. var a = document.createElement('a')
  107. a.href = blob
  108. a.target = '_blank'
  109. setTimeout(function () { click(a) })
  110. }
  111. } else {
  112. navigator.msSaveOrOpenBlob(bom(blob, opts), name)
  113. }
  114. }
  115. // Fallback to using FileReader and a popup
  116. : function saveAs (blob, name, opts, popup) {
  117. // Open a popup immediately do go around popup blocker
  118. // Mostly only available on user interaction and the fileReader is async so...
  119. popup = popup || open('', '_blank')
  120. if (popup) {
  121. popup.document.title =
  122. popup.document.body.innerText = 'downloading...'
  123. }
  124. if (typeof blob === 'string') return download(blob, name, opts)
  125. var force = blob.type === 'application/octet-stream'
  126. var isSafari = /constructor/i.test(_global.HTMLElement) || _global.safari
  127. var isChromeIOS = /CriOS\/[\d]+/.test(navigator.userAgent)
  128. if ((isChromeIOS || (force && isSafari) || isMacOSWebView) && typeof FileReader !== 'undefined') {
  129. // Safari doesn't allow downloading of blob URLs
  130. var reader = new FileReader()
  131. reader.onloadend = function () {
  132. var url = reader.result
  133. url = isChromeIOS ? url : url.replace(/^data:[^;]*;/, 'data:attachment/file;')
  134. if (popup) popup.location.href = url
  135. else location = url
  136. popup = null // reverse-tabnabbing #460
  137. }
  138. reader.readAsDataURL(blob)
  139. } else {
  140. var URL = _global.URL || _global.webkitURL
  141. var url = URL.createObjectURL(blob)
  142. if (popup) popup.location = url
  143. else location.href = url
  144. popup = null // reverse-tabnabbing #460
  145. setTimeout(function () { URL.revokeObjectURL(url) }, 4E4) // 40s
  146. }
  147. }
  148. )
  149. _global.saveAs = saveAs.saveAs = saveAs
  150. if (typeof module !== 'undefined') {
  151. module.exports = saveAs;
  152. }