mirror of
https://github.com/angular/angular
synced 2026-05-24 09:28:37 +00:00
refactor(devtools): implement programatic JSON download logic to replace dependency on filesaver library
This commit is contained in:
parent
aa0585b35f
commit
bec8a2cd1b
3 changed files with 5 additions and 95 deletions
|
|
@ -1,6 +1,5 @@
|
|||
import { Subject } from 'rxjs';
|
||||
import { Injectable } from '@angular/core';
|
||||
import { saveAs } from './vendor/filesaver';
|
||||
import { toISO8601Compact } from './vendor/chromium/date-utilities';
|
||||
|
||||
@Injectable({
|
||||
|
|
@ -23,7 +22,10 @@ export class FileApiService {
|
|||
}
|
||||
|
||||
saveObjectAsJSON(object: object): void {
|
||||
const blob = new Blob([JSON.stringify(object)], { type: 'application/json' });
|
||||
saveAs(blob, `NgDevTools-Profile-${toISO8601Compact(new Date())}.json`);
|
||||
const downloadLink = document.createElement('a');
|
||||
downloadLink.download = `NgDevTools-Profile-${toISO8601Compact(new Date())}.json`;
|
||||
downloadLink.href = URL.createObjectURL(new Blob([JSON.stringify(object)], { type: 'application/json' }));
|
||||
downloadLink.click();
|
||||
setTimeout(() => URL.revokeObjectURL(downloadLink.href));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,11 +0,0 @@
|
|||
The MIT License
|
||||
|
||||
Copyright © 2016 [Eli Grey][1].
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
[1]: http://eligrey.com
|
||||
|
|
@ -1,81 +0,0 @@
|
|||
// tslint:disable
|
||||
/*
|
||||
* FileSaver.js
|
||||
* A saveAs() FileSaver implementation.
|
||||
*
|
||||
* By Eli Grey, http://eligrey.com
|
||||
*
|
||||
* License : https://github.com/eligrey/FileSaver.js/blob/master/LICENSE.md (MIT)
|
||||
* source : http://purl.eligrey.com/github/FileSaver.js
|
||||
*/
|
||||
|
||||
// The one and only way of getting global scope in all environments
|
||||
// https://stackoverflow.com/q/3277182/1008999
|
||||
var _global = globalThis;
|
||||
|
||||
function download(url, name) {
|
||||
var xhr = new XMLHttpRequest();
|
||||
xhr.open('GET', url);
|
||||
xhr.responseType = 'blob';
|
||||
xhr.onload = function () {
|
||||
saveAs(xhr.response, name);
|
||||
};
|
||||
xhr.onerror = function () {
|
||||
console.error('could not download file');
|
||||
};
|
||||
xhr.send();
|
||||
}
|
||||
|
||||
function corsEnabled(url) {
|
||||
var xhr = new XMLHttpRequest();
|
||||
// use sync to avoid popup blocker
|
||||
xhr.open('HEAD', url, false);
|
||||
try {
|
||||
xhr.send();
|
||||
} catch (e) {}
|
||||
return xhr.status >= 200 && xhr.status <= 299;
|
||||
}
|
||||
|
||||
// `a.click()` doesn't work for all browsers (#465)
|
||||
function click(node, _?) {
|
||||
try {
|
||||
node.dispatchEvent(new MouseEvent('click'));
|
||||
} catch (e) {
|
||||
var evt = document.createEvent('MouseEvents');
|
||||
evt.initMouseEvent('click', true, true, window, 0, 0, 0, 80, 20, false, false, false, false, 0, null);
|
||||
node.dispatchEvent(evt);
|
||||
}
|
||||
}
|
||||
|
||||
function saveAs(blob, name) {
|
||||
var URL = _global.URL || _global.webkitURL;
|
||||
var a = document.createElement('a');
|
||||
name = name || blob.name || 'download';
|
||||
|
||||
a.download = name;
|
||||
a.rel = 'noopener'; // tabnabbing
|
||||
|
||||
// TODO: detect chrome extensions & packaged apps
|
||||
// a.target = '_blank'
|
||||
|
||||
if (typeof blob === 'string') {
|
||||
// Support regular links
|
||||
a.href = blob;
|
||||
if (a.origin !== location.origin) {
|
||||
corsEnabled(a.href) ? download(blob, name) : click(a, (a.target = '_blank'));
|
||||
} else {
|
||||
click(a);
|
||||
}
|
||||
} else {
|
||||
// Support blobs
|
||||
a.href = URL.createObjectURL(blob);
|
||||
setTimeout(function () {
|
||||
URL.revokeObjectURL(a.href);
|
||||
}, 4e4); // 40s
|
||||
setTimeout(function () {
|
||||
click(a);
|
||||
}, 0);
|
||||
}
|
||||
}
|
||||
|
||||
export { saveAs };
|
||||
Loading…
Reference in a new issue