-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathscript.js
457 lines (396 loc) · 14.1 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
// Constants
const GITHUB_GRAPHQL_API_URL = "https://api.github.com/graphql";
const RATE_LIMIT_ERROR = "RATE_LIMITED";
const MAX_USERS_PER_REQUEST = 25;
// Get DOM elements
const analyzeButton = document.getElementById('analyze');
const usernamesTextarea = document.getElementById('usernames');
const tokenInput = document.getElementById('token');
const progressBar = document.getElementById('progress-inner');
const resultDiv = document.getElementById('result');
const connectionsTableDiv = document.getElementById('connections-table');
const failedUsernamesDiv = document.getElementById('failed-usernames');
const copyResultsButton = document.getElementById('copy-results');
const takeSnapshotButton = document.getElementById('take-snapshot');
const buttonContainer = document.querySelector('.button-container');
// Add ARIA labels and roles for accessibility
usernamesTextarea.setAttribute('aria-label', 'Enter GitHub usernames, one per line');
tokenInput.setAttribute('aria-label', 'GitHub Personal Access Token');
analyzeButton.setAttribute('aria-label', 'Analyze GitHub Connections');
progressBar.setAttribute('role', 'progressbar');
progressBar.setAttribute('aria-valuemin', '0');
progressBar.setAttribute('aria-valuemax', '100');
resultDiv.setAttribute('role', 'status');
resultDiv.setAttribute('aria-live', 'polite');
// Event Listeners
analyzeButton.addEventListener('click', analyzeConnections);
copyResultsButton.addEventListener('click', copyResults);
takeSnapshotButton.addEventListener('click', takeSnapshot);
// Theme switching functionality
const themeSwitch = document.querySelector('input[type="checkbox"]');
const body = document.body;
const themeLabel = document.getElementById('theme-label');
/**
* Validate GitHub Personal Access Token
* @param {string} token - GitHub Personal Access Token
* @returns {Promise<boolean>} - Whether the token is valid
*/
async function validateToken(token) {
try {
const response = await fetch('https://api.github.com/user', {
headers: {
'Accept': 'application/vnd.github.v3+json',
'Authorization': `token ${token}`
}
});
if (response.status === 401) {
throw new Error('Invalid token or insufficient permissions');
}
if (!response.ok) {
throw new Error(`GitHub API error: ${response.status}`);
}
const data = await response.json();
return true;
} catch (error) {
console.error('Token validation error:', error);
throw new Error(`Failed to validate token: ${error.message}`);
}
}
/**
* Clean and format GitHub username
* @param {string} username - Raw username input
* @returns {string} - Cleaned username
*/
function cleanUsername(username) {
return username.trim()
.replace(/^@/, '')
.replace(/^(https?:\/\/)?(www\.)?github\.com\//, '')
.replace(/[^a-zA-Z0-9-]+/g, '-')
.toLowerCase();
}
/**
* Main function to analyze GitHub connections
*/
async function analyzeConnections() {
try {
resetUI();
updateProgress(0);
// Get and validate input
const usernames = usernamesTextarea.value
.split('\n')
.map(cleanUsername)
.filter(username => username !== '');
const token = tokenInput.value.trim();
if (usernames.length < 2) {
throw new Error('Please enter at least two GitHub usernames.');
}
if (!token) {
throw new Error('Please provide a GitHub Personal Access Token.');
}
// Validate token
await validateToken(token);
resultDiv.textContent = 'Analyzing connections...';
resultDiv.setAttribute('aria-busy', 'true');
const connections = new Map();
const failedUsernames = new Set();
const totalChecks = usernames.length;
let checksCompleted = 0;
// Process users in batches to optimize API calls
for (let i = 0; i < usernames.length; i += MAX_USERS_PER_REQUEST) {
const batch = usernames.slice(i, i + MAX_USERS_PER_REQUEST);
const [userInfoData, followingData] = await getUserInfoAndFollowing(batch, token);
for (const username of batch) {
try {
const followingInList = followingData[username].filter(user =>
usernames.includes(user.login.toLowerCase())
);
connections.set(userInfoData[username], followingInList);
checksCompleted++;
updateProgress((checksCompleted / totalChecks) * 100);
} catch (error) {
console.error(`Error processing ${username}:`, error);
failedUsernames.add(username);
}
}
}
displayResults(connections, failedUsernames);
} catch (error) {
handleError(error);
} finally {
resultDiv.setAttribute('aria-busy', 'false');
}
}
/**
* Update progress bar and ARIA attributes
* @param {number} percentage - Progress percentage (0-100)
*/
function updateProgress(percentage) {
progressBar.style.width = `${percentage}%`;
progressBar.setAttribute('aria-valuenow', percentage);
}
/**
* Handle and display errors
* @param {Error} error - Error object
*/
function handleError(error) {
console.error('Application error:', error);
resultDiv.textContent = `Error: ${error.message}`;
resultDiv.style.color = '#dc3545';
if (error.message.includes('rate limit')) {
resultDiv.textContent += ' Please try again later.';
}
}
/**
* Fetch user information from GitHub API
* @param {string} username - GitHub username
* @param {string} token - GitHub Personal Access Token
* @returns {Object} - User information
*/
function getUserInfo(userData) {
console.log('Fetching user info for:', userData);
return {
login: userData.login,
name: userData.name || userData.login,
followers: userData.followers.totalCount,
following: userData.following.totalCount
};
}
async function fetchInitialFollowing(usernames, token) {
const queries = usernames.map((username, i) => `
user${i}: user(login: "${username}") {
login
name
followers {
totalCount
}
following(first: 100) {
totalCount
nodes {
login
name
}
pageInfo {
endCursor
hasNextPage
}
}
}
`);
const fullQuery = `query { ${queries.join(' ')} }`;
const headers = {
"Authorization": `Bearer ${token}`,
"Content-Type": "application/json"
};
const response = await fetch(GITHUB_GRAPHQL_API_URL, {
method: 'POST',
headers: headers,
body: JSON.stringify({ query: fullQuery })
});
const data = await response.json();
return data.data;
}
async function fetchAdditionalFollowing(username, cursor, token) {
const query = `
query($login: String!, $cursor: String) {
user(login: $login) {
following(first: 100, after: $cursor) {
nodes {
login
name
}
pageInfo {
endCursor
hasNextPage
}
}
}
}`;
const variables = { login: username, cursor: cursor };
const headers = {
"Authorization": `Bearer ${token}`,
"Content-Type": "application/json"
};
const response = await fetch(GITHUB_GRAPHQL_API_URL, {
method: 'POST',
headers: headers,
body: JSON.stringify({ query, variables })
});
const data = await response.json();
return data.data.user.following;
}
async function getAllFollowing(username, initialFollowing, token) {
let allFollowing = initialFollowing.nodes;
let cursor = initialFollowing.pageInfo.endCursor;
let hasNextPage = initialFollowing.pageInfo.hasNextPage;
while (hasNextPage) {
const { nodes, pageInfo } = await fetchAdditionalFollowing(username, cursor, token);
allFollowing = allFollowing.concat(nodes);
cursor = pageInfo.endCursor;
hasNextPage = pageInfo.hasNextPage;
}
return allFollowing;
}
/**
* Fetch all users that a given user is following
* @param {string} username - GitHub username
* @param {string} token - GitHub Personal Access Token
* @returns {Array} - Array of user objects the given user is following
*/
async function getUserInfoAndFollowing(usernames, token) {
const initialData = await fetchInitialFollowing(usernames, token);
const userInfo = {};
for (const userData of Object.values(initialData)) {
console.log(userData);
userInfo[userData.login.toLowerCase()] = getUserInfo(userData);
}
const followingData = {};
for (const [i, username] of usernames.entries()) {
const initialFollowing = initialData[`user${i}`].following;
if (initialFollowing.pageInfo.hasNextPage) {
followingData[username] = await getAllFollowing(username, initialFollowing, token);
} else {
followingData[username] = initialFollowing.nodes;
}
}
return [userInfo, followingData];
}
/**
* Display the analysis results in the UI
* @param {Map} connections - Map of user connections
* @param {Set} failedUsernames - Set of usernames that failed to fetch
*/
function displayResults(connections, failedUsernames) {
if (connections.size === 0) {
resultDiv.textContent = 'No valid connections found.';
return;
}
resultDiv.textContent = 'Analysis complete!';
// Create table
const table = document.createElement('table');
table.setAttribute('role', 'grid');
table.innerHTML = createTableHeader(connections);
table.innerHTML += createTableBody(connections);
connectionsTableDiv.innerHTML = '';
connectionsTableDiv.appendChild(table);
// Show copy and snapshot buttons
copyResultsButton.style.display = 'inline-block';
takeSnapshotButton.style.display = 'inline-block';
// Display failed usernames if any
if (failedUsernames.size > 0) {
displayFailedUsernames(failedUsernames);
}
}
function createTableHeader(connections) {
let headerHTML = `
<thead>
<tr>
<th>Name <span class="caret">►</span></th>
<th>Following <span class="caret">►</span></th>
<th>Count <span class="caret">►</span></th>
<th>Total Followers <span class="caret">►</span></th>
<th>Total Following <span class="caret">►</span></th>
</tr>
</thead>
`;
return headerHTML;
}
function createTableBody(connections) {
let bodyHTML = '<tbody>';
const sortedConnections = new Map([...connections.entries()].sort((a, b) => a[0].name.localeCompare(b[0].name)));
for (const [user, following] of sortedConnections) {
bodyHTML += `
<tr>
<td>${user.name}</td>
<td>${following.map(f => f.name).join(', ') || 'None'}</td>
<td>${following.length}</td>
<td>${user.followers}</td>
<td>${user.following}</td>
</tr>
`;
}
bodyHTML += '</tbody>';
return bodyHTML;
}
function displayFailedUsernames(failedUsernames) {
failedUsernamesDiv.style.display = 'block';
failedUsernamesDiv.innerHTML = `
<h3>Failed to fetch information for the following usernames:</h3>
<ul>
${Array.from(failedUsernames).map(username => `<li>${username}</li>`).join('')}
</ul>
`;
}
// Function to handle theme switching
function switchTheme(e) {
if (e.target.checked) {
body.setAttribute('data-theme', 'dark');
localStorage.setItem('theme', 'dark');
themeLabel.textContent = 'Dark Mode';
} else {
body.setAttribute('data-theme', 'light');
localStorage.setItem('theme', 'light');
themeLabel.textContent = 'Light Mode';
}
}
// Event listener for theme switch
themeSwitch.addEventListener('change', switchTheme);
// Check for saved theme preference and apply it
const currentTheme = localStorage.getItem('theme');
if (currentTheme) {
body.setAttribute('data-theme', currentTheme);
if (currentTheme === 'dark') {
themeSwitch.checked = true;
themeLabel.textContent = 'Dark Mode';
}
}
/**
* Copy the results table to the clipboard
*/
function copyResults() {
const table = document.querySelector('#connections-table table');
const range = document.createRange();
range.selectNode(table);
window.getSelection().removeAllRanges();
window.getSelection().addRange(range);
document.execCommand('copy');
window.getSelection().removeAllRanges();
alert('Results copied to clipboard!');
}
/**
* Take a snapshot of the current results and copy to clipboard
*/
function takeSnapshot() {
const table = document.querySelector('#connections-table table');
const rows = Array.from(table.querySelectorAll('tr'));
const headers = rows.shift().querySelectorAll('th');
const headerNames = Array.from(headers).map(header => header.textContent);
const snapshot = rows.map(row => {
const cells = row.querySelectorAll('td');
const rowData = {};
headerNames.forEach((header, index) => {
rowData[header] = cells[index].textContent;
});
return rowData;
});
const timestamp = new Date().toISOString();
const snapshotData = {
timestamp: timestamp,
data: snapshot
};
const snapshotString = JSON.stringify(snapshotData);
navigator.clipboard.writeText(snapshotString).then(() => {
alert('Snapshot copied to clipboard!');
}, () => {
alert('Failed to copy snapshot to clipboard. Please try again.');
});
}
/**
* Reset the UI to its initial state
*/
function resetUI() {
resultDiv.textContent = '';
progressBar.style.width = '0%';
connectionsTableDiv.innerHTML = '';
failedUsernamesDiv.style.display = 'none';
buttonContainer.style.display = 'none';
}