|
1 | | -let is_searching = false; |
2 | | -let pattern_domain = "^[a-zA-Z0-9_][a-zA-Z0-9.-]*"; |
3 | | -let timer_search = undefined; |
4 | | -let reserved_domain = undefined; |
5 | | -const search_domain = document.getElementById("search-domain"); |
6 | | -const result_card = document.getElementById("result-card"); |
7 | | -const result_none = document.getElementById("result-none"); |
8 | | -const result_error = document.getElementById("result-error"); |
9 | | -const result_reserved = document.getElementById("result-reserved"); |
10 | | -const user_avatar = document.getElementById("user-avatar"); |
11 | | -const user_gh_link = document.getElementById("user-gh"); |
12 | | -const user_gh_name = document.getElementById("user-gh-name"); |
| 1 | +let isSearching = false; |
| 2 | +let reservedDomains; |
| 3 | +let searchTimeout; |
| 4 | +const DOMAIN_PATTERN = /^[a-zA-Z0-9_][a-zA-Z0-9.-]*/; |
13 | 5 |
|
14 | | -function check() { |
15 | | - const search = search_domain.value.trim().replace(/\s/g, "-").toLowerCase(); |
16 | | - if (isReserved(search)) { |
17 | | - hideResults(); |
18 | | - result_reserved.classList.toggle("hidden"); |
19 | | - return; |
20 | | - } |
21 | | - if (!search || is_searching) return; |
22 | | - is_searching = true; |
23 | | - fetch(`https://raw.githubusercontent.com/is-a-dev/register/main/domains/${search}.json`) |
24 | | - .then((response) => { |
25 | | - hideResults(); |
26 | | - if (!response.ok && response.status !== 404) throw new Error("Something went wrong"); |
27 | | - return !response.ok ? null : response.json(); |
28 | | - }) |
29 | | - .then((json) => { |
30 | | - if (json) { |
31 | | - json.search = search; |
32 | | - propagateResult(json); |
33 | | - } else if (result_none.classList.contains("hidden")) result_none.classList.toggle("hidden"); |
34 | | - }) |
35 | | - .catch((reason) => { |
36 | | - console.log(reason); |
37 | | - if (result_error.classList.contains("hidden")) result_error.classList.toggle("hidden"); |
38 | | - }) |
39 | | - .finally(() => { |
40 | | - is_searching = false; |
41 | | - }); |
42 | | -} |
| 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"); |
43 | 14 |
|
44 | | -function propagateResult(data) { |
45 | | - if (result_card.classList.contains("hidden")) result_card.classList.remove("hidden"); |
46 | | - var gh_link = `https://github.com/${data.owner.username}`; |
47 | | - user_avatar.src = `${gh_link}.png?size=64`; |
48 | | - user_gh_link.href = gh_link; |
49 | | - user_gh_name.innerHTML = data.owner.username; |
| 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 | + } |
50 | 27 | } |
51 | 28 |
|
52 | 29 | function hideResults() { |
53 | | - if (!result_card.classList.contains("hidden")) result_card.classList.toggle("hidden"); |
54 | | - if (!result_none.classList.contains("hidden")) result_none.classList.toggle("hidden"); |
55 | | - if (!result_error.classList.contains("hidden")) result_error.classList.toggle("hidden"); |
56 | | - if (!result_reserved.classList.contains("hidden")) result_reserved.classList.toggle("hidden"); |
| 30 | + [resultCard, resultNone, resultError, resultReserved].forEach((el) => el.classList.add("hidden")); |
57 | 31 | } |
58 | 32 |
|
59 | 33 | function isReserved(domain) { |
60 | | - const reserved_domains = new Set(reserved_domain); |
61 | 34 | return ( |
62 | | - reserved_domains.has(domain) || |
63 | | - reserved_domain.some((pattern) => pattern.includes("[") && new RegExp(pattern).test(domain)) |
| 35 | + reservedDomains?.includes(domain) || |
| 36 | + reservedDomains?.some((pattern) => /\[/.test(pattern) && new RegExp(pattern).test(domain)) |
64 | 37 | ); |
65 | 38 | } |
66 | 39 |
|
67 | | -async function fetchReservedDomains() { |
68 | | - try { |
69 | | - if (typeof reserved_domain === "undefined") { |
70 | | - if (typeof timer_search !== "undefined") { |
71 | | - clearInterval(timer_search); |
72 | | - } |
| 40 | +async function check() { |
| 41 | + const search = searchDomain.value.trim().replace(/\s/g, "-").toLowerCase(); |
| 42 | + if (!search || isSearching) return; |
73 | 43 |
|
74 | | - const response = await fetch("https://raw.githubusercontent.com/is-a-dev/register/main/util/reserved.json"); |
75 | | - if (!response.ok) { |
76 | | - throw new Error("Something went wrong"); |
77 | | - } |
| 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"); |
78 | 55 |
|
79 | | - reserved_domain = await response.json(); |
80 | | - search_domain.disabled = false; |
81 | | - search_domain.placeholder = "e.g. william"; |
| 56 | + const data = response.ok ? await response.json() : null; |
| 57 | + if (data) { |
| 58 | + propagateResult(data, search); |
| 59 | + } else { |
| 60 | + resultNone.classList.remove("hidden"); |
82 | 61 | } |
83 | 62 | } catch (error) { |
84 | | - console.error("Error fetching reserved domains:", error); |
| 63 | + console.error(error.message); |
| 64 | + resultError.classList.remove("hidden"); |
| 65 | + } finally { |
| 66 | + isSearching = false; |
85 | 67 | } |
86 | 68 | } |
87 | 69 |
|
88 | | -search_domain.addEventListener("input", function (evt) { |
89 | | - const input_value = this.value.trim(); |
| 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(); |
90 | 80 |
|
91 | | - if (input_value === "") { |
92 | | - clearTimer(); |
| 81 | + if (!inputValue) { |
| 82 | + clearTimeout(searchTimeout); |
93 | 83 | hideResults(); |
94 | 84 | return; |
95 | 85 | } |
96 | 86 |
|
97 | | - const matched_value = input_value.match(new RegExp(pattern_domain)); |
98 | | - if (matched_value) { |
99 | | - search_domain.value = matched_value[0]; |
| 87 | + const matchedValue = inputValue.match(DOMAIN_PATTERN); |
| 88 | + if (matchedValue) { |
| 89 | + searchDomain.value = matchedValue[0]; |
100 | 90 | } |
101 | 91 |
|
102 | | - clearTimer(); |
103 | | - timer_search = setTimeout(() => check(), 1000); |
| 92 | + clearTimeout(searchTimeout); |
| 93 | + searchTimeout = setTimeout(check, 1000); |
104 | 94 | }); |
105 | 95 |
|
106 | | -function clearTimer() { |
107 | | - if (timer_search !== undefined) { |
108 | | - clearInterval(timer_search); |
109 | | - } |
110 | | -} |
111 | | - |
112 | 96 | fetchReservedDomains(); |
0 commit comments