|
| 1 | +export default async function ({ feature }) { |
| 2 | + const parts = window.location.pathname.split("/"); |
| 3 | + const studioId = parts[parts.length - 2]; |
| 4 | + if (!studioId) return; |
| 5 | + |
| 6 | + const session = await ScratchTools.Session(); |
| 7 | + if (!session?.user?.username) return; |
| 8 | + |
| 9 | + const csrfToken = document.cookie.match(/scratchcsrftoken=([^;]+)/)?.[1]; |
| 10 | + if (!csrfToken) return; |
| 11 | + |
| 12 | + ScratchTools.waitForElements( |
| 13 | + ".comment-body", |
| 14 | + (comment) => { |
| 15 | + const topRow = comment.querySelector(".comment-top-row"); |
| 16 | + const actionList = topRow?.querySelector(".action-list"); |
| 17 | + if (!topRow || !actionList) return; |
| 18 | + if (actionList.querySelector(".ste-comment-invite")) return; |
| 19 | + |
| 20 | + const inviteBtn = document.createElement("span"); |
| 21 | + inviteBtn.classList.add("ste-comment-invite"); |
| 22 | + |
| 23 | + const innerSpan = document.createElement("span"); |
| 24 | + innerSpan.textContent = "Invite"; |
| 25 | + inviteBtn.appendChild(innerSpan); |
| 26 | + |
| 27 | + inviteBtn.onclick = async () => { |
| 28 | + const username = topRow.querySelector(".username")?.textContent.trim(); |
| 29 | + if (!username) return; |
| 30 | + const url = `https://scratch.mit.edu/site-api/users/curators-in/${studioId}/invite_curator/?usernames=${encodeURIComponent( |
| 31 | + username |
| 32 | + )}`; |
| 33 | + await fetch(url, { |
| 34 | + method: "PUT", |
| 35 | + headers: { |
| 36 | + "x-csrftoken": csrfToken, |
| 37 | + "Content-Type": "application/json", |
| 38 | + }, |
| 39 | + credentials: "include", |
| 40 | + }); |
| 41 | + }; |
| 42 | + |
| 43 | + actionList.appendChild(inviteBtn); |
| 44 | + }, |
| 45 | + "ste-invite-buttons", |
| 46 | + false |
| 47 | + ); |
| 48 | +} |
0 commit comments