-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathapp.js
77 lines (66 loc) · 1.81 KB
/
app.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
"use strict";
const path = document.location.pathname;
const target = document.querySelector("#target");
const style = document.createElement("style");
const css = `table {
border-radius: 0.3rem;
border: 0.1rem solid #474747;
border-spacing: 0;
padding: 0;
width: 50%;
}
table td {
border-right: 0.1rem solid #474747;
padding: 0.5rem 1rem;
}
table tr td:last-child {
border-right: 0;
text-align: center;
}
table td.pass {
background: #f2f9f4;
color: #4db277;
}
table td.fail {
background: #f2dede;
color: #a94442;
}`;
style.appendChild(document.createTextNode(css));
document.head.appendChild(style);
target.innerHTML = `Current Path: <code>${path}</code>`;
document.addEventListener(
"DOMContentLoaded",
() => {
if (document.querySelector("#files")) {
return;
}
const tests = [
{ url: "/", name: "index", re: /^<!doctype html>/i },
{ url: "/test", name: "non-existent path", re: /^<!doctype html>/i },
{ url: "/file.txt", name: "existing path", re: /^file/ },
];
const table = document.createElement("table");
const tbody = document.createElement("tbody");
table.id = "files";
table.appendChild(tbody);
target.parentNode.appendChild(table);
tests.forEach((test) => {
const tr = document.createElement("tr");
tbody.appendChild(tr);
check(test.url, test.re, (res) => {
tr.innerHTML = `<td>${test.name}</td>`;
tr.innerHTML += `<td><a href="${test.url}">${test.url}</a></td>`;
tr.innerHTML += `<td class="${res}">${res}</td>`;
});
});
},
true
);
function check(url, re, cb) {
const xhr = new XMLHttpRequest();
xhr.addEventListener("load", () => {
cb(re.test(xhr.responseText) ? "pass" : "fail");
});
xhr.open("GET", url);
xhr.send();
}