-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcredit.js
More file actions
47 lines (39 loc) · 1.85 KB
/
credit.js
File metadata and controls
47 lines (39 loc) · 1.85 KB
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
document.addEventListener('DOMContentLoaded', () => {
const grids = document.querySelectorAll('.grid');
const colorThief = new ColorThief();
const fetchAvatars = async () => {
const avatarPromises = Array.from(grids).map(grid => {
const userId = grid.getAttribute('data-user-id');
const avatarUrl = `https://avatar-cyan.vercel.app/api/${userId}`;
return fetch(avatarUrl)
.then(response => response.json())
.then(data => {
const imgElement = grid.querySelector('.grid-img');
const displayName = data.display_name;
const avatarImage = data.avatarUrl;
const h2Element = grid.querySelector('h2');
if (h2Element) {
h2Element.textContent = displayName;
}
imgElement.src = avatarImage;
return new Promise((resolve, reject) => {
const img = new Image();
img.crossOrigin = 'Anonymous';
img.src = avatarImage;
img.onload = () => {
const dominantColor = colorThief.getColor(img);
const hexColor = `rgb(${dominantColor[0]}, ${dominantColor[1]}, ${dominantColor[2]})`;
imgElement.style.border = `2px solid ${hexColor}`;
resolve();
};
img.onerror = () => reject(`Failed to load image: ${avatarImage}`);
});
})
.catch(error => {
console.error(`Error fetching avatar for user ${userId}:`, error);
});
});
await Promise.all(avatarPromises);
};
fetchAvatars();
});