|
1 | 1 | function addNoCommentBox() {
|
2 |
| - if ( |
3 |
| - Scratch?.INIT_DATA?.PROFILE?.model?.username !== |
4 |
| - Scratch?.INIT_DATA?.LOGGED_IN_USER?.model?.username |
5 |
| - ) { |
| 2 | + const loggedInUser = Scratch?.INIT_DATA?.LOGGED_IN_USER?.model?.username; |
| 3 | + const profileUser = Scratch?.INIT_DATA?.PROFILE?.model?.username; |
| 4 | + if (profileUser !== loggedInUser || /^gdpr\d+$/.test(profileUser)) { |
6 | 5 | waitForNoCommentsObserver.disconnect();
|
7 |
| - } else { |
8 |
| - if (document.querySelector("#comments-enabled") !== null) { |
9 |
| - if (ScratchTools.Storage.commentInputFound === undefined) { |
10 |
| - ScratchTools.Storage.commentInputFound = true; |
11 |
| - document |
12 |
| - .querySelector("#comments-enabled") |
13 |
| - .addEventListener("input", function () { |
14 |
| - if (document.querySelector("#comments-enabled").checked) { |
15 |
| - addBox(); |
16 |
| - } else { |
17 |
| - if ( |
18 |
| - document.querySelector("form.scratchtools.comments-on") !== null |
19 |
| - ) { |
20 |
| - document |
21 |
| - .querySelector("form.scratchtools.comments-on") |
22 |
| - .remove(); |
23 |
| - } |
24 |
| - } |
25 |
| - }); |
26 |
| - } |
27 |
| - } |
28 |
| - addBox(); |
29 |
| - function addBox() { |
30 |
| - var div = document.querySelector(".comments-off"); |
31 |
| - if (div !== null) { |
32 |
| - div.className = ""; |
| 6 | + return; |
| 7 | + } |
33 | 8 |
|
34 |
| - div.innerHTML = `<form id="main-post-form" class="comments-on scratchtools" style=" |
35 |
| - float: left; |
36 |
| - margin: 0; |
37 |
| - margin-left: 10px; |
38 |
| - margin-bottom: 0; |
39 |
| - display: block; |
40 |
| -"> |
41 |
| - <div class="control-group tooltip right"> |
42 |
| - |
43 |
| - <textarea name="content" placeholder="Commenting is closed, but you can comment anyways."></textarea> |
44 |
| - |
45 |
| - |
46 |
| - <span class="small-text" style="display/: |
47 |
| - block; |
48 |
| - block; |
49 |
| - bloc; |
50 |
| - blo; |
51 |
| - bl; |
52 |
| - b; |
53 |
| -">You have <span id="chars-left">500</span> characters left.</span> |
54 |
| - </div> |
55 |
| - <div class="control-group error"> |
56 |
| - <div class="button small postComment" data-parent-thread="" data-commentee-id=""><a>Post</a></div> |
57 |
| - <div class="button small grey cancelComment" data-control="cancel"><a>Cancel</a></div> |
58 |
| - <span class="notification"></span> |
59 |
| - </div> |
60 |
| - </form>`; |
61 |
| - div.querySelector("textarea").addEventListener("input", function () { |
62 |
| - div.querySelector("#chars-left").textContent = ( |
63 |
| - 500 - div.querySelector("textarea").value.length |
64 |
| - ).toString(); |
| 9 | + if (document.querySelector("#comments-enabled") !== null) { |
| 10 | + if (ScratchTools.Storage.commentInputFound === undefined) { |
| 11 | + ScratchTools.Storage.commentInputFound = true; |
| 12 | + document |
| 13 | + .querySelector("#comments-enabled") |
| 14 | + .addEventListener("input", function () { |
| 15 | + if (document.querySelector("#comments-enabled").checked) { |
| 16 | + addBox(); |
| 17 | + } else { |
| 18 | + if ( |
| 19 | + document.querySelector("form.scratchtools.comments-on") !== null |
| 20 | + ) { |
| 21 | + document |
| 22 | + .querySelector("form.scratchtools.comments-on") |
| 23 | + .remove(); |
| 24 | + } |
| 25 | + } |
65 | 26 | });
|
66 |
| - document.querySelector(".postComment").onclick = function () { |
67 |
| - postComment(div.querySelector("textarea").value); |
68 |
| - div.querySelector("textarea").value = ""; |
69 |
| - }; |
70 |
| - document.querySelector(".cancelComment").onclick = function () { |
71 |
| - div.querySelector("textarea").value = ""; |
72 |
| - div.querySelector("#chars-left").textContent = ( |
73 |
| - 500 - div.querySelector("textarea").value.length |
74 |
| - ).toString(); |
75 |
| - }; |
76 |
| - } |
77 | 27 | }
|
| 28 | + } |
| 29 | + addBox(); |
78 | 30 |
|
79 |
| - async function postComment(content) { |
80 |
| - var open = await fetch( |
81 |
| - "https://scratch.mit.edu/site-api/comments/user/" + |
82 |
| - Scratch.INIT_DATA.LOGGED_IN_USER.model.username + |
83 |
| - "/toggle-comments/", |
84 |
| - { |
85 |
| - headers: { |
86 |
| - accept: "*/*", |
87 |
| - "accept-language": "en-US,en;q=0.9,el;q=0.8", |
88 |
| - "sec-ch-ua": |
89 |
| - '" Not A;Brand";v="99", "Chromium";v="102", "Google Chrome";v="102"', |
90 |
| - "sec-ch-ua-mobile": "?0", |
91 |
| - "sec-ch-ua-platform": '"macOS"', |
92 |
| - "sec-fetch-dest": "empty", |
93 |
| - "sec-fetch-mode": "cors", |
94 |
| - "sec-fetch-site": "same-origin", |
95 |
| - "x-csrftoken": ScratchTools.cookies.get("scratchcsrftoken"), |
96 |
| - "x-requested-with": "XMLHttpRequest", |
97 |
| - }, |
98 |
| - referrer: window.location.href, |
99 |
| - referrerPolicy: "strict-origin-when-cross-origin", |
100 |
| - body: null, |
101 |
| - method: "POST", |
102 |
| - mode: "cors", |
103 |
| - credentials: "include", |
104 |
| - } |
105 |
| - ); |
106 |
| - var openData = await open.text(); |
107 |
| - var response = await fetch( |
108 |
| - "https://scratch.mit.edu/site-api/comments/user/" + |
109 |
| - Scratch.INIT_DATA.LOGGED_IN_USER.model.username + |
110 |
| - "/add/", |
111 |
| - { |
112 |
| - headers: { |
113 |
| - accept: "text/html, */*; q=0.01", |
114 |
| - "accept-language": "en-US,en;q=0.9,el;q=0.8", |
115 |
| - "content-type": "application/x-www-form-urlencoded; charset=UTF-8", |
116 |
| - "sec-ch-ua": |
117 |
| - '" Not A;Brand";v="99", "Chromium";v="102", "Google Chrome";v="102"', |
118 |
| - "sec-ch-ua-mobile": "?0", |
119 |
| - "sec-ch-ua-platform": '"macOS"', |
120 |
| - "sec-fetch-dest": "empty", |
121 |
| - "sec-fetch-mode": "cors", |
122 |
| - "sec-fetch-site": "same-origin", |
123 |
| - "x-csrftoken": ScratchTools.cookies.get("scratchcsrftoken"), |
124 |
| - "x-requested-with": "XMLHttpRequest", |
125 |
| - }, |
126 |
| - referrer: window.location.href, |
127 |
| - referrerPolicy: "strict-origin-when-cross-origin", |
128 |
| - body: |
129 |
| - '{"content":"' + content + '","parent_id":"","commentee_id":""}', |
130 |
| - method: "POST", |
131 |
| - mode: "cors", |
132 |
| - credentials: "include", |
133 |
| - } |
134 |
| - ); |
135 |
| - var close = await fetch( |
136 |
| - "https://scratch.mit.edu/site-api/comments/user/" + |
137 |
| - Scratch.INIT_DATA.LOGGED_IN_USER.model.username + |
138 |
| - "/toggle-comments/", |
139 |
| - { |
140 |
| - headers: { |
141 |
| - accept: "*/*", |
142 |
| - "accept-language": "en-US,en;q=0.9,el;q=0.8", |
143 |
| - "sec-ch-ua": |
144 |
| - '" Not A;Brand";v="99", "Chromium";v="102", "Google Chrome";v="102"', |
145 |
| - "sec-ch-ua-mobile": "?0", |
146 |
| - "sec-ch-ua-platform": '"macOS"', |
147 |
| - "sec-fetch-dest": "empty", |
148 |
| - "sec-fetch-mode": "cors", |
149 |
| - "sec-fetch-site": "same-origin", |
150 |
| - "x-csrftoken": ScratchTools.cookies.get("scratchcsrftoken"), |
151 |
| - "x-requested-with": "XMLHttpRequest", |
152 |
| - }, |
153 |
| - referrer: window.location.href, |
154 |
| - referrerPolicy: "strict-origin-when-cross-origin", |
155 |
| - body: null, |
156 |
| - method: "POST", |
157 |
| - mode: "cors", |
158 |
| - credentials: "include", |
159 |
| - } |
160 |
| - ); |
161 |
| - var closeData = await close.text(); |
162 |
| - ScratchTools.modals.create({ |
163 |
| - title: "Comment posted", |
164 |
| - description: "Reloading page..." |
165 |
| - }) |
166 |
| - window.location.href = window.location.href; |
| 31 | + function addBox() { |
| 32 | + var div = document.querySelector(".comments-off"); |
| 33 | + if (div !== null) { |
| 34 | + div.className = ""; |
| 35 | + div.innerHTML = `<form id="main-post-form" class="comments-on scratchtools" style=" |
| 36 | + float: left; |
| 37 | + margin: 0; |
| 38 | + margin-left: 10px; |
| 39 | + margin-bottom: 0; |
| 40 | + display: block; |
| 41 | + "> |
| 42 | + <div class="control-group tooltip right"> |
| 43 | + <textarea name="content" placeholder="Commenting is closed, but you can comment anyways."></textarea> |
| 44 | + <span class="small-text">You have <span id="chars-left">500</span> characters left.</span> |
| 45 | + </div> |
| 46 | + <div class="control-group error"> |
| 47 | + <div class="button small postComment" data-parent-thread="" data-commentee-id=""><a>Post</a></div> |
| 48 | + <div class="button small grey cancelComment" data-control="cancel"><a>Cancel</a></div> |
| 49 | + <span class="notification"></span> |
| 50 | + </div> |
| 51 | + </form>`; |
| 52 | + div.querySelector("textarea").addEventListener("input", function () { |
| 53 | + div.querySelector("#chars-left").textContent = ( |
| 54 | + 500 - div.querySelector("textarea").value.length |
| 55 | + ).toString(); |
| 56 | + }); |
| 57 | + document.querySelector(".postComment").onclick = function () { |
| 58 | + postComment(div.querySelector("textarea").value); |
| 59 | + div.querySelector("textarea").value = ""; |
| 60 | + }; |
| 61 | + document.querySelector(".cancelComment").onclick = function () { |
| 62 | + div.querySelector("textarea").value = ""; |
| 63 | + div.querySelector("#chars-left").textContent = ( |
| 64 | + 500 - div.querySelector("textarea").value.length |
| 65 | + ).toString(); |
| 66 | + }; |
167 | 67 | }
|
168 | 68 | }
|
| 69 | + |
| 70 | + async function postComment(content) { |
| 71 | + var open = await fetch( |
| 72 | + "https://scratch.mit.edu/site-api/comments/user/" + |
| 73 | + loggedInUser + |
| 74 | + "/toggle-comments/", |
| 75 | + { |
| 76 | + headers: { |
| 77 | + accept: "*/*", |
| 78 | + "x-csrftoken": ScratchTools.cookies.get("scratchcsrftoken"), |
| 79 | + "x-requested-with": "XMLHttpRequest", |
| 80 | + }, |
| 81 | + method: "POST", |
| 82 | + credentials: "include", |
| 83 | + } |
| 84 | + ); |
| 85 | + |
| 86 | + var response = await fetch( |
| 87 | + "https://scratch.mit.edu/site-api/comments/user/" + |
| 88 | + loggedInUser + |
| 89 | + "/add/", |
| 90 | + { |
| 91 | + headers: { |
| 92 | + accept: "text/html, */*; q=0.01", |
| 93 | + "content-type": "application/x-www-form-urlencoded; charset=UTF-8", |
| 94 | + "x-csrftoken": ScratchTools.cookies.get("scratchcsrftoken"), |
| 95 | + "x-requested-with": "XMLHttpRequest", |
| 96 | + }, |
| 97 | + body: `{"content":"${content}","parent_id":"","commentee_id":""}`, |
| 98 | + method: "POST", |
| 99 | + credentials: "include", |
| 100 | + } |
| 101 | + ); |
| 102 | + |
| 103 | + var close = await fetch( |
| 104 | + "https://scratch.mit.edu/site-api/comments/user/" + |
| 105 | + loggedInUser + |
| 106 | + "/toggle-comments/", |
| 107 | + { |
| 108 | + headers: { |
| 109 | + accept: "*/*", |
| 110 | + "x-csrftoken": ScratchTools.cookies.get("scratchcsrftoken"), |
| 111 | + "x-requested-with": "XMLHttpRequest", |
| 112 | + }, |
| 113 | + method: "POST", |
| 114 | + credentials: "include", |
| 115 | + } |
| 116 | + ); |
| 117 | + |
| 118 | + ScratchTools.modals.create({ |
| 119 | + title: "Comment posted", |
| 120 | + description: "Reloading page...", |
| 121 | + }); |
| 122 | + |
| 123 | + window.location.href = window.location.href; |
| 124 | + } |
169 | 125 | }
|
| 126 | + |
170 | 127 | if (window.location.href.startsWith("https://scratch.mit.edu/users/")) {
|
171 | 128 | var waitForNoCommentsObserver = new MutationObserver(addNoCommentBox);
|
172 | 129 | waitForNoCommentsObserver.observe(document.querySelector("body"), {
|
|
0 commit comments