|
| 1 | +let isSearching = false; |
| 2 | +let reservedDomains; |
| 3 | +let searchTimeout; |
| 4 | +const DOMAIN_PATTERN = /^[a-zA-Z0-9_][a-zA-Z0-9.-]*/; |
| 5 | + |
| 6 | +const searchDomain = document.getElementById("search-domain"); |
| 7 | +const resultCard = document.getElementById("result-card"); |
| 8 | +const resultNone = document.getElementById("result-none"); |
| 9 | +const resultError = document.getElementById("result-error"); |
| 10 | +const resultReserved = document.getElementById("result-reserved"); |
| 11 | +const userAvatar = document.getElementById("user-avatar"); |
| 12 | +const userGitHubLink = document.getElementById("user-gh"); |
| 13 | +const userGitHubName = document.getElementById("user-gh-name"); |
| 14 | + |
| 15 | +async function fetchReservedDomains() { |
| 16 | + try { |
| 17 | + if (!reservedDomains) { |
| 18 | + const response = await fetch("https://raw.githubusercontent.com/is-a-dev/register/main/util/reserved.json"); |
| 19 | + if (!response.ok) throw new Error(`Failed to fetch reserved domains: ${response.statusText}`); |
| 20 | + reservedDomains = await response.json(); |
| 21 | + searchDomain.disabled = false; |
| 22 | + searchDomain.placeholder = "e.g. william"; |
| 23 | + } |
| 24 | + } catch (error) { |
| 25 | + console.error(error.message); |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +function hideResults() { |
| 30 | + [resultCard, resultNone, resultError, resultReserved].forEach((el) => el.classList.add("hidden")); |
| 31 | +} |
| 32 | + |
| 33 | +function isReserved(domain) { |
| 34 | + return ( |
| 35 | + reservedDomains?.includes(domain) || |
| 36 | + reservedDomains?.some((pattern) => /\[/.test(pattern) && new RegExp(pattern).test(domain)) |
| 37 | + ); |
| 38 | +} |
| 39 | + |
| 40 | +async function check() { |
| 41 | + const search = searchDomain.value.trim().replace(/\s/g, "-").toLowerCase(); |
| 42 | + if (!search || isSearching) return; |
| 43 | + |
| 44 | + hideResults(); |
| 45 | + |
| 46 | + if (isReserved(search)) { |
| 47 | + resultReserved.classList.remove("hidden"); |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + isSearching = true; |
| 52 | + try { |
| 53 | + const response = await fetch(`https://raw.githubusercontent.com/is-a-dev/register/main/domains/${search}.json`); |
| 54 | + if (!response.ok && response.status !== 404) throw new Error("Something went wrong"); |
| 55 | + |
| 56 | + const data = response.ok ? await response.json() : null; |
| 57 | + if (data) { |
| 58 | + propagateResult(data, search); |
| 59 | + } else { |
| 60 | + resultNone.classList.remove("hidden"); |
| 61 | + } |
| 62 | + } catch (error) { |
| 63 | + console.error(error.message); |
| 64 | + resultError.classList.remove("hidden"); |
| 65 | + } finally { |
| 66 | + isSearching = false; |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +function propagateResult(data, search) { |
| 71 | + resultCard.classList.remove("hidden"); |
| 72 | + const githubURL = `https://github.com/${data.owner.username}`; |
| 73 | + userAvatar.src = `${githubURL}.png?size=64`; |
| 74 | + userGitHubLink.href = githubURL; |
| 75 | + userGitHubName.textContent = data.owner.username; |
| 76 | +} |
| 77 | + |
| 78 | +searchDomain.addEventListener("input", function () { |
| 79 | + const inputValue = this.value.trim(); |
| 80 | + |
| 81 | + if (!inputValue) { |
| 82 | + clearTimeout(searchTimeout); |
| 83 | + hideResults(); |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + const matchedValue = inputValue.match(DOMAIN_PATTERN); |
| 88 | + if (matchedValue) { |
| 89 | + searchDomain.value = matchedValue[0]; |
| 90 | + } |
| 91 | + |
| 92 | + clearTimeout(searchTimeout); |
| 93 | + searchTimeout = setTimeout(check, 1000); |
| 94 | +}); |
| 95 | + |
| 96 | +fetchReservedDomains(); |
0 commit comments