PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 6.1
MainWP Dashboard: Self-hosted WordPress Management for Agencies v6.1
6.2 6.1.8 6.1.7 6.1.6 6.1.5 6.1.4 6.1.3 6.1.2 6.1.1 6.1 6.0.12 6.0.11 4.6.0.1 5.0 5.0.1 5.0.2 5.0.3 5.0.3.1 5.0.3.2 5.1 5.1.1 5.2 5.2.1 5.2.2 5.3 All 153 releases
mainwp / assets / js / FileSaver.js

FileSaver.js in MainWP Dashboard: Self-hosted WordPress Management for Agencies 6.1, at assets/js/FileSaver.js

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