|
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 |
| - |
| 10 | + for (const [parameter, value] of searchParams.entries()) { |
14 | 11 | if (allowedQueryParams.has(parameter)) {
|
15 | 12 | parameters.set(parameter, value);
|
16 | 13 | }
|
|
19 | 16 | return parameters;
|
20 | 17 | }
|
21 | 18 |
|
22 |
| - // Add commas to numbers |
| 19 | + // Format 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('en-US').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 { |
| 35 | + data: { |
| 36 | + message: 'Not Found' |
| 37 | + } |
| 38 | + }; |
| 39 | + } |
33 | 40 | }
|
34 | 41 |
|
35 | 42 | // Parameters
|
|
55 | 62 | const REPO_URL = `${GITHUB_URL + user}/${repo}`;
|
56 | 63 | const USER_REPO = `${user}/${repo}`;
|
57 | 64 |
|
58 |
| - window.callback = function(obj) { |
59 |
| - if (obj.data.message === 'Not Found') { |
| 65 | + async function processApiData(data) { |
| 66 | + if (data.message === 'Not Found') { |
60 | 67 | return;
|
61 | 68 | }
|
62 | 69 |
|
63 | 70 | switch (type) {
|
64 | 71 | case 'watch': {
|
65 | 72 | if (v === '2') {
|
66 |
| - counter.textContent = obj.data.subscribers_count && addCommas(obj.data.subscribers_count); |
| 73 | + counter.textContent = data.subscribers_count && addCommas(data.subscribers_count); |
67 | 74 | counter.setAttribute('aria-label', `${counter.textContent} watchers ${LABEL_SUFFIX}`);
|
68 | 75 | } else {
|
69 |
| - counter.textContent = obj.data.stargazers_count && addCommas(obj.data.stargazers_count); |
| 76 | + counter.textContent = data.stargazers_count && addCommas(data.stargazers_count); |
70 | 77 | counter.setAttribute('aria-label', `${counter.textContent} stargazers ${LABEL_SUFFIX}`);
|
71 | 78 | }
|
72 | 79 |
|
73 | 80 | break;
|
74 | 81 | }
|
75 | 82 |
|
76 | 83 | case 'star': {
|
77 |
| - counter.textContent = obj.data.stargazers_count && addCommas(obj.data.stargazers_count); |
| 84 | + counter.textContent = data.stargazers_count && addCommas(data.stargazers_count); |
78 | 85 | counter.setAttribute('aria-label', `${counter.textContent} stargazers ${LABEL_SUFFIX}`);
|
79 | 86 | break;
|
80 | 87 | }
|
81 | 88 |
|
82 | 89 | case 'fork': {
|
83 |
| - counter.textContent = obj.data.network_count && addCommas(obj.data.network_count); |
| 90 | + counter.textContent = data.network_count && addCommas(data.network_count); |
84 | 91 | counter.setAttribute('aria-label', `${counter.textContent} forks ${LABEL_SUFFIX}`);
|
85 | 92 | break;
|
86 | 93 | }
|
87 | 94 |
|
88 | 95 | case 'follow': {
|
89 |
| - counter.textContent = obj.data.followers && addCommas(obj.data.followers); |
| 96 | + counter.textContent = data.followers && addCommas(data.followers); |
90 | 97 | counter.setAttribute('aria-label', `${counter.textContent} followers ${LABEL_SUFFIX}`);
|
91 | 98 | break;
|
92 | 99 | }
|
|
97 | 104 | counter.style.display = 'block';
|
98 | 105 | counter.removeAttribute('aria-hidden');
|
99 | 106 | }
|
100 |
| - }; |
| 107 | + } |
101 | 108 |
|
102 | 109 | // Set href to be URL for repo
|
103 | 110 | button.href = REPO_URL;
|
|
177 | 184 | return;
|
178 | 185 | }
|
179 | 186 |
|
180 |
| - if (type === 'follow') { |
181 |
| - jsonp(`${API_URL}users/${user}`); |
182 |
| - } else { |
183 |
| - jsonp(`${API_URL}repos/${user}/${repo}`); |
184 |
| - } |
| 187 | + (async() => { |
| 188 | + try { |
| 189 | + const apiPath = type === 'follow' ? `${API_URL}users/${user}` : `${API_URL}repos/${user}/${repo}`; |
| 190 | + const data = await fetchData(apiPath); |
| 191 | + |
| 192 | + await processApiData(data); |
| 193 | + } catch (error) { |
| 194 | + console.error(`Error fetching GitHub data: ${error}`); |
| 195 | + } |
| 196 | + })(); |
185 | 197 | })();
|
0 commit comments