|
1 | 1 | export default async function ({ feature, console }) { |
2 | | - const username = window.location.pathname.split('/')[2]; |
3 | | - if (!username) return; |
4 | | - |
5 | | - const followersEndpoint = `https://api.scratch.mit.edu/users/${username}/followers/`; |
6 | | - const followingEndpoint = `https://api.scratch.mit.edu/users/${username}/following/`; |
7 | | - |
8 | | - try { |
9 | | - const followersResponse = await fetch(followersEndpoint); |
10 | | - if (!followersResponse.ok) return; |
11 | | - |
12 | | - const followersData = await followersResponse.json(); |
13 | | - if (!Array.isArray(followersData)) return; |
14 | | - |
15 | | - const mostRecentFollowers = followersData |
16 | | - .slice(0, 9) |
17 | | - .filter(follower => follower.username && follower.profile && follower.profile.images) |
18 | | - .map(follower => ({ |
19 | | - username: follower.username, |
20 | | - profileImage: follower.profile.images['90x90'] || follower.profile.images['50x50'] || '', |
21 | | - })); |
22 | | - |
23 | | - const followingResponse = await fetch(followingEndpoint); |
24 | | - if (!followingResponse.ok) return; |
25 | | - |
26 | | - const followingData = await followingResponse.json(); |
27 | | - if (!Array.isArray(followingData)) return; |
28 | | - |
29 | | - const mostRecentFollowing = followingData |
30 | | - .slice(0, 9) |
31 | | - .filter(follow => follow.username && follow.profile && follow.profile.images) |
32 | | - .map(follow => ({ |
33 | | - username: follow.username, |
34 | | - profileImage: follow.profile.images['90x90'] || follow.profile.images['50x50'] || '', |
35 | | - })); |
36 | | - |
37 | | - const allFeaturedElements = document.querySelectorAll("#featured"); |
38 | | - if (allFeaturedElements.length === 0) return; |
39 | | - |
40 | | - const lastFeaturedElement = allFeaturedElements[allFeaturedElements.length - 1]; |
41 | | - lastFeaturedElement.innerHTML = ""; |
42 | | - |
43 | | - mostRecentFollowers.forEach(follower => { |
44 | | - const li = document.createElement("li"); |
45 | | - li.className = "user thumb item"; |
46 | | - |
47 | | - const link = document.createElement("a"); |
48 | | - link.href = `/users/${follower.username}/`; |
49 | | - link.title = follower.username; |
50 | | - |
51 | | - const img = document.createElement("img"); |
52 | | - img.className = "lazy"; |
53 | | - img.src = follower.profileImage; |
54 | | - img.alt = follower.username; |
55 | | - img.width = 60; |
56 | | - img.height = 60; |
57 | | - |
58 | | - const span = document.createElement("span"); |
59 | | - span.className = "title"; |
60 | | - |
61 | | - const spanLink = document.createElement("a"); |
62 | | - spanLink.href = `/users/${follower.username}/`; |
63 | | - spanLink.textContent = follower.username; |
64 | | - |
65 | | - link.appendChild(img); |
66 | | - span.appendChild(spanLink); |
67 | | - li.appendChild(link); |
68 | | - li.appendChild(span); |
69 | | - |
70 | | - lastFeaturedElement.appendChild(li); |
71 | | - }); |
72 | | - |
73 | | - if (allFeaturedElements.length > 1) { |
74 | | - const secondLastFeaturedElement = allFeaturedElements[allFeaturedElements.length - 2]; |
75 | | - secondLastFeaturedElement.innerHTML = ""; |
76 | | - |
77 | | - mostRecentFollowing.forEach(follow => { |
78 | | - const li = document.createElement("li"); |
79 | | - li.className = "user thumb item"; |
80 | | - |
81 | | - const link = document.createElement("a"); |
82 | | - link.href = `/users/${follow.username}/`; |
83 | | - link.title = follow.username; |
84 | | - |
85 | | - const img = document.createElement("img"); |
86 | | - img.className = "lazy"; |
87 | | - img.src = follow.profileImage; |
88 | | - img.alt = follow.username; |
89 | | - img.width = 60; |
90 | | - img.height = 60; |
91 | | - |
92 | | - const span = document.createElement("span"); |
93 | | - span.className = "title"; |
94 | | - |
95 | | - const spanLink = document.createElement("a"); |
96 | | - spanLink.href = `/users/${follow.username}/`; |
97 | | - spanLink.textContent = follow.username; |
98 | | - |
99 | | - link.appendChild(img); |
100 | | - span.appendChild(spanLink); |
101 | | - li.appendChild(link); |
102 | | - li.appendChild(span); |
103 | | - |
104 | | - secondLastFeaturedElement.appendChild(li); |
105 | | - }); |
106 | | - } |
| 2 | + const username = window.location.pathname.split('/')[2]; |
| 3 | + if (!username) return; |
| 4 | + |
| 5 | + const followersEndpoint = `https://api.scratch.mit.edu/users/${username}/followers/`; |
| 6 | + const followingEndpoint = `https://api.scratch.mit.edu/users/${username}/following/`; |
| 7 | + |
| 8 | + try { |
| 9 | + const followersResponse = await fetch(followersEndpoint); |
| 10 | + if (!followersResponse.ok) return; |
| 11 | + |
| 12 | + const followersData = await followersResponse.json(); |
| 13 | + if (!Array.isArray(followersData)) return; |
| 14 | + |
| 15 | + const mostRecentFollowers = followersData |
| 16 | + .slice(0, 9) |
| 17 | + .filter(follower => follower.username && follower.profile && follower.profile.images) |
| 18 | + .map(follower => ({ |
| 19 | + username: follower.username, |
| 20 | + profileImage: follower.profile.images['90x90'] || follower.profile.images['50x50'] || '', |
| 21 | + })); |
| 22 | + |
| 23 | + const followingResponse = await fetch(followingEndpoint); |
| 24 | + if (!followingResponse.ok) return; |
| 25 | + |
| 26 | + const followingData = await followingResponse.json(); |
| 27 | + if (!Array.isArray(followingData)) return; |
| 28 | + |
| 29 | + const mostRecentFollowing = followingData |
| 30 | + .slice(0, 9) |
| 31 | + .filter(follow => follow.username && follow.profile && follow.profile.images) |
| 32 | + .map(follow => ({ |
| 33 | + username: follow.username, |
| 34 | + profileImage: follow.profile.images['90x90'] || follow.profile.images['50x50'] || '', |
| 35 | + })); |
| 36 | + |
| 37 | + const allFeaturedElements = document.querySelectorAll("#featured"); |
| 38 | + if (allFeaturedElements.length === 0) return; |
| 39 | + |
| 40 | + const lastFeaturedElement = allFeaturedElements[allFeaturedElements.length - 1]; |
| 41 | + lastFeaturedElement.innerHTML = ""; |
| 42 | + |
| 43 | + mostRecentFollowers.forEach(follower => { |
| 44 | + const li = document.createElement("li"); |
| 45 | + li.className = "user thumb item"; |
| 46 | + |
| 47 | + const link = document.createElement("a"); |
| 48 | + link.href = `/users/${follower.username}/`; |
| 49 | + link.title = follower.username; |
| 50 | + |
| 51 | + const img = document.createElement("img"); |
| 52 | + img.className = "lazy"; |
| 53 | + img.src = follower.profileImage; |
| 54 | + img.alt = follower.username; |
| 55 | + img.width = 60; |
| 56 | + img.height = 60; |
| 57 | + |
| 58 | + const span = document.createElement("span"); |
| 59 | + span.className = "title"; |
| 60 | + |
| 61 | + const spanLink = document.createElement("a"); |
| 62 | + spanLink.href = `/users/${follower.username}/`; |
| 63 | + spanLink.textContent = follower.username; |
| 64 | + |
| 65 | + link.appendChild(img); |
| 66 | + span.appendChild(spanLink); |
| 67 | + li.appendChild(link); |
| 68 | + li.appendChild(span); |
| 69 | + |
| 70 | + lastFeaturedElement.appendChild(li); |
| 71 | + }); |
| 72 | + |
| 73 | + if (allFeaturedElements.length > 1) { |
| 74 | + const secondLastFeaturedElement = allFeaturedElements[allFeaturedElements.length - 2]; |
| 75 | + secondLastFeaturedElement.innerHTML = ""; |
| 76 | + |
| 77 | + mostRecentFollowing.forEach(follow => { |
| 78 | + const li = document.createElement("li"); |
| 79 | + li.className = "user thumb item"; |
| 80 | + |
| 81 | + const link = document.createElement("a"); |
| 82 | + link.href = `/users/${follow.username}/`; |
| 83 | + link.title = follow.username; |
| 84 | + |
| 85 | + const img = document.createElement("img"); |
| 86 | + img.className = "lazy"; |
| 87 | + img.src = follow.profileImage; |
| 88 | + img.alt = follow.username; |
| 89 | + img.width = 60; |
| 90 | + img.height = 60; |
| 91 | + |
| 92 | + const span = document.createElement("span"); |
| 93 | + span.className = "title"; |
| 94 | + |
| 95 | + const spanLink = document.createElement("a"); |
| 96 | + spanLink.href = `/users/${follow.username}/`; |
| 97 | + spanLink.textContent = follow.username; |
| 98 | + |
| 99 | + link.appendChild(img); |
| 100 | + span.appendChild(spanLink); |
| 101 | + li.appendChild(link); |
| 102 | + li.appendChild(span); |
| 103 | + |
| 104 | + secondLastFeaturedElement.appendChild(li); |
107 | 105 | }); |
108 | | - } catch (error) { |
109 | | - return; |
110 | 106 | } |
| 107 | + } catch (error) { |
| 108 | + return; |
111 | 109 | } |
112 | | - |
| 110 | +} |
0 commit comments