-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.js
More file actions
271 lines (232 loc) · 8.48 KB
/
code.js
File metadata and controls
271 lines (232 loc) · 8.48 KB
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
//URL of the JSON file to retrieve and parse
const url = "nvdcve-1.1-recent.json";
//Initial range values for item numbers to retrieve
let curRangeMin = 1;
let curRangeMax = 5;
let curRange = 5;
let itemCount = 999; //Will be updated after first JSON call to accurately reflect the count
//Function to obtain the JSON and parse its data at specific range values
function getJson(rangeStart, rangeStop)
{
//Fetch JSON at the local URL
fetch(url)
.then(res => res.json())
.then(data => {
//Obtain accurate itemCount
itemCount = data.CVE_Items.length;
//Ensure extra table rows are not displayed if we are on the last page and there are less items than the range
if(rangeStop == itemCount)
{
ifLastPage();
}
//Ensure these table rows are restored if coming from the last page with fewer items
else if(rangeStop == ceilToNearestRangeVal(itemCount) - curRange)
{
ifComingFromLastPage();
}
//Update the table with the current range of JSON items (-1 due to zero-indexing)
updateTableVals(rangeStart - 1, rangeStop - 1, data);
//Display what number items are being shown and the total count
document.getElementById("curDisplay").innerHTML = `Displaying items <strong>${curRangeMin}</strong> through <strong>${curRangeMax}</strong> out of <strong>${data.CVE_Items.length}</strong>`;
document.getElementById("curDisplayBottom").innerHTML = `Displaying items <strong>${curRangeMin}</strong> through <strong>${curRangeMax}</strong> out of <strong>${data.CVE_Items.length}</strong>`;
})
.catch((error) => {
console.error(error);
})
}
//Function to get the CVE Number of a specified JSON entry
function getCVENum(itemNumber, parsedJson)
{
return parsedJson.CVE_Items[itemNumber].cve.CVE_data_meta.ID;
}
//Function to get Published Date of a specified JSON entry
function getPublishedDate(itemNumber, parsedJson)
{
return parsedJson.CVE_Items[itemNumber].publishedDate;
}
//Function to get Last Modified Date of a specified JSON entry
function getLastModifiedDate(itemNumber, parsedJson)
{
return parsedJson.CVE_Items[itemNumber].lastModifiedDate;
}
//Function to get the Description Language of a specified JSON entry
function getDescriptionLang(itemNumber, parsedJson)
{
return parsedJson.CVE_Items[itemNumber].cve.description.description_data[0].lang;
}
//Function to get the Description Value of a specified JSON entry
function getDescriptionValue(itemNumber, parsedJson)
{
return parsedJson.CVE_Items[itemNumber].cve.description.description_data[0].value;
}
//Function to update a table Row with its newly obtained JSON Data
function updateTableRow(rowNum, itemNumber, parsedJson)
{
//Get the table from the HTML document
let nvdTable = document.getElementById("nvdTable");
//Update its cells with data from the JSON file
nvdTable.rows[rowNum].cells[0].innerHTML = getCVENum(itemNumber, parsedJson);
nvdTable.rows[rowNum].cells[1].innerHTML = getPublishedDate(itemNumber, parsedJson);
nvdTable.rows[rowNum].cells[2].innerHTML = getLastModifiedDate(itemNumber, parsedJson);
nvdTable.rows[rowNum].cells[3].innerHTML = getDescriptionLang(itemNumber, parsedJson);
nvdTable.rows[rowNum].cells[4].innerHTML = getDescriptionValue(itemNumber, parsedJson);
}
//Function to update the table as a Whole with its JSON data
function updateTableVals(newRangeMin, newRangeMax, parsedJson)
{
//To advance along table rows
let rowCount = 1;
//Loop through the range
for(let i = newRangeMin; i <= newRangeMax; i++)
{
//Update each row
updateTableRow(rowCount, i, parsedJson);
//Go to next row
rowCount++;
}
}
//Function to advance to the Next page by increasing the current range minimum and maximum
function increaseRange()
{
//If the next page has the full range of items (5)
if((curRangeMax + curRange) < itemCount)
{
//Add the range to the current min and max values
curRangeMin += curRange;
curRangeMax += curRange;
//Get the JSON data for these items
getJson(curRangeMin, curRangeMax);
}
//If we have reached the end of the item list and there are no more to obtain
else if(curRangeMax == itemCount)
{
//Do not get the JSON data for these items
alert("No further items from this point.")
}
//If the next page has LESS than the full range of items
else
{
//Set max to the total # of items, and min to += the range
curRangeMax = itemCount;
curRangeMin += curRange;
//Get the JSON data for these items
getJson(curRangeMin, curRangeMax);
}
}
//Function to go back to the Previous page by decreasing the current range minimum and maximum
function decreaseRange()
{
//Ensure that decreasing the range will not go out of scope
if(curRangeMin - curRange > 0 && curRangeMax != itemCount)
{
//Decrease range values
curRangeMin -= curRange;
curRangeMax -= curRange;
//Obtain JSON data for this new range
getJson(curRangeMin, curRangeMax);
}
//If the current maximum is the final item of the data
else if(curRangeMax == itemCount)
{
//Decrease min range value as normal
curRangeMin -= curRange;
//Decrease max range value from the next highest range value (multiple of 5 in this case)
curRangeMax = ceilToNearestRangeVal(curRangeMax) - curRange;
//Obtain JSON data for this new range
getJson(curRangeMin, curRangeMax);
}
//If decreasing the range will go out of scope
else
{
alert("No previous records to display.")
}
}
//Function to round to the next highest multiple of the range value
function ceilToNearestRangeVal(numToRound)
{
return Math.ceil(Number(numToRound)/curRange) * curRange;
}
//Function to go to a specific item number when the user types it in
function goToItem(itemNumber)
{
//Ensure that the typed number is a valid integer that isn't out of scope
if(itemNumber <= 0)
{
alert("There is no item to display at that number.")
}
else if(itemNumber > itemCount)
{
alert("There is no item to display at that number.")
}
else if(Number.isInteger(Number(itemNumber)) == false)
{
alert("Please enter a valid integer.")
}
else
{
//If we are coming from the last page, restore rows
if(curRangeMax != ceilToNearestRangeVal(curRangeMax))
{
ifComingFromLastPage();
}
//Get the minimum and maximum from this number
curRangeMax = ceilToNearestRangeVal(itemNumber);
curRangeMin = (curRangeMax + 1) - curRange;
//If this number is on the last page of data, ensure the max isn't out of scope
if(itemCount > curRangeMin && itemCount < curRangeMax)
{
curRangeMax = itemCount;
}
//Obtain JSON data for this new range
getJson(curRangeMin, curRangeMax);
}
}
//Function to delete a row in case we don't have the full range of items to display
function delRow(rowId)
{
document.getElementById(rowId).innerHTML = "";
}
//Function to restore a row after deleting it
function restoreRow(rowId)
{
document.getElementById(rowId).innerHTML = "<td></td><td></td><td></td><td></td><td id='descriptionCol'></td>";
}
//Function to temporarily delete extra rows if on the last page of data without a full range
function ifLastPage()
{
//Get the number of rows that this page should have
let numRows = (curRangeMax - curRangeMin) + 1;
//Switch based on the number of rows it should have
switch(numRows)
{
//Delete any unnecessary rows
case 1:
delRow("rowTwo");
delRow("rowThree");
delRow("rowFour");
delRow("rowFive");
break;
case 2:
delRow("rowThree");
delRow("rowFour");
delRow("rowFive");
break;
case 3:
delRow("rowFour");
delRow("rowFive");
break;
case 4:
delRow("rowFive");
break;
default:
break;
}
}
//Function to restore any rows that were deleted when viewing the last page
function ifComingFromLastPage()
{
restoreRow("rowTwo");
restoreRow("rowThree");
restoreRow("rowFour");
restoreRow("rowFive");
}