|
| 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 | +} |
0 commit comments