1+ export default async function ( { feature, console, className } ) {
2+ window . feature = feature
3+
4+ let auth = await feature . auth . fetch ( )
5+ if ( ! auth ?. user ?. username ) return console . log ( "User not logged in." ) ;
6+
7+ let user = auth . user . username
8+ let profile = Scratch . INIT_DATA . PROFILE . model . username
9+
10+ if ( user === profile ) return ;
11+
12+ async function getFollow ( username , type , maxRequests ) {
13+ const LIMIT = 40
14+
15+ let url = `https://api.scratch.mit.edu/users/${ username } /${ type } `
16+ let follows = [ ]
17+
18+ let keepGoing = true
19+ let offset = 0
20+ let requests = 0
21+ while ( keepGoing ) {
22+ let data = await ( await fetch ( url + `?offset=${ offset } &limit=${ LIMIT } ` ) ) . json ( )
23+ follows . push ( ...data )
24+
25+ requests += 1
26+
27+ if ( data . length < 20 ) {
28+ keepGoing = false
29+ }
30+
31+ if ( requests === maxRequests ) {
32+ keepGoing = false
33+ }
34+
35+ offset += LIMIT
36+ }
37+
38+ return follows
39+ }
40+
41+ const profileFollowing = await getFollow ( profile , "following" , 10 )
42+ const profileFollowers = await getFollow ( profile , "followers" , 10 )
43+ const userFollowing = await getFollow ( user , "following" , 5 )
44+
45+ const mutualFollowing = profileFollowing . filter ( ( pF ) => userFollowing . find ( ( uF ) => uF . username === pF . username ) )
46+ const mutualFollowers = profileFollowers . filter ( ( pF ) => userFollowing . find ( ( uF ) => uF . username === pF . username ) )
47+
48+ let followingUsernames = [ ]
49+ let followersUsernames = [ ]
50+
51+ for ( var i in mutualFollowing ) {
52+ followingUsernames . push ( mutualFollowing [ i ] . username )
53+ }
54+
55+ for ( var i in mutualFollowers ) {
56+ followersUsernames . push ( mutualFollowers [ i ] . username )
57+ }
58+
59+ const followingBox = document . querySelector ( `div.box.slider-carousel-container a[href='/users/${ profile } /following/']` ) . closest ( ".box" )
60+ const followersBox = document . querySelector ( `div.box.slider-carousel-container a[href='/users/${ profile } /followers/']` ) . closest ( ".box" )
61+
62+ let followingContainer = Object . assign ( document . createElement ( "div" ) , {
63+ className : className ( "mutual following container" )
64+ } )
65+ followingContainer . title = followingUsernames . join ( ", " )
66+ let followersContainer = Object . assign ( document . createElement ( "div" ) , {
67+ className : className ( "mutual followers container" )
68+ } )
69+ followersContainer . title = followersUsernames . join ( ", " )
70+ feature . self . hideOnDisable ( followingContainer )
71+ feature . self . hideOnDisable ( followersContainer )
72+
73+ followingBox . querySelector ( ".box-head" ) . insertBefore ( followingContainer , followingBox . querySelector ( ".box-head a" ) )
74+ followersBox . querySelector ( ".box-head" ) . insertBefore ( followersContainer , followersBox . querySelector ( ".box-head a" ) )
75+
76+ for ( var i in mutualFollowing ) {
77+ if ( Number ( i ) < 5 ) {
78+ let mF = mutualFollowing [ i ]
79+ let image = Object . assign ( document . createElement ( "img" ) , {
80+ src : mF . profile . images [ "90x90" ]
81+ } )
82+ image . setAttribute ( "style" , "--i:" + i )
83+ followingContainer . appendChild ( image )
84+ }
85+ }
86+
87+ if ( mutualFollowing . length > 0 ) {
88+ let span = Object . assign ( document . createElement ( "span" ) , {
89+ textContent : `Following ${ mutualFollowing [ 0 ] . username } ${ mutualFollowing . length > 1 ? ` and ${ mutualFollowing . length - 1 } ${ mutualFollowing . length > 2 ? "others" : "other" } ` : "" } `
90+ } )
91+ followingContainer . appendChild ( span )
92+ }
93+
94+ for ( var i in mutualFollowers ) {
95+ let mF = mutualFollowers [ i ]
96+ let image = Object . assign ( document . createElement ( "img" ) , {
97+ src : mF . profile . images [ "90x90" ]
98+ } )
99+ image . setAttribute ( "style" , "--i:" + i )
100+ followersContainer . appendChild ( image )
101+ }
102+
103+ if ( mutualFollowers . length > 0 ) {
104+ let span = Object . assign ( document . createElement ( "span" ) , {
105+ textContent : `Followed by ${ mutualFollowers [ 0 ] . username } ${ mutualFollowers . length > 1 ? ` and ${ mutualFollowers . length - 1 } ${ mutualFollowers . length > 2 ? "others" : "other" } ` : "" } `
106+ } )
107+ followersContainer . appendChild ( span )
108+ }
109+ }
0 commit comments