-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
282 lines (231 loc) · 12.5 KB
/
index.html
File metadata and controls
282 lines (231 loc) · 12.5 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
272
273
274
275
276
277
278
279
280
281
282
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Wikipedia API - Search 4.8M Articles</title>
<style>
body { font-family: Arial, sans-serif; max-width: 800px; margin: 0 auto; padding: 20px; background: #f5f5f5; }
h1 { color: #333; text-align: center; }
.search-box { background: white; padding: 20px; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); margin-bottom: 20px; }
input { width: 100%; padding: 12px; font-size: 16px; border: 1px solid #ddd; border-radius: 4px; }
button { background: #007bff; color: white; padding: 12px 24px; border: none; border-radius: 4px; cursor: pointer; margin-top: 10px; }
button:hover { background: #0056b3; }
.results { background: white; padding: 20px; border-radius: 8px; }
.result { padding: 10px 0; border-bottom: 1px solid #eee; }
.result h3 { margin: 0 0 5px 0; color: #1a0dab; }
.loading { text-align: center; color: #666; }
.strategy-section { margin: 15px 0; padding: 10px; border-left: 3px solid #007bff; background: #f8f9fa; }
.strategy-title { font-weight: bold; color: #007bff; margin-bottom: 5px; }
.strategy-status { font-size: 0.9em; color: #666; margin-bottom: 10px; }
.result-score { float: right; font-size: 0.8em; color: #999; }
</style>
</head>
<body>
<h1>Wikipedia API</h1>
<p style="text-align: center; color: #666;">Search 4.8 million articles instantly</p>
<div class="search-box">
<input type="text" id="query" placeholder="Search Wikipedia..." onkeypress="if(event.key=='Enter') doSearch()">
<button onclick="doSearch()">Fast Search</button>
<button onclick="doStreamingSearch()" style="background: #28a745;">Progressive Search</button>
</div>
<div id="loading" class="loading" style="display:none;">Searching...</div>
<div id="results" class="results"></div>
<script>
// Escape HTML to prevent XSS attacks
function escapeHtml(text) {
if (text === null || text === undefined) return '';
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}
// Create a text node safely (alternative to escapeHtml for DOM manipulation)
function createSafeTextNode(text) {
return document.createTextNode(text || '');
}
async function doSearch() {
const query = document.getElementById('query').value;
if (!query) return;
document.getElementById('loading').style.display = 'block';
document.getElementById('results').innerHTML = '';
try {
const response = await fetch('/api/search?q=' + encodeURIComponent(query) + '&limit=10', {
headers: {'X-API-Key': 'demo_key_123'}
});
const data = await response.json();
const resultsDiv = document.getElementById('results');
if (data.error) {
const errorP = document.createElement('p');
errorP.textContent = 'Error: ' + data.error;
resultsDiv.appendChild(errorP);
} else {
const infoP = document.createElement('p');
infoP.textContent = 'Found ' + data.count + ' results in ' + data.time_seconds.toFixed(3) + ' seconds';
resultsDiv.appendChild(infoP);
data.results.forEach((r, index) => {
const resultDiv = document.createElement('div');
resultDiv.className = 'result';
const title = document.createElement('h3');
title.textContent = r.title;
const summary = document.createElement('p');
summary.textContent = r.summary || r.snippet || '';
resultDiv.appendChild(title);
resultDiv.appendChild(summary);
resultsDiv.appendChild(resultDiv);
});
}
} catch (err) {
const resultsDiv = document.getElementById('results');
const errorP = document.createElement('p');
errorP.textContent = 'Error: ' + err.message;
resultsDiv.appendChild(errorP);
}
document.getElementById('loading').style.display = 'none';
}
async function doStreamingSearch() {
const query = document.getElementById('query').value;
if (!query) return;
document.getElementById('loading').style.display = 'block';
document.getElementById('results').innerHTML = '';
const startTime = Date.now();
let totalResults = 0;
let completedStrategies = 0;
// Define search strategies in order of speed/priority
const strategies = [
{ endpoint: '/api/search/exact', name: 'exact_matches', title: 'Exact Matches', delay: 0 },
{ endpoint: '/api/search/titles', name: 'title_matches', title: 'Title Matches', delay: 100 },
{ endpoint: '/api/search/phrase', name: 'phrase_search', title: 'Phrase Search', delay: 300 },
{ endpoint: '/api/search/hyphen', name: 'hyphen_search', title: 'Hyphenated Terms', delay: 500 },
{ endpoint: '/api/search/keywords', name: 'keyword_search', title: 'Keyword Search', delay: 700 }
];
// Function to execute a single search strategy
async function executeStrategy(strategy) {
// Create strategy section using safe DOM methods
const section = document.createElement('div');
section.className = 'strategy-section';
section.id = `section-${strategy.name}`;
const titleDiv = document.createElement('div');
titleDiv.className = 'strategy-title';
titleDiv.textContent = strategy.title;
const statusDiv = document.createElement('div');
statusDiv.className = 'strategy-status';
statusDiv.id = `status-${strategy.name}`;
statusDiv.textContent = 'Searching...';
const resultsDiv = document.createElement('div');
resultsDiv.className = 'strategy-results';
resultsDiv.id = `results-${strategy.name}`;
section.appendChild(titleDiv);
section.appendChild(statusDiv);
section.appendChild(resultsDiv);
document.getElementById('results').appendChild(section);
try {
const response = await fetch(`${strategy.endpoint}?q=${encodeURIComponent(query)}&limit=5`);
const data = await response.json();
const statusEl = document.getElementById(`status-${strategy.name}`);
const resultsEl = document.getElementById(`results-${strategy.name}`);
if (data.error) {
statusEl.textContent = 'Error: ' + data.error;
statusEl.style.color = '#dc3545';
return;
}
if (data.count === 0) {
statusEl.textContent = 'No results found';
statusEl.style.color = '#6c757d';
return;
}
// Update status with results count
statusEl.textContent = `Found ${data.count} results (${data.time_seconds}s)`;
statusEl.style.color = '#28a745';
// Add results with deduplication using safe DOM methods
data.results.forEach(result => {
// Simple deduplication check
const existingTitles = Array.from(document.querySelectorAll('.result h3')).map(h3 => h3.textContent.trim());
if (existingTitles.includes(result.title)) {
return; // Skip duplicate
}
const resultDiv = document.createElement('div');
resultDiv.className = 'result';
const h3 = document.createElement('h3');
h3.textContent = result.title;
const scoreSpan = document.createElement('span');
scoreSpan.className = 'result-score';
scoreSpan.textContent = Math.round(result.relevance_score * 100) + '%';
h3.appendChild(scoreSpan);
const p = document.createElement('p');
p.textContent = result.summary || '';
resultDiv.appendChild(h3);
resultDiv.appendChild(p);
resultsEl.appendChild(resultDiv);
});
totalResults += data.count;
} catch (error) {
const statusEl = document.getElementById(`status-${strategy.name}`);
statusEl.textContent = 'Network error: ' + error.message;
statusEl.style.color = '#dc3545';
} finally {
completedStrategies++;
// Update loading indicator
if (completedStrategies === strategies.length) {
document.getElementById('loading').style.display = 'none';
const elapsed = (Date.now() - startTime) / 1000;
const summary = document.createElement('div');
summary.style.textAlign = 'center';
summary.style.marginTop = '20px';
summary.style.padding = '10px';
summary.style.background = '#e9ecef';
summary.style.borderRadius = '5px';
const strong = document.createElement('strong');
strong.textContent = 'Progressive search complete!';
const br = document.createElement('br');
const info = document.createTextNode(
`Found ${totalResults} results across ${completedStrategies} strategies in ${elapsed.toFixed(2)}s`
);
summary.appendChild(strong);
summary.appendChild(br);
summary.appendChild(info);
document.getElementById('results').appendChild(summary);
}
}
}
// Execute strategies with staggered delays for better UX
strategies.forEach(strategy => {
setTimeout(() => executeStrategy(strategy), strategy.delay);
});
}
function getStrategyTitle(strategy) {
const titles = {
'exact_matches': 'Exact Matches',
'title_matches': 'Title Matches',
'phrase_search': 'Phrase Search',
'hyphen_search': 'Hyphenated Terms',
'keyword_search': 'Keyword Search'
};
return titles[strategy] || strategy;
}
// Load trending on page load - using safe DOM methods
window.onload = async () => {
try {
const response = await fetch('/api/trending');
const data = await response.json();
const resultsDiv = document.getElementById('results');
const h2 = document.createElement('h2');
h2.textContent = 'Trending Articles';
resultsDiv.appendChild(h2);
data.trending.slice(0, 5).forEach(a => {
const resultDiv = document.createElement('div');
resultDiv.className = 'result';
const title = document.createElement('h3');
title.textContent = a.title;
const summary = document.createElement('p');
summary.textContent = a.summary || '';
resultDiv.appendChild(title);
resultDiv.appendChild(summary);
resultsDiv.appendChild(resultDiv);
});
} catch (err) {
console.error('Failed to load trending', err);
}
};
</script>
</body>
</html>