Skip to content

Commit 76c6838

Browse files
committed
Follow on projects
1 parent 3e1b711 commit 76c6838

File tree

5 files changed

+123
-0
lines changed

5 files changed

+123
-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": "follow-on-projects",
5+
"versionAdded": "v3.5.0"
6+
},
27
{
38
"version": 2,
49
"id": "dark-paint-editor",
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"title": "Follow on Project Page",
3+
"description": "Follow users directly from the project page instead of going to their profile.",
4+
"credits": [
5+
{ "username": "rgantzos", "url": "https://scratch.mit.edu/users/rgantzos/" }
6+
],
7+
"type": ["Website"],
8+
"tags": [],
9+
"scripts": [{ "file": "script.js", "runOn": "/projects/*" }],
10+
"styles": [{ "file": "style.css", "runOn": "/projects/*" }],
11+
"resources": [{ "name": "project-follow-icon", "path": "/follow.svg" }]
12+
}
Lines changed: 1 addition & 0 deletions
Loading
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
export default async function ({ feature, console }) {
2+
await ScratchTools.waitForElement(".project-buttons");
3+
let auth = await feature.auth.fetch();
4+
let username = feature.redux.getState().preview.projectInfo.author.username;
5+
let data = await (
6+
await fetch(`https://scratch.mit.edu/users/${username}/`)
7+
).text();
8+
9+
let html = document.createElement("html");
10+
html.innerHTML = data.split('<div id="follow-button" class="buttons">')[1]
11+
.split(`</div>
12+
13+
</div>
14+
<div class="box-content" id="profile-box">`)[0];
15+
let following = !!html.querySelector("div[data-control=unfollow");
16+
17+
if (!document.querySelector(".ste-follow-btn")) {
18+
let button = document.createElement("button");
19+
button.className =
20+
"ste-follow-btn button " + (following ? "following" : "notfollowing");
21+
button.textContent = (following ? "Unfollow" : "Follow") + " " + username;
22+
button.addEventListener("click", async function () {
23+
if (following) {
24+
following = false;
25+
button.className =
26+
"ste-follow-btn button " + (following ? "following" : "notfollowing");
27+
button.textContent =
28+
(following ? "Unfollow" : "Follow") + " " + username;
29+
let data = await (
30+
await fetch(
31+
"https://scratch.mit.edu/site-api/users/followers/rgantzosTEST/remove/?usernames=" +
32+
auth.user.username,
33+
{
34+
headers: {
35+
"x-csrftoken": "VZ0lgYGuLZzG5nD4nNirmbbze7CulCmP",
36+
"x-requested-with": "XMLHttpRequest",
37+
},
38+
referrer: "https://scratch.mit.edu/users/" + username + "/",
39+
body: '{"id":"' + username + '"}',
40+
method: "PUT",
41+
mode: "cors",
42+
credentials: "include",
43+
}
44+
)
45+
).json();
46+
} else {
47+
following = true;
48+
button.className =
49+
"ste-follow-btn button " + (following ? "following" : "notfollowing");
50+
button.textContent =
51+
(following ? "Unfollow" : "Follow") + " " + username;
52+
let data = await (
53+
await fetch(
54+
"https://scratch.mit.edu/site-api/users/followers/rgantzosTEST/add/?usernames=" +
55+
auth.user.username,
56+
{
57+
headers: {
58+
"x-csrftoken": "VZ0lgYGuLZzG5nD4nNirmbbze7CulCmP",
59+
"x-requested-with": "XMLHttpRequest",
60+
},
61+
referrer: "https://scratch.mit.edu/users/" + username + "/",
62+
body: '{"id":"' + username + '"}',
63+
method: "PUT",
64+
mode: "cors",
65+
credentials: "include",
66+
}
67+
)
68+
).json();
69+
}
70+
});
71+
ScratchTools.appendToSharedSpace({
72+
space: "beforeRemixButton",
73+
order: 0,
74+
element: button,
75+
});
76+
}
77+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
.ste-follow-btn {
2+
margin-top: 0;
3+
border-radius: 0.25rem;
4+
padding-top: 0.6875rem;
5+
padding-right: 0.9375rem;
6+
padding-bottom: 0.6875rem;
7+
padding-left: 0.875rem;
8+
height: 2.5rem;
9+
font-size: .875rem;
10+
}
11+
12+
.ste-follow-btn::before {
13+
background-image: var(--scratchtoolsresource-project-follow-icon);
14+
display: inline-block;
15+
margin-top: -2px;
16+
margin-right: 0.4375rem;
17+
background-repeat: no-repeat;
18+
background-position: center center;
19+
background-size: contain;
20+
width: 1.125rem;
21+
height: 1.125rem;
22+
vertical-align: middle;
23+
content: "";
24+
}
25+
26+
.ste-follow-btn.following {
27+
background-color: rgb(171, 171, 171);
28+
}

0 commit comments

Comments
 (0)