Skip to content

Commit 2f0140f

Browse files
authored
Merge pull request #828 from rgantzos/main
Total stats
2 parents ef36402 + 210cc7d commit 2f0140f

File tree

4 files changed

+96
-0
lines changed

4 files changed

+96
-0
lines changed

features/features.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
[
2+
{
3+
"version": 2,
4+
"id": "total-stats",
5+
"versionAdded": "v3.8.0"
6+
},
27
{
38
"version": 2,
49
"id": "project-miniplayer",

features/total-stats/data.json

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"title": "Total User Stats",
3+
"description": "Next to shared projects on a user's profile, displays the total loves, favorites and views that the user has received across all of their shared projects.",
4+
"credits": [
5+
{
6+
"username": "RowanMoonBoy",
7+
"url": "https://scratch.mit.edu/users/RowanMoonBoy/"
8+
},
9+
{
10+
"username": "rgantzos",
11+
"url": "https://scratch.mit.edu/users/rgantzos/"
12+
}
13+
],
14+
"dynamic": true,
15+
"styles": [
16+
{
17+
"file": "style.css",
18+
"runOn": "/users/*"
19+
}
20+
],
21+
"scripts": [
22+
{
23+
"file": "script.js",
24+
"runOn": "/users/*"
25+
}
26+
],
27+
"tags": [
28+
"New"
29+
],
30+
"type": [
31+
"Website"
32+
]
33+
}

features/total-stats/script.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
export default async function ({ feature, console }) {
2+
let div = await ScratchTools.waitForElement("div.box.slider-carousel-container")
3+
if (!document.querySelector("#profile-data")) return;
4+
5+
let stats = await getStats(Scratch.INIT_DATA.PROFILE.model.username)
6+
7+
let span = document.createElement("span")
8+
span.textContent = stats.loves.toLocaleString() + " loves • " + stats.favorites.toLocaleString() + " favorites • " + stats.views.toLocaleString() + " views"
9+
span.className = "ste-total-stats"
10+
feature.self.hideOnDisable(span)
11+
div.querySelector(".box-head").insertBefore(span, div.querySelector("a"))
12+
13+
async function getProjects(user) {
14+
let projects = []
15+
let offset = 0
16+
let keepGoing = true
17+
18+
while (keepGoing) {
19+
let data = await (await fetch(`https://api.scratch.mit.edu/users/${user}/projects?limit=40&offset=${offset.toString()}`)).json()
20+
21+
projects.push(...data)
22+
23+
if (data.length === 40) {
24+
offset += 40
25+
} else {
26+
keepGoing = false
27+
}
28+
}
29+
30+
return projects
31+
}
32+
33+
async function getStats(user) {
34+
let projects = await getProjects(user)
35+
let stats = {
36+
loves: 0,
37+
favorites: 0,
38+
remixes: 0,
39+
views: 0,
40+
}
41+
42+
for (var i in projects) {
43+
stats.loves += projects[i].stats.loves
44+
stats.favorites += projects[i].stats.favorites
45+
stats.remixes += projects[i].stats.remixes
46+
stats.views += projects[i].stats.views
47+
}
48+
49+
return stats
50+
}
51+
}

features/total-stats/style.css

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.ste-total-stats {
2+
font-weight: 500;
3+
opacity: .6;
4+
margin-left: 1rem;
5+
font-style: italic;
6+
font-size: .9rem;
7+
}

0 commit comments

Comments
 (0)