-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
544 lines (464 loc) · 17.1 KB
/
index.html
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>WikiTables</title>
<link rel="icon" href="https://en.wikipedia.org/static/favicon/wikipedia.ico" type="image/x-icon">
<style>
:root {
--primary-color: #4a90e2;
--secondary-color: #f2f2f2;
--text-color: #333;
--background-color: #f0f0e1;
--border-color: #ddd;
}
body {
font-family: Arial, sans-serif;
max-width: 1200px;
margin: 0 auto;
padding: 20px;
color: var(--text-color);
background-color: var(--background-color);
transition: background-color 0.3s, color 0.3s;
}
input,
button {
font-size: 16px;
padding: 10px;
margin: 5px 0;
}
button {
background-color: var(--primary-color);
color: white;
border: none;
cursor: pointer;
transition: background-color 0.3s;
}
button:hover {
background-color: #3a7bc8;
}
#results {
margin-top: 20px;
}
.wikipediaPage {
border: 1px solid var(--border-color);
padding: 10px;
margin-bottom: 10px;
cursor: pointer;
transition: background-color 0.3s;
}
.wikipediaPage:hover {
background-color: var(--secondary-color);
}
table {
border-collapse: collapse;
width: 100%;
}
th,
td {
border: 1px solid var(--border-color);
padding: 8px;
text-align: left;
}
th {
background-color: var(--secondary-color);
}
.loading {
display: none;
text-align: center;
margin-top: 20px;
}
.error {
color: red;
margin-top: 10px;
}
#content-wrapper {
display: flex;
width: 100%;
gap: 20px;
}
#infoTable {
flex: 1;
max-width: calc(50% - 10px);
}
#tables {
flex: 1;
display: flex;
flex-wrap: wrap;
align-content: flex-start;
}
/* Add styles for links */
a {
color: #0645ad;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
.wikipediaPage.selected {
background-color: #e0e0e0;
}
/* Mobile-specific styles */
@media (max-width: 767px) {
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
max-width: 100%;
padding: 10px;
-webkit-text-size-adjust: 100%;
}
h1 {
font-size: 24px;
margin-bottom: 15px;
}
input,
button {
width: 100%;
box-sizing: border-box;
-webkit-appearance: none;
border-radius: 0;
}
#content-wrapper {
flex-direction: column;
}
#infoTable,
#tables {
flex: none;
max-width: 100%;
width: 100%;
}
table {
font-size: 14px;
}
th,
td {
padding: 5px;
}
}
</style>
<style>
/* Dark mode styles */
body.dark-mode {
--primary-color: #6ab0ff;
--secondary-color: #2c2c2c;
--text-color: #f0f0f0;
--background-color: #1a1a1a;
--border-color: #444;
}
/* Dark mode toggle styles */
.dark-mode-toggle {
position: fixed;
top: 20px;
right: 20px;
display: flex;
align-items: center;
}
.dark-mode-input {
display: none;
}
.dark-mode-label {
cursor: pointer;
padding: 10px;
background-color: var(--secondary-color);
color: var(--text-color);
border-radius: 20px;
transition: background-color 0.3s, color 0.3s;
}
.dark-mode-input:checked+.dark-mode-label {
background-color: var(--primary-color);
}
/* Additional dark mode adjustments */
.dark-mode button {
background-color: var(--primary-color);
color: var(--text-color);
}
.dark-mode .wikipediaPage {
background-color: var(--secondary-color);
border-color: var(--border-color);
}
.dark-mode .wikipediaPage:hover,
.dark-mode .wikipediaPage.selected {
background-color: #3a3a3a;
}
.dark-mode th {
background-color: var(--secondary-color);
}
.dark-mode a {
color: var(--primary-color);
}
</style>
</head>
<body>
<div id="landing_page_div">
<div class="dark-mode-toggle">
<input type="checkbox" id="darkModeToggle" class="dark-mode-input">
<label for="darkModeToggle" class="dark-mode-label">Dark Mode</label>
</div>
<h1>WikiTables</h1>
<input type="text" id="searchInput" placeholder="Search Wikipedia" aria-label="Search for a Wikipedia page">
<button id="clearButton" onclick="clearSearchInput()" style="display: none;">×</button>
<button id="searchButton" onclick="searchWikipediaPages()">Search</button>
<div id="loading" class="loading" aria-live="polite">Loading...</div>
<div id="error" class="error" aria-live="assertive"></div>
<div id="results" role="list"></div>
</div>
<div id="content-wrapper">
<div id="infoTable">
<!-- Content for infocard div -->
</div>
<div id="tables">
<!-- Content for rest of the tables div -->
</div>
</div>
<script>
let pages = [];
let selectedPageTitleIndex = -1;
const searchPageDiv = document.getElementById('landing_page_div');
const searchInput = document.getElementById('searchInput');
const searchButton = document.getElementById('searchButton');
const loadingDiv = document.getElementById('loading');
const errorDiv = document.getElementById('error');
const resultsDiv = document.getElementById('results');
const infoCardResultDiv = document.getElementById('infoTable');
const tableResultsDiv = document.getElementById('tables');
async function fetchWikipediaPage(e) {
if (e.startsWith('https://en.wikipedia.org/wiki/')) {
e = e.split('/').pop();
}
const t = `https://en.wikipedia.org/w/api.php?action=parse&page=${encodeURIComponent(e)}&format=json&origin=*`;
try {
const e = await fetch(t);
if (!e.ok) throw new Error(`HTTP error! status: ${e.status}`);
const r = await e.json();
if (r.error) throw new Error(`Wikipedia API error: ${r.error.info}`);
return r.parse.text["*"];
} catch (e) {
throw e;
}
}
function removeEditSpans(element) {
const editSpans = element.querySelectorAll('span');
editSpans.forEach(span => {
if (/edit/i.test(span.textContent)) {
span.remove();
}
});
}
function extractTablesAndDivs(htmlContent) {
const parser = new DOMParser();
const doc = parser.parseFromString(htmlContent, 'text/html');
// Remove all elements with class "mw-empty-elt"
doc.querySelectorAll('.mw-empty-elt').forEach(el => el.remove());
// Find the info card table
const infocardTables = Array.from(doc.querySelectorAll('.infobox.vcard'));
const infocardTable = infocardTables[0];
const result = [];
if (infocardTable) {
result.push(infocardTable.outerHTML);
}
const tables = Array.from(doc.querySelectorAll('.wikitable'));
tables.forEach(table => {
let currentNode = table.previousElementSibling;
const precedingDivs = [];
while (currentNode && currentNode.tagName.toLowerCase() === 'div') {
removeEditSpans(currentNode);
precedingDivs.unshift(currentNode.outerHTML);
currentNode = currentNode.previousElementSibling;
}
// Add the preceding divs to the result array
result.push(...precedingDivs);
// Add the table to the result array
result.push(table.outerHTML);
});
return result;
}
async function scrapeTables(url) {
try {
showLoading(true);
const htmlContent = await fetchWikipediaPage(url);
let tables = extractTablesAndDivs(htmlContent);
if (tables.length === 0) {
throw new Error('No tables found on this page');
}
return tables;
} catch (error) {
showError(error.message);
return null;
} finally {
showLoading(false);
}
}
function clearSearchScreen() {
resultsDiv.innerHTML = '';
}
async function showWikipediaPageTables(e) {
try {
showLoading(true);
const t = await scrapeTables(e);
if (t) {
infoCardResultDiv.innerHTML = modifyLinks(t[0]);
tableResultsDiv.innerHTML = modifyLinks(t.slice(1).join(''));
clearSearchScreen();
}
} catch (e) {
showError(e.message);
} finally {
showLoading(false);
}
}
function modifyLinks(e) {
const t = (new DOMParser).parseFromString(e, 'text/html');
t.querySelectorAll('a').forEach(e => {
const t = e.getAttribute('href');
if (t && t.startsWith('/wiki/')) {
e.setAttribute('href', `https://en.wikipedia.org${t}`);
e.setAttribute('target', '_blank');
}
});
return t.body.innerHTML;
}
function showLoading(e) {
loadingDiv.style.display = e ? 'block' : 'none';
}
function showError(e) {
errorDiv.textContent = e;
setTimeout(() => {
errorDiv.textContent = '';
}, 5e3);
}
function debounce(e, t) {
let r;
return function (...n) {
clearTimeout(r);
r = setTimeout(() => e.apply(this, n), t);
}
}
async function searchWikipediaPages() {
const searchText = searchInput.value.trim();
if (!searchText) {
showError('Please enter a wikipedia title to search for');
return;
}
const urlString = `https://en.wikipedia.org/w/api.php?action=opensearch&search=${encodeURIComponent(searchText)}&limit=10&pageTitlespace=0&format=json&origin=*`;
try {
showLoading(true);
const response = await fetch(urlString);
const jsonArray = await response.json();
if (Array.isArray(jsonArray) && jsonArray.length > 3) {
const pageTitles = jsonArray[1];
const urls = jsonArray[3];
if (Array.isArray(pageTitles) && Array.isArray(urls) && pageTitles.length === urls.length) {
pages = pageTitles.map((pageTitle, index) => ({ pageTitle, url: urls[index] }));
displayResults();
if (pages.length > 0) {
highlightTopResult(pages[0].pageTitle);
selectedPageTitleIndex = 0;
}
} else {
throw new Error('Invalid response format');
}
} else {
throw new Error('No results found');
}
} catch (error) {
console.error('Error:', error);
showError(error.message);
} finally {
showLoading(false);
}
}
function displayResults() {
resultsDiv.innerHTML = pages.map((wikipediaPage, index) => `
<div class="wikipediaPage" onclick="handlePageTitleClick(${index})" role="listitem">
<h2>${wikipediaPage.pageTitle}</h2>
</div>
`).join('');
}
function handlePageTitleClick(index) {
if (index >= 0 && index < pages.length) {
searchInput.value = pages[index].pageTitle;
showWikipediaPageTables(pages[index].url);
clearResults();
}
}
function highlightTopResult(suggestion) {
const userInput = searchInput.value;
if (suggestion.toLowerCase().startsWith(userInput.toLowerCase())) {
searchInput.value = userInput + suggestion.slice(userInput.length);
searchInput.setSelectionRange(userInput.length, suggestion.length);
}
}
function clearResults() {
resultsDiv.innerHTML = '';
pages = [];
selectedPageTitleIndex = -1;
}
function handleKeyDown(event) {
if (pages.length === 0) return;
if (event.key === 'ArrowDown') {
event.preventDefault();
selectedPageTitleIndex = (selectedPageTitleIndex + 1) % pages.length;
updateHighlight();
} else if (event.key === 'ArrowUp') {
event.preventDefault();
selectedPageTitleIndex = (selectedPageTitleIndex - 1 + pages.length) % pages.length;
updateHighlight();
} else if (event.key === 'Enter') {
event.preventDefault();
if (selectedPageTitleIndex !== -1) {
handlePageTitleClick(selectedPageTitleIndex);
} else {
searchWikipediaPages();
}
}
}
function updateHighlight() {
if (selectedPageTitleIndex !== -1) {
highlightTopResult(pages[selectedPageTitleIndex].pageTitle);
const wikipediaPageElements = resultsDiv.querySelectorAll('.pageTitle');
pageTitleElements.forEach((el, index) => {
el.classList.toggle('selected', index === selectedPageTitleIndex);
});
}
}
function clearSearchInput() {
searchInput.value = '';
clearResults();
document.getElementById('clearButton').style.display = 'none'; // Hide the clear button
searchInput.focus(); // Set focus back to the search input
}
// Event listeners
searchInput.addEventListener('input', () => {
document.getElementById('clearButton').style.display = searchInput.value ? 'inline' : 'none'; // Show/hide clear button
});
searchInput.addEventListener('input', debounce(searchWikipediaPages, 300));
searchInput.addEventListener('keydown', handleKeyDown);
searchButton.addEventListener('click', searchWikipediaPages);
</script>
<script>
// Dark mode toggle functionality
const darkModeToggle = document.getElementById('darkModeToggle');
// Check for saved dark mode preference
if (localStorage.getItem('darkMode') === 'enabled') {
document.body.classList.add('dark-mode');
darkModeToggle.checked = true;
}
darkModeToggle.addEventListener('change', () => {
if (darkModeToggle.checked) {
document.body.classList.add('dark-mode');
localStorage.setItem('darkMode', 'enabled');
} else {
document.body.classList.remove('dark-mode');
localStorage.setItem('darkMode', 'disabled');
}
});
// Function to update dark mode styles for dynamically added content
function updateDarkModeStyles() {
const isDarkMode = document.body.classList.contains('dark-mode');
document.querySelectorAll('.pageTitle, button, th, a').forEach(element => {
element.style.transition = 'background-color 0.3s, color 0.3s';
});
}
</script>
</body>
</html>