-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
58 lines (53 loc) · 2.5 KB
/
main.js
File metadata and controls
58 lines (53 loc) · 2.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
document.getElementById('searchForm').addEventListener('submit', function (e) {
e.preventDefault();
const query = document.getElementById('query').value;
if (query) {
fetchResults(query);
window.history.pushState({}, '', `?q=${encodeURIComponent(query)}`);
}
});
function fetchResults(query, start = 1) {
document.getElementById('loading').style.display = "block";
fetch(`load.php?q=${encodeURIComponent(query)}&start=${start}`)
.then(response => response.json())
.then(data => displayResults(data))
.catch(error => console.error('Error:', error));
document.title = `${query} - CPSearchX`;
}
function displayResults(data) {
const itemList = document.getElementById('item_list');
// itemList.innerHTML = '';
document.getElementById('loading').style.display = "none";
if (data.items) {
data.items.forEach(item => {
const title = item.title || '<i>no heading for that one</i>';
const link = item.link || '<i>No link</i>';
const snippet = item.snippet || '<i>No more details</i>';
const parsedUrl = new URL(link);
const domain = parsedUrl.hostname || 'No domain';
const formattedUrl = link.length > 40 ? link.substring(0, 30) + "..." : link;
const faviconUrl = `https://${domain}/favicon.ico`;
const favicon = new Image();
favicon.src = faviconUrl;
favicon.onerror = () => { favicon.src = './testfav.ico'; };
const itemHTML = `
<div class='items'>
<div class='top'>
<img src='${favicon.src}' width='40px' height='40px' alt='favicon'>
<a href='${link}' class='link'>
<p class='domain'>${domain}</p>
<p class='url'>${formattedUrl}</p>
</a>
</div>
<h3 class='title'><a href='${link}'>${title}</a></h3>
<p class='description'>${snippet}</p>
</div>`;
itemList.innerHTML += itemHTML;
});
document.getElementById("load_more").style.display = "block";
} else if (data.error) {
itemList.innerHTML = `<div class='items'><h3 class='title'><a href=''>Error: ${data.error}</a></h3></div>`;
} else {
itemList.innerHTML = `<div class='items'><h3 class='title'><a href=''>No Result for: <strong>${document.getElementById('query').value}</strong></a></h3></div>`;
}
}