-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.js
203 lines (135 loc) · 4.8 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
const { app, clipboard, dialog, shell } = require('electron')
const { mkdirSync, writeFileSync, existsSync } = require('fs');
const mimeDB = require('mime-db');
const config = require('./config/config.json');
app.whenReady().then(() => {
try {
// Main Thread
main();
} catch (error) {
// Unknown error in the main thread
dialog.showErrorBox("", "There was a problem while extracting the data!\n" + error);
app.quit();
return;
}
});
/* Main Thread */
async function main() {
try {
// Grab HAR data from the clipboard.
var clipboardData = clipboard.readText();
var { entries: harEntries, title: harTitle } = await extractHarData(clipboardData);
// The provided data did not match the HAR-JSON structure
} catch (error) {
dialog.showErrorBox("", "There was no valid HAR data in your clipboard!");
app.quit();
return;
}
// Select base path for the extraction
var baseFolder = (await dialog.showOpenDialog({ properties: ['openDirectory'], buttonLabel: "Extract here" }))["filePaths"][0];
// Cancel was clicked
if (!baseFolder) {
app.quit();
return;
}
var folder = `${baseFolder}/${harTitle}`;
mkdirSync(folder, { recursive: true });
// Await the extraction of all files
await Promise.all(harEntries.map(async (entry) => {
if (entry.data) {
let buffer = Buffer.from(entry.data, entry.encoding);
return writeFileSync(`${folder}/${entry.fileName}`, buffer);
}
}));
// Open created folder with explorer
if (existsSync(folder)) shell.openPath(folder);
app.quit();
return;
/* Functions */
/*
Main extraction function
*/
async function extractHarData(data) {
// Get root of HAR data
var harData = (JSON.parse(String(data)))["log"];
// Get HAR title and remove illegal characters from it
var harTitle = (harData["pages"][0]["title"] || config.defaultFolderName).replace(/(\W+)/gi, '-');
// Reduce entries to base information
var parsedHarData = harData["entries"].map((entry) => {
var content = entry["response"]["content"];
// Get filename from url without extension
var name = (new URL(entry["request"]["url"]).pathname.split('/').pop()) || config.defaultFileName;
if (name.includes(".")) name = name.substring(0, name.lastIndexOf("."));
// Get MIME type
var mimeType = fixMimeTypes((content["mimeType"] || config.defaultMimeType).split(';').shift());
var mimeLookup = mimeDB[mimeType];
// Get MIME extension
var extension = (mimeLookup) ? mimeLookup.extensions[0] : config.defaultExtension;
var fileName = `${name}.${extension}`;
// Get HAR entry's data
var data = content["text"];
// Get HAR entry's encoding
var encoding = content["encoding"] || config.defaultEncoding;
return ({
name: name,
fileName: fileName,
type: mimeType,
extension: extension,
encoding: encoding,
data: data,
})
});
// Suffixing items with multiple occurrences
var nameArray = parsedHarData.map((entry) => entry.fileName);
var suffixArray = createSuffixArray(nameArray);
var suffixedHarEntries = parsedHarData.map((entry, index) => (
{
...entry,
fileName: `${entry.name}${suffixArray[index] || ""}${(entry.extension) ? `.${entry.extension}` : `${config.defaultExtension}`}`
}
));
return {
title: harTitle,
entries: suffixedHarEntries
};
}
/*
Helper function to fix export bugs caused by obsolete MIME types
*/
function fixMimeTypes(type) {
let workingType = type;
// Replace obsolete MIME-Types with fixed types
config.obsoleteMimeTypes.forEach(obsoleteType => {
workingType = workingType.replace(obsoleteType[0], obsoleteType[1]);
});
let fixedType = workingType;
return fixedType;
}
/*
Helper function to suffix duplicates in an array - returns an array containing only suffixes
*/
function createSuffixArray(arrayWithoutSuffixes) {
var list = arrayWithoutSuffixes;
var suffixArray = [];
// Objects to collect occurrences
var count = {};
var firstOccurrences = {};
var item, itemCount;
// Loop through the list
for (var i = 0, c = list.length; i < c; i++) {
item = list[i];
itemCount = count[item];
itemCount = count[item] = (itemCount == null ? 1 : itemCount + 1);
if (itemCount == 2)
// First occurrence of an item which has multiple occurrences
suffixArray[firstOccurrences[item]] = `-(1)`;
if (count[item] > 1)
// All other occurrences of an item which has multiple occurrences
suffixArray[i] = `-(${count[item]})`;
else
// Item without any other occurrences
firstOccurrences[item] = i;
}
return suffixArray;
}
}