|
4 | 4 | const allowedQueryParams = new Set(['user', 'repo', 'type', 'count', 'size', 'text', 'v']);
|
5 | 5 |
|
6 | 6 | function getUrlParameters() {
|
7 |
| - // TODO: Replace with URLSearchParams later |
8 |
| - const hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&'); |
| 7 | + const searchParams = new URLSearchParams(globalThis.location.search); |
9 | 8 | const parameters = new Map();
|
10 | 9 |
|
11 |
| - for (const hash of hashes) { |
12 |
| - const [parameter, value] = hash.split('='); |
13 |
| - |
14 |
| - if (allowedQueryParams.has(parameter)) { |
15 |
| - parameters.set(parameter, value); |
| 10 | + for (const [param, value] of searchParams.entries()) { |
| 11 | + if (allowedQueryParams.has(param)) { |
| 12 | + parameters.set(param, value); |
16 | 13 | }
|
17 | 14 | }
|
18 | 15 |
|
|
21 | 18 |
|
22 | 19 | // Add commas to numbers
|
23 | 20 | function addCommas(n) {
|
24 |
| - // eslint-disable-next-line unicorn/prefer-string-replace-all |
25 |
| - return String(n).replace(/(\d)(?=(\d{3})+$)/g, '$1,'); |
| 21 | + return new Intl.NumberFormat().format(n); |
26 | 22 | }
|
27 | 23 |
|
28 |
| - function jsonp(path) { |
29 |
| - const script = document.createElement('script'); |
| 24 | + async function fetchData(path) { |
| 25 | + try { |
| 26 | + const response = await fetch(path); |
| 27 | + if (!response.ok) { |
| 28 | + throw new Error('Network response was not ok'); |
| 29 | + } |
30 | 30 |
|
31 |
| - script.src = `${path}?callback=callback`; |
32 |
| - document.head.insertBefore(script, document.head.firstChild); |
| 31 | + return await response.json(); |
| 32 | + } catch (error) { |
| 33 | + console.error(`Error fetching data: ${error}`); |
| 34 | + return { data: { message: 'Not Found' } }; |
| 35 | + } |
33 | 36 | }
|
34 | 37 |
|
35 | 38 | // Parameters
|
|
55 | 58 | const REPO_URL = `${GITHUB_URL + user}/${repo}`;
|
56 | 59 | const USER_REPO = `${user}/${repo}`;
|
57 | 60 |
|
58 |
| - window.callback = function(obj) { |
59 |
| - if (obj.data.message === 'Not Found') { |
| 61 | + async function processApiData(data) { |
| 62 | + if (data.message === 'Not Found') { |
60 | 63 | return;
|
61 | 64 | }
|
62 | 65 |
|
63 | 66 | switch (type) {
|
64 | 67 | case 'watch': {
|
65 | 68 | if (v === '2') {
|
66 |
| - counter.textContent = obj.data.subscribers_count && addCommas(obj.data.subscribers_count); |
| 69 | + counter.textContent = data.subscribers_count && addCommas(data.subscribers_count); |
67 | 70 | counter.setAttribute('aria-label', `${counter.textContent} watchers ${LABEL_SUFFIX}`);
|
68 | 71 | } else {
|
69 |
| - counter.textContent = obj.data.stargazers_count && addCommas(obj.data.stargazers_count); |
| 72 | + counter.textContent = data.stargazers_count && addCommas(data.stargazers_count); |
70 | 73 | counter.setAttribute('aria-label', `${counter.textContent} stargazers ${LABEL_SUFFIX}`);
|
71 | 74 | }
|
72 | 75 |
|
73 | 76 | break;
|
74 | 77 | }
|
75 | 78 |
|
76 | 79 | case 'star': {
|
77 |
| - counter.textContent = obj.data.stargazers_count && addCommas(obj.data.stargazers_count); |
| 80 | + counter.textContent = data.stargazers_count && addCommas(data.stargazers_count); |
78 | 81 | counter.setAttribute('aria-label', `${counter.textContent} stargazers ${LABEL_SUFFIX}`);
|
79 | 82 | break;
|
80 | 83 | }
|
81 | 84 |
|
82 | 85 | case 'fork': {
|
83 |
| - counter.textContent = obj.data.network_count && addCommas(obj.data.network_count); |
| 86 | + counter.textContent = data.network_count && addCommas(data.network_count); |
84 | 87 | counter.setAttribute('aria-label', `${counter.textContent} forks ${LABEL_SUFFIX}`);
|
85 | 88 | break;
|
86 | 89 | }
|
87 | 90 |
|
88 | 91 | case 'follow': {
|
89 |
| - counter.textContent = obj.data.followers && addCommas(obj.data.followers); |
| 92 | + counter.textContent = data.followers && addCommas(data.followers); |
90 | 93 | counter.setAttribute('aria-label', `${counter.textContent} followers ${LABEL_SUFFIX}`);
|
91 | 94 | break;
|
92 | 95 | }
|
|
97 | 100 | counter.style.display = 'block';
|
98 | 101 | counter.removeAttribute('aria-hidden');
|
99 | 102 | }
|
100 |
| - }; |
| 103 | + } |
101 | 104 |
|
102 | 105 | // Set href to be URL for repo
|
103 | 106 | button.href = REPO_URL;
|
|
177 | 180 | return;
|
178 | 181 | }
|
179 | 182 |
|
180 |
| - if (type === 'follow') { |
181 |
| - jsonp(`${API_URL}users/${user}`); |
182 |
| - } else { |
183 |
| - jsonp(`${API_URL}repos/${user}/${repo}`); |
184 |
| - } |
| 183 | + (async() => { |
| 184 | + try { |
| 185 | + const apiPath = type === 'follow' ? `${API_URL}users/${user}` : `${API_URL}repos/${user}/${repo}`; |
| 186 | + const data = await fetchData(apiPath); |
| 187 | + |
| 188 | + await processApiData(data); |
| 189 | + } catch (error) { |
| 190 | + console.error(`Error fetching GitHub data: ${error}`); |
| 191 | + } |
| 192 | + })(); |
185 | 193 | })();
|
0 commit comments