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
|
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>LinkLogger | Dashboard</title>
</head>
<body>
<div>
<!-- Create a table with 4 columns with a total of 1000px width -->
<table style="width: 1000px; margin: 0 auto;">
<tr>
<th style="width: 250px;">Link</th>
<th style="width: 250px;">Visits</th>
<th style="width: 250px;">Redirect</th>
<th style="width: 250px;">Expire Date</th>
</tr>
<!-- Create a table row for each link -->
<!-- <tr>
<td>(Link)</td>
<td>(Visits)</td>
<td>(Redirect)</td>
<td>(Expire Date)</td>
<td class="log-table-row"> -->
<!-- Create a sub table that will be opened when the link is clicked -->
<!-- <table style="width: 750px; margin: 0 auto;">
<tr>
<th style="width: 150px;">ID</th>
<th style="width: 150px;">Timestamp</th>
<th style="width: 150px;">IP</th>
<th style="width: 150px;">Location</th>
<th style="width: 150px;">ISP</th>
</tr> -->
<!-- Create a table row for each log -->
<!-- <tr>
<td>(ID)</td>
<td>(Timestamp)</td>
<td>(IP)</td>
<td>(Location)</td>
<td>(ISP)</td>
</tr>
</table>
</td>
</tr> -->
</table>
</div>
</body>
</html>
<style>
body {
margin: 0;
padding: 0;
font-family: Arial, sans-serif;
background-color: #2c3338;
}
div {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
text-align: center;
font-size: 25px;
color: #ccc;
}
table {
border-collapse: collapse;
background-color: #333;
color: #ccc;
}
th, td {
border: 1px solid #ccc;
padding: 10px;
}
th {
background-color: #444;
}
tr:nth-child(even) {
background-color: #444;
}
tr:nth-child(odd) {
background-color: #333;
}
.log-table-row {
display: none;
}
.log-table-row td {
padding: 0;
}
.log-table-row table {
width: 100%;
border-collapse: collapse;
}
.log-table-row th, .log-table-row td {
border: 1px solid #ccc;
padding: 10px;
}
.log-table-row th {
background-color: #444;
}
.log-table-row tr:nth-child(even) {
background-color: #444;
}
.log-table-row tr:nth-child(odd) {
background-color: #333;
}
</style>
<script>
async function fetchLinks() {
const response = await fetch('/api/links');
if (!response.ok) {
throw new Error('Failed to fetch links');
}
const links = await response.json();
return links;
}
async function fetchLogs(link) {
const response = await fetch(`/api/links/${link}/logs`);
if (!response.ok) {
throw new Error('Failed to fetch logs');
}
const logs = await response.json();
return logs;
}
function createRow(index, link, logs) {
// Create the sub-table with the logs
let subTable = `
<table style="width: 750px; margin: 0 auto; id=${index}">
<tr>
<th style="width: 150px;">ID</th>
<th style="width: 150px;">Timestamp</th>
<th style="width: 150px;">IP</th>
<th style="width: 150px;">Location</th>
<th style="width: 150px;">ISP</th>
</tr>
`;
// Loop through the logs and create a row for each one
logs.forEach((log, index) => {
let row = `
<tr>
<td>${logs.length - index}</td>
<td>${log.timestamp}</td>
<td>${log.ip}</td>
<td>${log.location}</td>
<td>${log.isp}</td>
</tr>
`;
subTable += row;
});
subTable += '</table>';
// Convert the link expire timestamp to a readable date
let date = new Date(link.expire_date);
let expireDate = date.toLocaleTimeString('en-US', {
month: 'short',
day: 'numeric',
year: 'numeric',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
hour12: false
});
// Create the HTML for the row with sub-table
let row = `
<tr>
<td>
<button class="link-button" id="${index}">${link.link}</button>
</td>
<td>${logs.length}</td>
<td>${link.redirect_link}</td>
<td>${expireDate}</td>
</tr>
<tr class="log-table-row" id="${index}-log">
<td colspan="4">
${subTable}
</td>
</tr>
`;
return row;
}
async function getData() {
let links = fetchLinks();
links = await links;
console.log(links);
// Links is an Array of objects with the link data
// Loop through the links and create a row for each one
links.forEach(async (link, index) => {
// Fetch the logs for the link
let logs = await fetchLogs(link.link);
logs = await logs;
// Create the entire row with sub-table and logs
let row = createRow(index, link, logs);
// Add the new table row to the main table
document.querySelector('table').innerHTML += row;
});
}
function hideLogTables() {
let tables = document.querySelectorAll('table tr table');
tables.forEach(table => {
table.style.display = 'none';
});
}
getData();
hideLogTables();
// Add event listener to each element with the class of link-button
document.addEventListener('click', (event) => {
if (event.target.classList.contains('link-button')) {
let id = event.target.id;
let table = document.getElementById(`${id}-log`);
if (table.style.display === 'none') {
table.style.display = 'table-row';
} else {
table.style.display = 'none';
}
}
});
</script>
|