Changed around line 1
- document.addEventListener('DOMContentLoaded', () => {
- const brandInput = document.getElementById('brandInput');
- const searchButton = document.getElementById('searchButton');
- const resultContainer = document.getElementById('resultContainer');
+ document.addEventListener("DOMContentLoaded", () => {
+ const brandInput = document.getElementById("brandInput");
+ const searchButton = document.getElementById("searchButton");
+ const resultContainer = document.getElementById("resultContainer");
- // Simulate API call with random result
- return new Promise((resolve) => {
- setTimeout(() => {
- const isAvailable = Math.random() > 0.5;
- resolve(isAvailable);
- }, 800);
- });
+ try {
+ // Fetch data from DuckDuckGo API
+ const response = await fetch(
+ `https://api.duckduckgo.com/?q=${encodeURIComponent(brandName)}&format=json&no_html=1`,
+ );
+ const data = await response.json();
+
+ // If the API returns an abstract, assume the brand is in use
+ const isAvailable = !data.AbstractText;
+ return isAvailable;
+ } catch (error) {
+ console.error("Error fetching data:", error);
+ throw new Error("An error occurred while checking availability.");
+ }
- resultContainer.textContent = '';
- const resultMessage = document.createElement('p');
- resultMessage.textContent = isAvailable
- ? '✅ Brand name is available!'
- : '❌ Brand name is already in use';
- resultMessage.style.color = isAvailable ? 'var(--success-color)' : 'var(--error-color)';
+ resultContainer.textContent = "";
+ const resultMessage = document.createElement("p");
+ resultMessage.textContent = isAvailable
+ ? "✅ Brand name is available!"
+ : "❌ Brand name is already in use";
+ resultMessage.style.color = isAvailable
+ ? "var(--success-color)"
+ : "var(--error-color)";
- searchButton.addEventListener('click', async () => {
+ searchButton.addEventListener("click", async () => {
- resultContainer.textContent = 'Checking availability...';
- const isAvailable = await checkBrandAvailability(brandName);
- displayResult(isAvailable);
+ resultContainer.textContent = "Checking availability...";
+ try {
+ const isAvailable = await checkBrandAvailability(brandName);
+ displayResult(isAvailable);
+ } catch (error) {
+ resultContainer.textContent = error.message;
+ resultContainer.style.color = "var(--error-color)";
+ }
- resultContainer.textContent = 'Please enter a brand name';
- resultContainer.style.color = 'var(--error-color)';
+ resultContainer.textContent = "Please enter a brand name";
+ resultContainer.style.color = "var(--error-color)";
- brandInput.addEventListener('keypress', (e) => {
- if (e.key === 'Enter') {
+ brandInput.addEventListener("keypress", (e) => {
+ if (e.key === "Enter") {
- });
+ });