saver.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. var a = document.createElement('a')
  75. name = name || blob.name || 'download'
  76. a.download = name
  77. a.rel = 'noopener' // tabnabbing
  78. // TODO: detect chrome extensions & packaged apps
  79. // a.target = '_blank'
  80. if (typeof blob === 'string') {
  81. // Support regular links
  82. a.href = blob
  83. if (a.origin !== location.origin) {
  84. corsEnabled(a.href)
  85. ? download(blob, name, opts)
  86. : click(a, a.target = '_blank')
  87. } else {
  88. click(a)
  89. }
  90. } else {
  91. // Support blobs
  92. a.href = URL.createObjectURL(blob)
  93. setTimeout(function () { URL.revokeObjectURL(a.href) }, 4E4) // 40s
  94. setTimeout(function () { click(a) }, 0)
  95. }
  96. }
  97. // Use msSaveOrOpenBlob as a second approach
  98. : 'msSaveOrOpenBlob' in navigator
  99. ? function saveAs(blob, name, opts) {
  100. name = name || blob.name || 'download'
  101. if (typeof blob === 'string') {
  102. if (corsEnabled(blob)) {
  103. download(blob, name, opts)
  104. } else {
  105. var a = document.createElement('a')
  106. a.href = blob
  107. a.target = '_blank'
  108. setTimeout(function () { click(a) })
  109. }
  110. } else {
  111. navigator.msSaveOrOpenBlob(bom(blob, opts), name)
  112. }
  113. }
  114. // Fallback to using FileReader and a popup
  115. : function saveAs(blob, name, opts, popup) {
  116. // Open a popup immediately do go around popup blocker
  117. // Mostly only available on user interaction and the fileReader is async so...
  118. popup = popup || open('', '_blank')
  119. if (popup) {
  120. popup.document.title =
  121. popup.document.body.innerText = 'downloading...'
  122. }
  123. if (typeof blob === 'string') return download(blob, name, opts)
  124. var force = blob.type === 'application/octet-stream'
  125. var isSafari = /constructor/i.test(_global.HTMLElement) || _global.safari
  126. var isChromeIOS = /CriOS\/[\d]+/.test(navigator.userAgent)
  127. if ((isChromeIOS || (force && isSafari) || isMacOSWebView) && typeof FileReader !== 'undefined') {
  128. // Safari doesn't allow downloading of blob URLs
  129. var reader = new FileReader()
  130. reader.onloadend = function () {
  131. var url = reader.result
  132. url = isChromeIOS ? url : url.replace(/^data:[^;]*;/, 'data:attachment/file;')
  133. if (popup) popup.location.href = url
  134. else location = url
  135. popup = null // reverse-tabnabbing #460
  136. }
  137. reader.readAsDataURL(blob)
  138. } else {
  139. var URL = _global.URL || _global.webkitURL
  140. var url = URL.createObjectURL(blob)
  141. if (popup) popup.location = url
  142. else location.href = url
  143. popup = null // reverse-tabnabbing #460
  144. setTimeout(function () { URL.revokeObjectURL(url) }, 4E4) // 40s
  145. }
  146. }
  147. )
  148. _global.saveAs = saveAs.saveAs = saveAs
  149. if (typeof module !== 'undefined') {
  150. module.exports = saveAs;
  151. }
  152. if (typeof jQuery !== 'undefined' && typeof saveAs !== 'undefined') {
  153. ; (function ($) {
  154. $.fn.wordExport = function (fileName) {
  155. fileName =
  156. typeof fileName !== 'undefined' ? fileName : 'jQuery-Word-Export'
  157. var static = {
  158. // mhtml: {
  159. // top:
  160. // 'Mime-Version: 1.0\nContent-Base: ' +
  161. // location.href +
  162. // '\nContent-Type: Multipart/related; boundary="NEXT.ITEM-BOUNDARY";type="text/html"\n\n--NEXT.ITEM-BOUNDARY\nContent-Type: text/html; charset="utf-8"\nContent-Location: ' +
  163. // location.href +
  164. // '\n\n<!DOCTYPE html>\n<html>\n_html_</html>',
  165. // head:
  166. // '<head>\n<meta http-equiv="Content-Type" content="text/html; charset=utf-8">\n<style>\n_styles_\n</style>\n</head>\n',
  167. // body: '<body>_body_</body>'
  168. // }
  169. mhtml: {
  170. top:
  171. 'Mime-Version: 1.0\nContent-Base: ' +
  172. location.href +
  173. '\nContent-Type: Multipart/related; boundary="NEXT.ITEM-BOUNDARY";type="text/html"\n\n--NEXT.ITEM-BOUNDARY\nContent-Type: text/html; charset="utf-8"\nContent-Location: ' +
  174. location.href +
  175. '\n\n<!DOCTYPE html>\n' +
  176. '<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">\n_html_</html>',
  177. head:
  178. '<head>\n<meta http-equiv="Content-Type" content="text/html; charset=utf-8">\n<style>\n_styles_\n</style>\n<!--[if gte mso 9]><xml><w:WordDocument><w:View>Print</w:View><w:TrackMoves>false</w:TrackMoves><w:TrackFormatting/><w:ValidateAgainstSchemas/><w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid><w:IgnoreMixedContent>false</w:IgnoreMixedContent><w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText><w:DoNotPromoteQF/><w:LidThemeOther>EN-US</w:LidThemeOther><w:LidThemeAsian>ZH-CN</w:LidThemeAsian><w:LidThemeComplexScript>X-NONE</w:LidThemeComplexScript><w:Compatibility><w:BreakWrappedTables/><w:SnapToGridInCell/><w:WrapTextWithPunct/><w:UseAsianBreakRules/><w:DontGrowAutofit/><w:SplitPgBreakAndParaMark/><w:DontVertAlignCellWithSp/><w:DontBreakConstrainedForcedTables/><w:DontVertAlignInTxbx/><w:Word11KerningPairs/><w:CachedColBalance/><w:UseFELayout/></w:Compatibility><w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel><m:mathPr><m:mathFont m:val="Cambria Math"/><m:brkBin m:val="before"/><m:brkBinSub m:val="--"/><m:smallFrac m:val="off"/><m:dispDef/><m:lMargin m:val="0"/> <m:rMargin m:val="0"/><m:defJc m:val="centerGroup"/><m:wrapIndent m:val="1440"/><m:intLim m:val="subSup"/><m:naryLim m:val="undOvr"/></m:mathPr></w:WordDocument></xml><![endif]--></head>\n',
  179. body: '<body>_body_</body>'
  180. }
  181. }
  182. var options = {
  183. maxWidth: 624
  184. }
  185. // Clone selected element before manipulating it
  186. var markup = $(this).clone()
  187. // Remove hidden elements from the output
  188. markup.each(function () {
  189. var self = $(this)
  190. if (self.is(':hidden')) self.remove()
  191. })
  192. // Embed all images using Data URLs
  193. var images = Array()
  194. var img = markup.find('img')
  195. for (var i = 0; i < img.length; i++) {
  196. // Calculate dimensions of output image
  197. var w = Math.min(img[i].width, options.maxWidth)
  198. var h = img[i].height * (w / img[i].width)
  199. // var w = '200'
  200. // var h = '100'
  201. // Create canvas for converting image to data URL
  202. var canvas = document.createElement('CANVAS')
  203. canvas.width = w
  204. canvas.height = h
  205. // Draw image to canvas
  206. var context = canvas.getContext('2d')
  207. context.drawImage(img[i], 0, 0, w, h)
  208. // Get data URL encoding of image
  209. var uri = canvas.toDataURL('image/png')
  210. $(img[i]).attr('src', img[i].src)
  211. img[i].width = w
  212. img[i].height = h
  213. // Save encoded image to array
  214. images[i] = {
  215. type: uri.substring(uri.indexOf(':') + 1, uri.indexOf(';')),
  216. encoding: uri.substring(uri.indexOf(';') + 1, uri.indexOf(',')),
  217. location: $(img[i]).attr('src'),
  218. data: uri.substring(uri.indexOf(',') + 1)
  219. }
  220. }
  221. // Prepare bottom of mhtml file with image data
  222. var mhtmlBottom = '\n'
  223. for (var i = 0; i < images.length; i++) {
  224. mhtmlBottom += '--NEXT.ITEM-BOUNDARY\n'
  225. mhtmlBottom += 'Content-Location: ' + images[i].location + '\n'
  226. mhtmlBottom += 'Content-Type: ' + images[i].type + '\n'
  227. mhtmlBottom +=
  228. 'Content-Transfer-Encoding: ' + images[i].encoding + '\n\n'
  229. mhtmlBottom += images[i].data + '\n\n'
  230. }
  231. mhtmlBottom += '--NEXT.ITEM-BOUNDARY--'
  232. //TODO: load css from included stylesheet
  233. var styles = ''
  234. // Aggregate parts of the file together
  235. var fileContent =
  236. static.mhtml.top.replace(
  237. '_html_',
  238. static.mhtml.head.replace('_styles_', styles) +
  239. static.mhtml.body.replace('_body_', markup.html())
  240. ) + mhtmlBottom
  241. // Create a Blob with the file contents
  242. var blob = new Blob([fileContent], {
  243. type: 'application/msword;charset=utf-8'
  244. })
  245. saveAs(blob, fileName + '.doc')
  246. }
  247. })(jQuery)
  248. } else {
  249. if (typeof jQuery === 'undefined') {
  250. console.error('jQuery Word Export: missing dependency (jQuery)')
  251. }
  252. if (typeof saveAs === 'undefined') {
  253. console.error('jQuery Word Export: missing dependency (FileSaver.js)')
  254. }
  255. }