-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
119 lines (93 loc) · 3.03 KB
/
index.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
var allCommands;
var downloadButton = document.getElementById('download');
(function (w) {
(async () => {
document.getElementById('file-selector').addEventListener('change', getFile)
downloadButton.addEventListener('click', save)
downloadButton.style.display = 'none';
})();
})(window);
function hide(el) {
el.style.display = 'none';
}
function show(el, value) {
el.style.display = value;
}
function removeDuplicates(commands) {
commands.filter(function (value, index, array) {
return array.indexOf(value) === index;
});
return commands;
}
function getFile(event) {
const input = event.target
if ('files' in input && input.files.length > 0) {
placeFileContent(
document.getElementById('content-target'),
input.files[0])
}
}
function placeFileContent(target, file) {
readFileContent(file).then(content => {
// target.value = content // in case you want to load it on the view
generateRows(content);
}).catch(error => console.log(error))
}
function readFileContent(file) {
const reader = new FileReader()
return new Promise((resolve, reject) => {
reader.onload = event => resolve(event.target.result)
reader.onerror = error => reject(error)
reader.readAsText(file)
})
}
function generateRows(text) {
downloadButton.style.display = 'none';
const output = document.getElementById('output');
// const text = await (await fetch("commands2.hive")).text();
result = text.split('\u0000');
var commands = [];
result.forEach(row => {
if (row.substring(0, 2) == '{"') {
row = row.replace(/[\x00-\x08\x0E-\x1F\x7F-\uFFFF]/g, '');
row = row.replace('":"\"/', '":"');
row = row.replace('==\""', '=="');
row = row.replace('=\""', '=\"');
commands.push(row);
}
});
console.log('All Commands: ', commands.length)
/// Remove duplicates
commands = removeDuplicates(commands)
console.log('All Commands (No duplicates): ', commands.length)
commands.forEach(el => {
console.log(el);
});
allCommands = commands;
commands.forEach(element => {
const li = document.createElement('li');
output.appendChild(li);
json = hljs.highlight(element, {
language: 'json'
}).value;
li.innerHTML = '<pre><code class="language-json">' + json + '</code></pre>';
});
hljs.highlightAll();
downloadButton.style.display = 'block';
}
function save() {
var filename = 'commands.json';
const blob = new Blob(["[" + allCommands + "]"], {
type: 'text/json'
});
if (window.navigator.msSaveOrOpenBlob) {
window.navigator.msSaveBlob(blob, filename);
} else {
const elem = window.document.createElement('a');
elem.href = window.URL.createObjectURL(blob);
elem.download = filename;
document.body.appendChild(elem);
elem.click();
document.body.removeChild(elem);
}
}