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