Skip to content

Commit 0bc7de1

Browse files
committed
Better featured projects
1 parent a1184c6 commit 0bc7de1

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"title": "Better Featured Projects",
3+
"description": "Replaces the Featured Projects section with featured projects curated by ScratchTools users. You can submit your own projects to be featured as well.",
4+
"credits": [
5+
{
6+
"username": "rgantzos",
7+
"url": "https://scratch.mit.edu/users/rgantzos/"
8+
}
9+
],
10+
"type": ["Website"],
11+
"tags": ["New", "Recommended"],
12+
"scripts": [{ "file": "script.js", "runOn": "/*" }]
13+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
export default async function ({ feature, console }) {
2+
let data = await getFeatured();
3+
4+
// Wait for boxes to load
5+
await ScratchTools.waitForElement(".box");
6+
7+
// Select featured section using internal key
8+
let box = [...document.querySelectorAll("div.box")].find(
9+
(box) =>
10+
box[
11+
Object.keys(box).find((key) => key.startsWith("__reactInternalInstance"))
12+
]?.return.key === "community_featured_projects"
13+
);
14+
15+
if (box) {
16+
// Add class to select using waitForElements API
17+
box.classList.add("featured-projects")
18+
19+
// Change box title and link to studio
20+
box.querySelector("h4").textContent = "ScratchTools Featured Projects"
21+
box.querySelector("a").href = "/studios/32047713/comments"
22+
box.querySelector("a").textContent = "Submit a project"
23+
24+
ScratchTools.waitForElements(".featured-projects .thumbnail-info", function(info) {
25+
let project = info.closest(".project")
26+
let indexOfProject = project[Object.keys(project).find((key) => key.startsWith("__reactInternalInstance"))].return.index
27+
28+
if (data[indexOfProject]) {
29+
let thumbnail = project.querySelector("a.thumbnail-image")
30+
thumbnail.href = `/projects/${data[indexOfProject].id}/`
31+
thumbnail.firstChild.src = data[indexOfProject].thumbnail
32+
33+
info.querySelector(".thumbnail-creator a").href = `/users/${data[indexOfProject].author}/`
34+
info.querySelector(".thumbnail-creator a").textContent = data[indexOfProject].author
35+
36+
info.querySelector("a").href = `/projects/${data[indexOfProject].id}/`
37+
info.querySelector("a").title = data[indexOfProject].title
38+
info.querySelector("a").textContent = data[indexOfProject].title
39+
}
40+
})
41+
}
42+
43+
async function getFeatured() {
44+
let FEATURED = [];
45+
let data = await (
46+
await fetch(feature.server.endpoint("/featured/"))
47+
).json();
48+
49+
for (var i in data) {
50+
try {
51+
let project = await (
52+
await fetch(`https://api.scratch.mit.edu/projects/${data[i]}/`)
53+
).json();
54+
FEATURED.push({
55+
id: data[i],
56+
thumbnail: `https://cdn2.scratch.mit.edu/get_image/project/${data[i]}_480x360.png`,
57+
title: project.title,
58+
author: project.author.username,
59+
});
60+
} catch (err) {}
61+
}
62+
63+
return FEATURED
64+
}
65+
}

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": "better-featured-projects",
5+
"versionAdded": "v4.0.0"
6+
},
27
{
38
"version": 2,
49
"id": "align-to-center",

0 commit comments

Comments
 (0)