|
1 | 1 | (function() { |
2 | 2 | 'use strict'; |
3 | 3 |
|
4 | | - // Read a page's GET URL variables and return them as an associative array. |
5 | | - // Source: https://jquery-howto.blogspot.com/2009/09/get-url-parameters-values-with-jquery.html |
| 4 | + const allowedQueryParams = new Set(['user', 'repo', 'type', 'count', 'size', 'text', 'v']); |
| 5 | + |
6 | 6 | function getUrlParameters() { |
7 | | - var vars = []; |
8 | | - var hash; |
9 | | - var location = window.location; |
10 | | - var hashes = location.href.slice(location.href.indexOf('?') + 1).split('&'); |
11 | | - |
12 | | - for (var i = 0; i < hashes.length; i++) { |
13 | | - hash = hashes[i].split('='); |
14 | | - vars.push(hash[0]); |
15 | | - vars[hash[0]] = hash[1]; |
| 7 | + // TODO: Replace with URLSearchParams later |
| 8 | + const hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&'); |
| 9 | + const parameters = new Map(); |
| 10 | + |
| 11 | + for (const hash of hashes) { |
| 12 | + const [parameter, value] = hash.split('='); |
| 13 | + |
| 14 | + if (allowedQueryParams.has(parameter)) { |
| 15 | + parameters.set(parameter, value); |
| 16 | + } |
16 | 17 | } |
17 | 18 |
|
18 | | - return vars; |
| 19 | + return parameters; |
19 | 20 | } |
20 | 21 |
|
21 | 22 | // Add commas to numbers |
|
24 | 25 | } |
25 | 26 |
|
26 | 27 | function jsonp(path) { |
27 | | - var head = document.head; |
28 | | - var script = document.createElement('script'); |
| 28 | + const script = document.createElement('script'); |
29 | 29 |
|
30 | | - script.src = path + '?callback=callback'; |
31 | | - head.insertBefore(script, head.firstChild); |
| 30 | + script.src = `${path}?callback=callback`; |
| 31 | + document.head.insertBefore(script, document.head.firstChild); |
32 | 32 | } |
33 | 33 |
|
34 | | - var parameters = getUrlParameters(); |
35 | | - |
36 | 34 | // Parameters |
37 | | - var user = parameters.user; |
38 | | - var repo = parameters.repo; |
39 | | - var type = parameters.type; |
40 | | - var count = parameters.count; |
41 | | - var size = parameters.size; |
42 | | - var v = parameters.v; |
43 | | - var noText = parameters.text; |
| 35 | + const parameters = getUrlParameters(); |
| 36 | + const user = parameters.get('user'); |
| 37 | + const repo = parameters.get('repo'); |
| 38 | + const type = parameters.get('type'); |
| 39 | + const count = parameters.get('count'); |
| 40 | + const size = parameters.get('size'); |
| 41 | + const noText = parameters.get('text'); |
| 42 | + const v = parameters.get('v'); |
44 | 43 |
|
45 | 44 | // Elements |
46 | | - var button = document.querySelector('.gh-btn'); |
47 | | - var mainButton = document.querySelector('.github-btn'); |
48 | | - var text = document.querySelector('.gh-text'); |
49 | | - var counter = document.querySelector('.gh-count'); |
| 45 | + const button = document.querySelector('.gh-btn'); |
| 46 | + const mainButton = document.querySelector('.github-btn'); |
| 47 | + const text = document.querySelector('.gh-text'); |
| 48 | + const counter = document.querySelector('.gh-count'); |
50 | 49 |
|
51 | 50 | // Constants |
52 | | - var LABEL_SUFFIX = ' on GitHub'; |
53 | | - var GITHUB_URL = 'https://github.com/'; |
54 | | - var API_URL = 'https://api.github.com/'; |
55 | | - var REPO_URL = GITHUB_URL + user + '/' + repo; |
56 | | - var USER_REPO = user + '/' + repo; |
| 51 | + const LABEL_SUFFIX = 'on GitHub'; |
| 52 | + const GITHUB_URL = 'https://github.com/'; |
| 53 | + const API_URL = 'https://api.github.com/'; |
| 54 | + const REPO_URL = `${GITHUB_URL + user}/${repo}`; |
| 55 | + const USER_REPO = `${user}/${repo}`; |
57 | 56 |
|
58 | 57 | window.callback = function(obj) { |
59 | 58 | if (obj.data.message === 'Not Found') { |
|
64 | 63 | case 'watch': { |
65 | 64 | if (v === '2') { |
66 | 65 | counter.textContent = obj.data.subscribers_count && addCommas(obj.data.subscribers_count); |
67 | | - counter.setAttribute('aria-label', counter.textContent + ' watchers' + LABEL_SUFFIX); |
| 66 | + counter.setAttribute('aria-label', `${counter.textContent} watchers ${LABEL_SUFFIX}`); |
68 | 67 | } else { |
69 | 68 | counter.textContent = obj.data.stargazers_count && addCommas(obj.data.stargazers_count); |
70 | | - counter.setAttribute('aria-label', counter.textContent + ' stargazers' + LABEL_SUFFIX); |
| 69 | + counter.setAttribute('aria-label', `${counter.textContent} stargazers ${LABEL_SUFFIX}`); |
71 | 70 | } |
72 | 71 |
|
73 | 72 | break; |
74 | 73 | } |
75 | 74 |
|
76 | 75 | case 'star': { |
77 | 76 | counter.textContent = obj.data.stargazers_count && addCommas(obj.data.stargazers_count); |
78 | | - counter.setAttribute('aria-label', counter.textContent + ' stargazers' + LABEL_SUFFIX); |
| 77 | + counter.setAttribute('aria-label', `${counter.textContent} stargazers ${LABEL_SUFFIX}`); |
79 | 78 | break; |
80 | 79 | } |
81 | 80 |
|
82 | 81 | case 'fork': { |
83 | 82 | counter.textContent = obj.data.network_count && addCommas(obj.data.network_count); |
84 | | - counter.setAttribute('aria-label', counter.textContent + ' forks' + LABEL_SUFFIX); |
| 83 | + counter.setAttribute('aria-label', `${counter.textContent} forks ${LABEL_SUFFIX}`); |
85 | 84 | break; |
86 | 85 | } |
87 | 86 |
|
88 | 87 | case 'follow': { |
89 | 88 | counter.textContent = obj.data.followers && addCommas(obj.data.followers); |
90 | | - counter.setAttribute('aria-label', counter.textContent + ' followers' + LABEL_SUFFIX); |
| 89 | + counter.setAttribute('aria-label', `${counter.textContent} followers ${LABEL_SUFFIX}`); |
91 | 90 | break; |
92 | 91 | } |
93 | 92 | } |
|
102 | 101 | // Set href to be URL for repo |
103 | 102 | button.href = REPO_URL; |
104 | 103 |
|
105 | | - var title; |
| 104 | + let title; |
106 | 105 |
|
107 | 106 | // Add the class, change the text label, set count link href |
108 | 107 | switch (type) { |
109 | 108 | case 'watch': { |
110 | 109 | if (v === '2') { |
111 | | - mainButton.className += ' github-watchers'; |
| 110 | + mainButton.classList.add('github-watchers'); |
112 | 111 | text.textContent = 'Watch'; |
113 | | - counter.href = REPO_URL + '/watchers'; |
| 112 | + counter.href = `${REPO_URL}/watchers`; |
114 | 113 | } else { |
115 | | - mainButton.className += ' github-stargazers'; |
| 114 | + mainButton.classList.add('github-stargazers'); |
116 | 115 | text.textContent = 'Star'; |
117 | | - counter.href = REPO_URL + '/stargazers'; |
| 116 | + counter.href = `${REPO_URL}/stargazers`; |
118 | 117 | } |
119 | 118 |
|
120 | | - title = text.textContent + ' ' + USER_REPO; |
| 119 | + title = `${text.textContent} ${USER_REPO}`; |
121 | 120 | break; |
122 | 121 | } |
123 | 122 |
|
124 | 123 | case 'star': { |
125 | | - mainButton.className += ' github-stargazers'; |
| 124 | + mainButton.classList.add('github-stargazers'); |
126 | 125 | text.textContent = 'Star'; |
127 | | - counter.href = REPO_URL + '/stargazers'; |
128 | | - title = text.textContent + ' ' + USER_REPO; |
| 126 | + counter.href = `${REPO_URL}/stargazers`; |
| 127 | + title = `${text.textContent} ${USER_REPO}`; |
129 | 128 | break; |
130 | 129 | } |
131 | 130 |
|
132 | 131 | case 'fork': { |
133 | | - mainButton.className += ' github-forks'; |
| 132 | + mainButton.classList.add('github-forks'); |
134 | 133 | text.textContent = 'Fork'; |
135 | | - button.href = REPO_URL + '/fork'; |
136 | | - counter.href = REPO_URL + '/network'; |
137 | | - title = text.textContent + ' ' + USER_REPO; |
| 134 | + button.href = `${REPO_URL}/fork`; |
| 135 | + counter.href = `${REPO_URL}/network`; |
| 136 | + title = `${text.textContent} ${USER_REPO}`; |
138 | 137 | break; |
139 | 138 | } |
140 | 139 |
|
141 | 140 | case 'follow': { |
142 | | - mainButton.className += ' github-me'; |
143 | | - text.textContent = 'Follow @' + user; |
| 141 | + mainButton.classList.add('github-me'); |
| 142 | + text.textContent = `Follow @${user}`; |
144 | 143 | button.href = GITHUB_URL + user; |
145 | | - counter.href = GITHUB_URL + user + '?tab=followers'; |
| 144 | + counter.href = `${GITHUB_URL + user}?tab=followers`; |
146 | 145 | title = text.textContent; |
147 | 146 | break; |
148 | 147 | } |
149 | 148 |
|
150 | 149 | case 'sponsor': { |
151 | | - mainButton.className += ' github-me'; |
152 | | - text.textContent = 'Sponsor @' + user; |
153 | | - button.href = GITHUB_URL + 'sponsors/' + user; |
| 150 | + mainButton.classList.add('github-me'); |
| 151 | + text.textContent = `Sponsor @${user}`; |
| 152 | + button.href = `${GITHUB_URL}sponsors/${user}`; |
154 | 153 | title = text.textContent; |
155 | 154 | break; |
156 | 155 | } |
157 | 156 | } |
158 | 157 |
|
159 | 158 | if (noText === 'false') { |
160 | | - button.className += ' no-text'; |
| 159 | + button.classList.add('no-text'); |
161 | 160 | text.setAttribute('aria-hidden', true); |
162 | 161 | text.style.display = 'none'; |
163 | 162 | text.textContent = ''; |
164 | 163 | } |
165 | 164 |
|
166 | | - button.setAttribute('aria-label', title + LABEL_SUFFIX); |
167 | | - document.title = title + LABEL_SUFFIX; |
| 165 | + button.setAttribute('aria-label', `${title} ${LABEL_SUFFIX}`); |
| 166 | + document.title = `${title} ${LABEL_SUFFIX}`; |
168 | 167 |
|
169 | 168 | // Change the size if requested |
170 | 169 | if (size === 'large') { |
171 | | - mainButton.className += ' github-btn-large'; |
| 170 | + mainButton.classList.add('github-btn-large'); |
172 | 171 | } |
173 | 172 |
|
174 | 173 | // If count is not requested or type is sponsor, |
|
178 | 177 | } |
179 | 178 |
|
180 | 179 | if (type === 'follow') { |
181 | | - jsonp(API_URL + 'users/' + user); |
| 180 | + jsonp(`${API_URL}users/${user}`); |
182 | 181 | } else { |
183 | | - jsonp(API_URL + 'repos/' + user + '/' + repo); |
| 182 | + jsonp(`${API_URL}repos/${user}/${repo}`); |
184 | 183 | } |
185 | 184 | })(); |
0 commit comments