-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBarclaySearchEngineScripts.js
More file actions
94 lines (83 loc) · 3.42 KB
/
BarclaySearchEngineScripts.js
File metadata and controls
94 lines (83 loc) · 3.42 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
document.getElementById('search-form').addEventListener('submit', function(e) {
e.preventDefault();
const query = document.getElementById('query').value;
if (query) {
window.history.pushState({}, '', `?query=${encodeURIComponent(query)}`);
search(query);
}
});
document.getElementById('prev').addEventListener('click', function() {
if (startIndex > 1) {
startIndex -= 10;
const query = new URLSearchParams(window.location.search).get('query');
if (query) search(query);
}
});
document.getElementById('next').addEventListener('click', function() {
startIndex += 10;
const query = new URLSearchParams(window.location.search).get('query');
if (query) search(query);
});
let startIndex = 1;
function search(query) {
const apiKey = '';
const searchEngineId = '';
const url = `https://www.googleapis.com/customsearch/v1?key=${apiKey}&cx=${searchEngineId}&q=${query}&start=${startIndex}`;
document.title = `Barclay Search - "${query}"`;
fetch(url)
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => {
handleSearchResults(data, query);
})
.catch(error => {
console.error('Error:', error);
fallbackSearch(query);
});
}
function handleSearchResults(data, query) {
const resultsContainer = document.getElementById('results');
const totalResultsElement = document.getElementById('total-results');
const paginationInfoElement = document.getElementById('pagination-info');
resultsContainer.innerHTML = '';
if (data.items) {
totalResultsElement.textContent = `Showing ${data.items.length} of ${parseInt(data.searchInformation.totalResults).toLocaleString()} results`;
data.items.forEach(item => {
const resultItem = document.createElement('div');
resultItem.classList.add('result-item');
resultItem.innerHTML = `
<p class="result-url">${item.link}</p>
<h3><a href="${item.link}" target="_blank">${item.title}</a></h3>
<p>${item.snippet}</p>
`;
resultsContainer.appendChild(resultItem);
});
const currentPage = Math.ceil(startIndex / 10);
const totalPages = Math.ceil(data.searchInformation.totalResults / 10);
paginationInfoElement.textContent = `Page ${currentPage.toLocaleString()} of ${totalPages.toLocaleString()}`;
document.getElementById('prev').disabled = startIndex === 1;
document.getElementById('next').disabled = startIndex + data.items.length > data.searchInformation.totalResults;
window.scrollTo(0, 0)
} else {
resultsContainer.innerHTML = '<p>No results found</p>';
totalResultsElement.textContent = '';
paginationInfoElement.textContent = '';
document.getElementById('prev').disabled = true;
document.getElementById('next').disabled = true;
}
}
function fallbackSearch(query) {
const googleSearchUrl = `https://www.google.com/search?q=${encodeURIComponent(query)}`;
window.open(googleSearchUrl, '_blank');
}
window.onload = function() {
const query = new URLSearchParams(window.location.search).get('query');
if (query) {
document.getElementById('query').value = query;
search(query);
}
};