-
Notifications
You must be signed in to change notification settings - Fork 0
/
myscript.js
63 lines (56 loc) · 1.87 KB
/
myscript.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
// @ts-nocheck
$(document).ready(function () {
function jsEscape(str) {
// This method is used to prevent user from XSS (cross-site scripting) attack.
// NOTE: We are using this method instead of $(selector).text() provided by jQuery becuase of whitespace issue with it. (https://forum.jquery.com/topic/inserting-whitespace-into-a-textnode)
var tagsToReplace = {
"&": "&",
"<": "<",
">": ">",
'"': """,
"'": "'",
"/": "/",
"`": "`",
"=": "=",
};
if (typeof str === "string") {
return str.replace(/[&<>"'`=\/]/g, function (tag) {
return tagsToReplace[tag];
});
} else {
return str;
}
}
$.ajax({
url: "https://run.mocky.io/v3/e11e89e4-f328-4d3c-974d-b6707e9f27df", // Mock api (just for testing).
type: "GET",
dataType: "json",
success: function (res) {
console.log(res);
// This code generates table from the data provided by this http call.
const caption = "Codility Table";
let $table = $("<table><caption>" + caption + "</caption></table>");
let tr_1 = "<tr><td id='blank_td'></td>";
res.headers.forEach(function (item, index) {
tr_1 += "<th scope='col'>" + jsEscape(item) + "</th>";
});
tr_1 += "</tr>";
$table.append(tr_1);
for (const [key, value] of Object.entries(res.data)) {
var other_tr = "<tr>";
other_tr += "<th scope='row'>" + jsEscape(key) + "</th>";
value.forEach(function (item, index) {
other_tr += "<td class='my_td'>" + jsEscape(item) + "</td>";
});
other_tr += "</tr>";
$table.append(other_tr);
}
$("#my_table").append($table);
},
error: function (err) {
// Error occured.
console.log(err);
alert("Something went wrong. Please try again later.");
},
});
});