-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
169 lines (155 loc) · 6.14 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
let pageSize = 10; // alterable for whatever reason you like
var pageNum = 0;
var capacity = 0;
var pageCount = 0;
function TableRow() {
this.id = "N/A";
this.type = "N/A";
this.error = "N/A";
this.impact = "Unset";
this.date = "N/A";
return this;
}
function storeCVE(cveEntry) {
// console.log(cveEntry);
if (cveEntry == undefined)
return "";
var retval = TableRow();
retval.id = cveEntry.cve.CVE_data_meta.ID;
retval.type = (cveEntry.cve.problemtype.problemtype_data[0].description[0] == undefined) ? " " : cveEntry.cve.problemtype.problemtype_data[0].description[0].value;
retval.error = cveEntry.cve.description.description_data[0].value;
retval.impact = (cveEntry.impact.baseMetricV3 == undefined) ? "Unset" : cveEntry.impact.baseMetricV3.impactScore;
retval.date = cveEntry.lastModifiedDate;
return retval;
}
function generateTable() {
// ok so
// open the file
fetch("nvdcve-1.1-recent.json")
.then(response => response.json())
.then((data) => {
// console.log(data.CVE_Items[0].cve);
capacity = data.CVE_data_numberOfCVEs;
pageCount = ~~(capacity/pageSize) + 1;
// console.log(capacity/10);
var table = document.getElementById("cveTable");
var selector = document.getElementById("selector");
// get the first ten entries of the file
for (var i = 0; i < pageSize; i++) {
// setup the row
var row = table.insertRow(i+1);
// turn each entry into a table row
var rowVals = storeCVE(data.CVE_Items[i]);
// with some tweaking of TableRow this could just be another loop but I don't know if that's worth it?
// reduces readability anyways unless I make an enum
row.insertCell(0).innerHTML = rowVals.id;
row.insertCell(1).innerHTML = rowVals.type;
row.insertCell(2).innerHTML = rowVals.error;
row.insertCell(3).innerHTML = rowVals.impact;
row.insertCell(4).innerHTML = rowVals.date;
}
if (pageCount > 1) {
document.getElementById("nextPage").removeAttribute("disabled");
document.getElementById("lastPage").removeAttribute("disabled");
}
// some extra stuff to make the dropdown selector the right length
// code basically copied from https://stackoverflow.com/questions/18417114/add-item-to-dropdown-list-in-html-using-javascript
// not necessarily a hard thing to come up with but I figured I should note my sources anyway
for (var j = 0; j < pageCount; j++) {
var option = document.createElement('option');
option.text = option.value = j + 1;
selector.add(option, j);
}
})
}
function addPage(size, page) {
// basically emulating old generateTable() behavior but paring it down to a specific length
let offset = page*size;
fetch("nvdcve-1.1-recent.json")
.then(response => response.json())
.then((data) => {
// console.log(data.CVE_Items[0].cve);
var table = document.getElementById("cveTable");
// get the first ten entries of the file
for (let i = 0; i < size && i+offset < data.CVE_data_numberOfCVEs; i++) {
// setup the row
var row = table.insertRow(i+1);
// turn each entry into a table row
var rowVals = storeCVE(data.CVE_Items[i+offset]);
// with some tweaking of TableRow this could just be another loop but I don't know if that's worth it?
// reduces readability anyways unless I make an enum
row.insertCell(0).innerHTML = rowVals.id;
row.insertCell(1).innerHTML = rowVals.type;
row.insertCell(2).innerHTML = rowVals.error;
row.insertCell(3).innerHTML = rowVals.impact;
row.insertCell(4).innerHTML = rowVals.date;
}
})
}
function deletePage() {
// works at the top
var table = document.getElementById("cveTable");
// check if there's actually a page atm before deleting the page
if (table.rows.length > 1)
{
while (table.rows.length > 1) {
table.deleteRow(1); // deleting the second row because we love the headers
}
}
}
function buttonValidator() {
if (pageNum == 0) {
document.getElementById("previousPage").setAttribute("disabled", "");
document.getElementById("firstPage").setAttribute("disabled", "");
} else {
document.getElementById("previousPage").removeAttribute("disabled");
document.getElementById("firstPage").removeAttribute("disabled");
}
if (pageSize*(pageNum+1) > capacity) {
document.getElementById("nextPage").setAttribute("disabled", "");
document.getElementById("lastPage").setAttribute("disabled", "");
} else {
document.getElementById("nextPage").removeAttribute("disabled");
document.getElementById("lastPage").removeAttribute("disabled");
}
}
function nextPage() {
deletePage();
pageNum++;
addPage(pageSize, pageNum);
buttonValidator();
// console.log(pageNum);
}
function previousPage() { // this button should start greyed out
deletePage();
pageNum--;
addPage(pageSize, pageNum);
buttonValidator();
}
function testfunction() {
console.log(Number(document.getElementById("selector").value));
let retval = document.getElementById("selector").value;
if (!isNaN(retval) && !isNaN(parseFloat(retval))) {
choosePage(Number(retval));
} else {
choosePage(0);
}
}
function choosePage(pageChosen) {
deletePage();
if (capacity <= pageChosen*pageSize) {
pageNum = pageCount - 1;
} else if (pageChosen < 1) {
pageNum = 0;
} else {
pageNum = pageChosen;
}
addPage(pageSize, pageNum);
buttonValidator();
}
function chooseReversePage(pageChosen) {
choosePage(pageCount - pageChosen);
}
window.onload = function() {
generateTable();
};