|
<!DOCTYPE html> |
|
<html> |
|
<head> |
|
<meta charset="UTF-8" /> |
|
<title>Brave News Search Example</title> |
|
<style> |
|
|
|
body { font-family: sans-serif; margin: 20px; } |
|
form > div { margin-bottom: 8px; } |
|
label { font-weight: bold; margin-right: 5px; } |
|
</style> |
|
</head> |
|
<body> |
|
<h1>Brave News Search</h1> |
|
<form id="searchForm"> |
|
<div> |
|
<label for="query">Query:</label> |
|
<input type="text" id="query" name="query" required /> |
|
</div> |
|
<div> |
|
<label for="country">Country:</label> |
|
<select id="country" name="country"> |
|
<option value="us">United States (US)</option> |
|
<option value="gb">United Kingdom (GB)</option> |
|
<option value="ca">Canada (CA)</option> |
|
<option value="all">All Regions (ALL)</option> |
|
|
|
</select> |
|
</div> |
|
<div> |
|
<label for="language">Language:</label> |
|
<select id="language" name="language"> |
|
<option value="en">English (en)</option> |
|
<option value="es">Spanish (es)</option> |
|
<option value="fr">French (fr)</option> |
|
|
|
</select> |
|
</div> |
|
<div> |
|
<label for="safesearch">SafeSearch:</label> |
|
<select id="safesearch" name="safesearch"> |
|
<option value="moderate">Moderate (default)</option> |
|
<option value="off">Off</option> |
|
<option value="strict">Strict</option> |
|
</select> |
|
</div> |
|
<div> |
|
<label for="freshness">Freshness:</label> |
|
<select id="freshness" name="freshness" onchange="toggleDateInputs()"> |
|
<option value="">Any time (no filter)</option> |
|
<option value="pd">Last 24 hours</option> |
|
<option value="pw">Past week</option> |
|
<option value="pm">Past month</option> |
|
<option value="py">Past year</option> |
|
<option value="custom">Custom range...</option> |
|
</select> |
|
</div> |
|
<div id="customDateRange" style="display:none; margin-left: 20px;"> |
|
<label for="fromDate">From:</label> |
|
<input type="date" id="fromDate" name="fromDate" /> |
|
<label for="toDate">To:</label> |
|
<input type="date" id="toDate" name="toDate" /> |
|
</div> |
|
<button type="submit">Search</button> |
|
</form> |
|
|
|
<div id="results"></div> |
|
|
|
<script> |
|
const API_KEY = 'YOUR_API_KEY_HERE'; |
|
|
|
|
|
function toggleDateInputs() { |
|
const freshnessValue = document.getElementById('freshness').value; |
|
const customRangeDiv = document.getElementById('customDateRange'); |
|
if (freshnessValue === 'custom') { |
|
customRangeDiv.style.display = 'block'; |
|
} else { |
|
customRangeDiv.style.display = 'none'; |
|
} |
|
} |
|
|
|
|
|
document.getElementById('searchForm').addEventListener('submit', function(event) { |
|
event.preventDefault(); |
|
|
|
|
|
const query = document.getElementById('query').value.trim(); |
|
const country = document.getElementById('country').value; |
|
const language = document.getElementById('language').value; |
|
const safesearch = document.getElementById('safesearch').value; |
|
const freshnessSelection = document.getElementById('freshness').value; |
|
|
|
|
|
let freshnessParam = ""; |
|
if (freshnessSelection === 'custom') { |
|
|
|
const fromDate = document.getElementById('fromDate').value; |
|
const toDate = document.getElementById('toDate').value; |
|
if (fromDate && toDate) { |
|
freshnessParam = `${fromDate}to${toDate}`; |
|
} else { |
|
freshnessParam = ""; |
|
} |
|
} else if (freshnessSelection !== "") { |
|
|
|
freshnessParam = freshnessSelection; |
|
} |
|
|
|
|
|
|
|
const params = new URLSearchParams(); |
|
params.append('q', query); |
|
params.append('country', country); |
|
params.append('search_lang', language); |
|
params.append('safesearch', safesearch); |
|
if (freshnessParam) { |
|
params.append('freshness', freshnessParam); |
|
} |
|
|
|
|
|
|
|
const headers = { |
|
'Accept': 'application/json', |
|
'Accept-Encoding': 'gzip', |
|
'X-Subscription-Token': API_KEY |
|
}; |
|
|
|
|
|
fetch(`https://api.search.brave.com/res/v1/news/search?${params.toString()}`, { headers: headers }) |
|
.then(response => { |
|
if (!response.ok) { |
|
throw new Error(`API request failed with status ${response.status}`); |
|
} |
|
return response.json(); |
|
}) |
|
.then(data => { |
|
console.log("API response:", data); |
|
|
|
const resultsDiv = document.getElementById('results'); |
|
resultsDiv.innerHTML = ""; |
|
if (data.news && data.news.results) { |
|
data.news.results.forEach(item => { |
|
const p = document.createElement('p'); |
|
p.innerHTML = `<a href="${item.url}" target="_blank">${item.title}</a><br><small>${item.source.name} – ${item.date}</small>`; |
|
resultsDiv.appendChild(p); |
|
}); |
|
} else { |
|
resultsDiv.textContent = "No results found."; |
|
} |
|
}) |
|
.catch(error => { |
|
console.error("Error fetching search results:", error); |
|
document.getElementById('results').textContent = "Error: " + error.message; |
|
}); |
|
}); |
|
</script> |
|
</body> |
|
</html> |
|
|