Skip to content

Commit 6168bb4

Browse files
committed
Live stats
1 parent 97e17f1 commit 6168bb4

3 files changed

Lines changed: 59 additions & 0 deletions

File tree

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": "live-stats",
5+
"versionAdded": "v3.3.0"
6+
},
27
{
38
"version": 2,
49
"id": "hide-scratch-news",

features/live-stats/data.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"title": "Live Stats",
3+
"description": "Updates the loves, favorites, remixes, and views on a project live, without having to reload to view the new counts.",
4+
"credits": [
5+
{
6+
"username": "RowanMoonBoy",
7+
"url": "https://scratch.mit.edu/users/RowanMoonBoy/"
8+
},
9+
{ "username": "rgantzos", "url": "https://scratch.mit.edu/users/rgantzos/" }
10+
],
11+
"scripts": [
12+
{
13+
"file": "script.js",
14+
"runOn": "/projects/*"
15+
}
16+
],
17+
"dynamic": true,
18+
"type": ["Website"],
19+
"tags": ["New", "Featured"]
20+
}

features/live-stats/script.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
export default function ({ feature, console }) {
2+
ScratchTools.waitForElements(
3+
"div.flex-row.stats.noselect",
4+
async function (row) {
5+
let love = row.querySelector(".project-loves");
6+
let favorite = row.querySelector(".project-favorites");
7+
let remix = row.querySelector(".project-remixes");
8+
let view = row.querySelector(".project-views");
9+
10+
let interval = setInterval(async function () {
11+
if (!row) clearInterval(interval);
12+
if (!row) return;
13+
if (!feature.self.enabled) return;
14+
15+
let data = await (
16+
await fetch(
17+
`https://api.scratch.mit.edu/projects/${
18+
window.location.pathname.split("/")[2]
19+
}/?nocache=${Date.now().toString()}`
20+
)
21+
).json();
22+
23+
if (data.error) return;
24+
25+
love.textContent = data.stats.loves.toString()
26+
favorite.textContent = data.stats.favorites.toString()
27+
remix.textContent = data.stats.remixes.toString()
28+
view.textContent = data.stats.views.toString()
29+
30+
feature.redux.getState().preview.projectInfo.stats = data.stats
31+
}, 5000);
32+
}
33+
);
34+
}

0 commit comments

Comments
 (0)