-
Notifications
You must be signed in to change notification settings - Fork 64
Revamp Total Stats #1009
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MaterArc
wants to merge
10
commits into
STForScratch:main
Choose a base branch
from
MaterArc:total-stats
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Revamp Total Stats #1009
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a055ee8
Update data.json
MaterArc b69aa3e
Update script.js
MaterArc babe5bc
Delete features/total-stats/style.css
MaterArc ce75d23
Update features.json
MaterArc a58dad8
Add styles
MaterArc 9d82a88
Update data.json
MaterArc 11a2715
Fix styling
MaterArc d651ba1
Update style.css
MaterArc 881f043
Make row horizontal
MaterArc f546ce1
Make row more compact
MaterArc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,25 @@ | ||
{ | ||
"title": "Total User Stats", | ||
"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.", | ||
"credits": [ | ||
{ | ||
"username": "RowanMoonBoy", | ||
"url": "https://scratch.mit.edu/users/RowanMoonBoy/" | ||
}, | ||
{ | ||
"username": "rgantzos", | ||
"url": "https://scratch.mit.edu/users/rgantzos/" | ||
} | ||
], | ||
"dynamic": true, | ||
"styles": [ | ||
{ | ||
"file": "style.css", | ||
"runOn": "/users/*" | ||
} | ||
], | ||
"scripts": [ | ||
{ | ||
"file": "script.js", | ||
"runOn": "/users/*" | ||
} | ||
], | ||
"tags": [ | ||
"New" | ||
], | ||
"type": [ | ||
"Website" | ||
] | ||
} | ||
"title": "Total Project Stats", | ||
"description": "Displays a box on user profiles showing the total loves, favorites, views, and remixes across all their shared projects.", | ||
"credits": [ | ||
{ | ||
"username": "MaterArc", | ||
"url": "https://scratch.mit.edu/users/MaterArc/" | ||
} | ||
], | ||
"type": ["Website"], | ||
"tags": ["New", "Featured"], | ||
"dynamic": true, | ||
"styles": [ | ||
{ | ||
"file": "style.css", | ||
"runOn": "/users/*" | ||
} | ||
], | ||
"scripts": [ | ||
{ | ||
"file": "script.js", | ||
"runOn": "/users/*" | ||
} | ||
] | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,76 @@ | ||
export default async function ({ feature, console }) { | ||
let div = await ScratchTools.waitForElement("div.box.slider-carousel-container") | ||
if (!document.querySelector("#profile-data")) return; | ||
|
||
let stats = await getStats(Scratch.INIT_DATA.PROFILE.model.username) | ||
|
||
let span = document.createElement("span") | ||
span.textContent = stats.loves.toLocaleString() + " loves • " + stats.favorites.toLocaleString() + " favorites • " + stats.views.toLocaleString() + " views" | ||
span.className = "ste-total-stats" | ||
feature.self.hideOnDisable(span) | ||
div.querySelector(".box-head").insertBefore(span, div.querySelector("a")) | ||
|
||
async function getProjects(user) { | ||
let projects = [] | ||
let offset = 0 | ||
let keepGoing = true | ||
|
||
while (keepGoing) { | ||
let data = await (await fetch(`https://api.scratch.mit.edu/users/${user}/projects?limit=40&offset=${offset.toString()}`)).json() | ||
|
||
projects.push(...data) | ||
|
||
if (data.length === 40) { | ||
offset += 40 | ||
} else { | ||
keepGoing = false | ||
} | ||
} | ||
|
||
return projects | ||
} | ||
|
||
async function getStats(user) { | ||
let projects = await getProjects(user) | ||
let stats = { | ||
loves: 0, | ||
favorites: 0, | ||
remixes: 0, | ||
views: 0, | ||
} | ||
|
||
for (var i in projects) { | ||
stats.loves += projects[i].stats.loves | ||
stats.favorites += projects[i].stats.favorites | ||
stats.remixes += projects[i].stats.remixes | ||
stats.views += projects[i].stats.views | ||
} | ||
|
||
return stats | ||
} | ||
} | ||
export default async function ({ feature }) { | ||
await ScratchTools.waitForElement(".box-head"); | ||
|
||
const username = window.location.pathname.split("/")[2]; | ||
const res = await fetch(`https://scratchdata.vercel.app/api/user-stats/${username}`); | ||
if (!res.ok) return; | ||
|
||
const data = await res.json(); | ||
const format = (n) => n.toLocaleString(); | ||
|
||
const stats = [ | ||
{ | ||
icon: "https://scratch.mit.edu/svgs/project/love-red.svg", | ||
alt: "Loves", | ||
value: data.totalLoves, | ||
}, | ||
{ | ||
icon: "https://scratch.mit.edu/svgs/project/fav-yellow.svg", | ||
alt: "Favorites", | ||
value: data.totalFavorites, | ||
}, | ||
{ | ||
icon: "https://scratch.mit.edu/svgs/project/views-gray.svg", | ||
alt: "Views", | ||
value: data.totalViews, | ||
}, | ||
{ | ||
icon: "https://raw.githubusercontent.com/scratchfoundation/scratch-www/a9cf52cd7fbec834897587dd9e17d648ba0a38b2/static/svgs/messages/remix.svg", | ||
alt: "Remixes", | ||
value: data.totalRemixes, | ||
}, | ||
]; | ||
|
||
const box = document.createElement("div"); | ||
box.className = "box slider-carousel-container prevent-select ste-tps-box"; | ||
box.dataset.stFeature = "total-project-stats"; | ||
|
||
const head = document.createElement("div"); | ||
head.className = "box-head"; | ||
|
||
const title = document.createElement("h4"); | ||
title.textContent = "Total Project Stats"; | ||
head.appendChild(title); | ||
box.appendChild(head); | ||
|
||
const content = document.createElement("div"); | ||
content.className = "box-content slider-carousel horizontal ste-tps-content"; | ||
|
||
for (const stat of stats) { | ||
const statDiv = document.createElement("div"); | ||
statDiv.className = "ste-tps-stat"; | ||
|
||
const icon = document.createElement("img"); | ||
icon.src = stat.icon; | ||
icon.width = 32; | ||
icon.height = 32; | ||
icon.alt = stat.alt; | ||
icon.style.display = "block"; | ||
|
||
const value = document.createElement("div"); | ||
value.className = "ste-tps-value"; | ||
value.textContent = format(stat.value); | ||
|
||
statDiv.appendChild(icon); | ||
statDiv.appendChild(value); | ||
content.appendChild(statDiv); | ||
} | ||
|
||
box.appendChild(content); | ||
|
||
const boxHeads = document.querySelectorAll(".box-head"); | ||
if (boxHeads.length >= 5) { | ||
const targetBox = boxHeads[4].parentNode; | ||
targetBox.parentNode.insertBefore(box, targetBox.nextSibling); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,22 @@ | ||
.ste-total-stats { | ||
font-weight: 500; | ||
opacity: .6; | ||
margin-left: 1rem; | ||
font-style: italic; | ||
font-size: .9rem; | ||
} | ||
.ste-tps-content { | ||
display: flex; | ||
justify-content: space-evenly; | ||
align-items: center; | ||
height: 99.5px; | ||
} | ||
|
||
.ste-tps-stat { | ||
text-align: center; | ||
display: flex; | ||
} | ||
|
||
.ste-tps-stat img { | ||
display: block; | ||
} | ||
|
||
.ste-tps-value { | ||
font-size: 21px; | ||
font-weight: 600; | ||
margin-top: 5px; | ||
margin-left: 10px; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.