save object as file dev tools

Solutions on MaxInterview for save object as file dev tools by the best coders in the world

showing results for - "save object as file dev tools"
Noha
11 Nov 2017
1function saveDataAsFile(data, filename = "browser-scrapped.json") {
2    if (!data) { console.error('Console.save: No data'); return; }
3
4    if (typeof data === "object") { data = JSON.stringify(data, undefined, 4); }
5
6    var blob = new Blob([data], {type: 'text/json'}),
7    e = document.createEvent('MouseEvents'),
8    a = document.createElement('a');
9
10    a.download = filename;
11    a.href = window.URL.createObjectURL(blob);
12    a.dataset.downloadurl = ['text/json', a.download, a.href].join(':');
13    e.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
14    a.dispatchEvent(e);
15}
16
17
18
19
20
21
22// Example: Extracts some data from page and save it as a file on the hard drive
23function scrappePage() {
24    const currentURL = window.location.href;
25    const dataToSave = { url: currentURL };
26  
27    dataToSave.links = Array.from(document.querySelectorAll(".mfr-part-num a")).map(item => item.href);
28
29    return dataToSave;
30}
31var scrappePageAndDownload = () => saveDataAsFile(scrappePage());
32scrappePageAndDownload();
33