|
1 | 1 | // ==UserScript==
|
2 | 2 | // @name Unedit and Undelete for Reddit
|
3 | 3 | // @namespace http://tampermonkey.net/
|
4 |
| -// @version 3.6.0 |
| 4 | +// @version 3.6.1 |
5 | 5 | // @description Creates the option next to edited and deleted Reddit comments/posts to show the original comment from before it was edited
|
6 | 6 | // @author u/DenverCoder1
|
7 | 7 | // @match *://*reddit.com/*
|
8 | 8 | // @include https://*.reddit.com/*
|
9 | 9 | // @include https://reddit.com/*
|
10 | 10 | // @grant none
|
11 | 11 | // @require https://cdn.jsdelivr.net/npm/showdown@1.9.0/dist/showdown.min.js
|
| 12 | +// @license MIT |
12 | 13 | // ==/UserScript==
|
13 | 14 |
|
| 15 | +/* jshint esversion: 8 */ |
| 16 | + |
14 | 17 | (function () {
|
15 |
| - 'use strict'; |
| 18 | + "use strict"; |
16 | 19 |
|
17 | 20 | /* check if website is an old reddit url or has an old reddit image header */
|
18 | 21 | var isOldReddit = /old\.reddit/.test(window.location.href) || !!document.querySelector("#header-img");
|
|
31 | 34 | var id = "";
|
32 | 35 | try {
|
33 | 36 | if (!old) {
|
34 |
| - var comment = e?.parentElement?.parentElement?.parentElement?.parentElement?.querySelector(".Comment"); |
35 |
| - if (!comment) { |
36 |
| - comment = e?.parentElement?.parentElement?.parentElement?.parentElement; |
37 |
| - } |
38 |
| - id = Array.from(comment.classList).filter(function (x) { return x.indexOf("_") > -1; })[0]; |
39 |
| - } |
40 |
| - else { |
| 37 | + var comment = e?.closest(".Comment") || e?.closest("[class*=t1_]"); |
| 38 | + id = Array.from(comment.classList).filter(function (x) { |
| 39 | + return x.indexOf("_") > -1; |
| 40 | + })[0]; |
| 41 | + } else { |
41 | 42 | id = e.parentElement.parentElement.parentElement.id;
|
42 | 43 | /* old reddit submission */
|
43 | 44 | if (id === "" && isInSubmission(e)) {
|
44 | 45 | id = window.location.href.match(/comments\/([A-Za-z0-9]{5,8})\//)[1];
|
45 |
| - } |
| 46 | + } else { |
46 | 47 | /* old reddit comment */
|
47 |
| - else { |
48 | 48 | id = id.split("_").slice(1).join("_");
|
49 | 49 | }
|
50 | 50 | if (id === "") {
|
51 |
| - id = e.parentElement.parentElement.getElementsByClassName("reportform")[0].className.replace(/.*t1/,'t1'); |
| 51 | + id = e.parentElement.parentElement |
| 52 | + .getElementsByClassName("reportform")[0] |
| 53 | + .className.replace(/.*t1/, "t1"); |
52 | 54 | }
|
53 | 55 | }
|
54 |
| - } |
55 |
| - catch (error) { |
| 56 | + } catch (error) { |
56 | 57 | return null;
|
57 | 58 | }
|
58 | 59 | return id;
|
|
66 | 67 | if (!old) {
|
67 | 68 | var baseEl = document.getElementById(id);
|
68 | 69 | if (baseEl) {
|
69 |
| - if (baseEl.getElementsByClassName("RichTextJSON-root").length > 0) |
70 |
| - el = baseEl.getElementsByClassName("RichTextJSON-root")[0]; |
71 |
| - else |
72 |
| - el = baseEl; |
| 70 | + if (baseEl.getElementsByClassName("RichTextJSON-root").length > 0) |
| 71 | + el = baseEl.getElementsByClassName("RichTextJSON-root")[0]; |
| 72 | + else el = baseEl; |
73 | 73 | }
|
74 |
| - baseEl = document.querySelector(".Comment."+id); |
| 74 | + baseEl = document.querySelector(".Comment." + id); |
75 | 75 | if (!el && baseEl) {
|
76 |
| - if (baseEl.getElementsByClassName("RichTextJSON-root").length > 0) |
77 |
| - el = baseEl.getElementsByClassName("RichTextJSON-root")[0]; |
78 |
| - else |
79 |
| - el = baseEl.firstElementChild.lastElementChild; |
| 76 | + if (baseEl.getElementsByClassName("RichTextJSON-root").length > 0) |
| 77 | + el = baseEl.getElementsByClassName("RichTextJSON-root")[0]; |
| 78 | + else el = baseEl.firstElementChild.lastElementChild; |
80 | 79 | }
|
81 |
| - } |
| 80 | + } else { |
82 | 81 | /* old reddit */
|
83 |
| - else { |
84 |
| - if (document.querySelector("form[id*="+id+"] div.md")) { |
85 |
| - el = document.querySelector("form[id*="+id+"] div.md"); |
| 82 | + if (document.querySelector("form[id*=" + id + "] div.md")) { |
| 83 | + el = document.querySelector("form[id*=" + id + "] div.md"); |
| 84 | + } |
| 85 | + if (!el) { |
| 86 | + el = document.querySelector(".report-" + id).parentElement.parentElement; |
86 | 87 | }
|
87 |
| - if (!el) { el = document.querySelector('.report-'+id).parentElement.parentElement; } |
88 | 88 | }
|
89 |
| - } |
90 |
| - catch (error) { |
| 89 | + } catch (error) { |
91 | 90 | return null;
|
92 | 91 | }
|
93 | 92 | return el;
|
|
115 | 114 | var origBody = document.createElement("p");
|
116 | 115 | origBody.className = "og";
|
117 | 116 | /* set text */
|
118 |
| - origBody.innerHTML = mdConverter.makeHtml("\n\n### Original "+postType+":\n\n" + body); |
| 117 | + origBody.innerHTML = mdConverter.makeHtml("\n\n### Original " + postType + ":\n\n" + body); |
119 | 118 | /* paragraph styling */
|
120 | 119 | origBody.style.opacity = 0.96;
|
121 | 120 | origBody.style.fontSize = "14px";
|
|
125 | 124 | origBody.style.lineHeight = "20px";
|
126 | 125 | commentBodyElement.appendChild(origBody);
|
127 | 126 | /* scroll into view */
|
128 |
| - setTimeout(function(){ |
| 127 | + setTimeout(function () { |
129 | 128 | if (!isInViewport(origBody)) {
|
130 |
| - origBody.scrollIntoView({behavior: "smooth"}); |
| 129 | + origBody.scrollIntoView({ behavior: "smooth" }); |
131 | 130 | }
|
132 | 131 | }, 500);
|
133 | 132 | }
|
|
144 | 143 | x.parentElement.appendChild(l);
|
145 | 144 | x.className += " found";
|
146 | 145 | /* click event */
|
147 |
| - l.addEventListener("click", async function () { |
148 |
| - /* allow only 1 request at a time */ |
149 |
| - if ((typeof (currentLoading) != "undefined") && (currentLoading !== null)) { return; } |
150 |
| - /* find id of selected comment */ |
151 |
| - var id = getId(this, isOldReddit); |
152 |
| - /* create url for getting comment/post from pushshift api */ |
153 |
| - var idURL = isInSubmission(this) |
154 |
| - ? "https://api.pushshift.io/reddit/search/submission/?ids=" + id + "&sort=desc&sort_type=created_utc&fields=selftext,author,id" |
155 |
| - : "https://api.pushshift.io/reddit/search/comment/?ids=" + id + "&sort=desc&sort_type=created_utc&fields=body,author,id,link_id"; |
156 |
| - /* create url for getting author comments/posts from pushshift api */ |
157 |
| - var author = this.parentElement.querySelector("a[href*=user]")?.innerText; |
158 |
| - var authorURL = isInSubmission(this) |
159 |
| - ? "https://api.pushshift.io/reddit/search/submission/?author=" + author + "&size=200&sort=desc&sort_type=created_utc&fields=selftext,author,id" |
160 |
| - : "https://api.pushshift.io/reddit/search/comment/?author=" + author + "&size=200&sort=desc&sort_type=created_utc&fields=body,author,id,link_id"; |
161 |
| - |
162 |
| - /* set loading status */ |
163 |
| - currentLoading = this; |
164 |
| - this.innerHTML = "loading..."; |
165 |
| - |
166 |
| - /* request from pushshift api */ |
167 |
| - await Promise.all([ |
168 |
| - fetch(idURL).then((resp) => resp.json()), |
169 |
| - fetch(authorURL).then((resp) => resp.json()), |
170 |
| - ]) |
171 |
| - .then((responses) => { |
172 |
| - responses.forEach((out) => { |
173 |
| - /* locate the comment that was being loaded */ |
174 |
| - var loading = currentLoading; |
175 |
| - // exit if already found |
176 |
| - if (loading.innerHTML === "") { return; } |
177 |
| - /* locate comment body */ |
178 |
| - var id = getId(loading, isOldReddit); |
179 |
| - var commentBodyElement = getCommentBodyElement(id, isOldReddit); |
180 |
| - var post = out?.data?.find((post) => post?.id === id?.split("_").pop()); |
181 |
| - console.log({author, id, post}); |
182 |
| - /* check that comment was fetched and body element exists */ |
183 |
| - if (commentBodyElement && post?.body) { |
184 |
| - /* create new paragraph containing the body of the original comment */ |
185 |
| - showOriginalComment(x, commentBodyElement, "comment", post.body); |
186 |
| - /* remove loading status from comment */ |
187 |
| - loading.innerHTML = ""; |
188 |
| - } |
189 |
| - /* check if result has selftext instead of body (it is a submission post) */ |
190 |
| - else if (commentBodyElement && post?.selftext) { |
191 |
| - /* create new paragraph containing the selftext of the original submission */ |
192 |
| - showOriginalComment(x, commentBodyElement, "post", post.selftext); |
193 |
| - /* remove loading status from post */ |
194 |
| - loading.innerHTML = ""; |
195 |
| - } |
196 |
| - /* data was not returned or returned empty */ |
197 |
| - else if (out?.data?.length === 0) { |
198 |
| - loading.innerHTML = "not found"; |
199 |
| - console.log("id: "+id); |
200 |
| - console.log(out); |
201 |
| - } |
202 |
| - /* other issue occurred with displaying comment */ |
203 |
| - else { |
204 |
| - loading.innerHTML = "fetch failed"; |
205 |
| - console.log("id: "+id); |
206 |
| - console.log(out); |
207 |
| - } |
208 |
| - }); |
209 |
| - }) |
210 |
| - .catch(function(err) { throw err; }); |
211 |
| - |
212 |
| - /* reset status */ |
213 |
| - currentLoading = null; |
214 |
| - }, false); |
| 146 | + l.addEventListener( |
| 147 | + "click", |
| 148 | + async function () { |
| 149 | + /* allow only 1 request at a time */ |
| 150 | + if (typeof currentLoading != "undefined" && currentLoading !== null) { |
| 151 | + return; |
| 152 | + } |
| 153 | + /* find id of selected comment */ |
| 154 | + var id = getId(this, isOldReddit); |
| 155 | + /* create url for getting comment/post from pushshift api */ |
| 156 | + var idURL = isInSubmission(this) |
| 157 | + ? "https://api.pushshift.io/reddit/search/submission/?ids=" + |
| 158 | + id + |
| 159 | + "&sort=desc&sort_type=created_utc&fields=selftext,author,id" |
| 160 | + : "https://api.pushshift.io/reddit/search/comment/?ids=" + |
| 161 | + id + |
| 162 | + "&sort=desc&sort_type=created_utc&fields=body,author,id,link_id"; |
| 163 | + /* create url for getting author comments/posts from pushshift api */ |
| 164 | + var author = this.parentElement.querySelector("a[href*=user]")?.innerText; |
| 165 | + var authorURL = isInSubmission(this) |
| 166 | + ? "https://api.pushshift.io/reddit/search/submission/?author=" + |
| 167 | + author + |
| 168 | + "&size=200&sort=desc&sort_type=created_utc&fields=selftext,author,id" |
| 169 | + : "https://api.pushshift.io/reddit/search/comment/?author=" + |
| 170 | + author + |
| 171 | + "&size=200&sort=desc&sort_type=created_utc&fields=body,author,id,link_id"; |
| 172 | + |
| 173 | + /* set loading status */ |
| 174 | + currentLoading = this; |
| 175 | + this.innerHTML = "loading..."; |
| 176 | + |
| 177 | + /* request from pushshift api */ |
| 178 | + await Promise.all([ |
| 179 | + fetch(idURL) |
| 180 | + .then((resp) => resp.json()) |
| 181 | + .catch((error) => { |
| 182 | + console.error("Error:", error); |
| 183 | + }), |
| 184 | + fetch(authorURL) |
| 185 | + .then((resp) => resp.json()) |
| 186 | + .catch((error) => { |
| 187 | + console.error("Error:", error); |
| 188 | + }), |
| 189 | + ]) |
| 190 | + .then((responses) => { |
| 191 | + responses.forEach((out) => { |
| 192 | + /* locate the comment that was being loaded */ |
| 193 | + var loading = currentLoading; |
| 194 | + // exit if already found |
| 195 | + if (loading.innerHTML === "") { |
| 196 | + return; |
| 197 | + } |
| 198 | + /* locate comment body */ |
| 199 | + var id = getId(loading, isOldReddit); |
| 200 | + var commentBodyElement = getCommentBodyElement(id, isOldReddit); |
| 201 | + var post = out?.data?.find((post) => post?.id === id?.split("_").pop()); |
| 202 | + console.log({ author, id, post }); |
| 203 | + /* check that comment was fetched and body element exists */ |
| 204 | + if (commentBodyElement && post?.body) { |
| 205 | + /* create new paragraph containing the body of the original comment */ |
| 206 | + showOriginalComment(x, commentBodyElement, "comment", post.body); |
| 207 | + /* remove loading status from comment */ |
| 208 | + loading.innerHTML = ""; |
| 209 | + } else if (commentBodyElement && post?.selftext) { |
| 210 | + /* check if result has selftext instead of body (it is a submission post) */ |
| 211 | + /* create new paragraph containing the selftext of the original submission */ |
| 212 | + showOriginalComment(x, commentBodyElement, "post", post.selftext); |
| 213 | + /* remove loading status from post */ |
| 214 | + loading.innerHTML = ""; |
| 215 | + } else if (out?.data?.length === 0) { |
| 216 | + /* data was not returned or returned empty */ |
| 217 | + loading.innerHTML = "not found"; |
| 218 | + console.log("id: " + id); |
| 219 | + console.log(out); |
| 220 | + } else { |
| 221 | + /* other issue occurred with displaying comment */ |
| 222 | + loading.innerHTML = "fetch failed"; |
| 223 | + console.log("id: " + id); |
| 224 | + console.log(out); |
| 225 | + } |
| 226 | + }); |
| 227 | + }) |
| 228 | + .catch(function (err) { |
| 229 | + throw err; |
| 230 | + }); |
| 231 | + |
| 232 | + /* reset status */ |
| 233 | + currentLoading = null; |
| 234 | + }, |
| 235 | + false |
| 236 | + ); |
215 | 237 | }
|
216 | 238 |
|
217 | 239 | /* locate comments and call function to add links to each */
|
|
225 | 247 | /* Redesign */
|
226 | 248 | if (!isOldReddit) {
|
227 | 249 | /* fix styling of created paragraphs in new reddit */
|
228 |
| - document.head.insertAdjacentHTML("beforeend", "<style>p.og pre { font-family: monospace; background: #ffffff50; padding: 6px; margin: 6px 0; } p.og h1 { font-size: 2em; } p.og h2 { font-size: 1.5em; } p.og h3 { font-size: 1.17em; } p.og h4 { font-size: 1em; } p.og h5 { font-size: 0.83em; } p.og h6 { font-size: 0.67em; } p.og a { color: lightblue; text-decoration: underline; }</style>"); |
| 250 | + document.head.insertAdjacentHTML( |
| 251 | + "beforeend", |
| 252 | + "<style>p.og pre { font-family: monospace; background: #ffffff50; padding: 6px; margin: 6px 0; } p.og h1 { font-size: 2em; } p.og h2 { font-size: 1.5em; } p.og h3 { font-size: 1.17em; } p.og h4 { font-size: 1em; } p.og h5 { font-size: 0.83em; } p.og h6 { font-size: 0.67em; } p.og a { color: lightblue; text-decoration: underline; }</style>" |
| 253 | + ); |
229 | 254 | /* edited comments */
|
230 | 255 | editedComments = Array.from(document.querySelectorAll(".Comment div span")).filter(function (x, y, z) {
|
231 |
| - return x.parentElement.querySelector("a.showOriginal") === null && |
232 |
| - x.innerText.substr(0, 6) == "edited"; |
| 256 | + return x.parentElement.querySelector("a.showOriginal") === null && x.innerText.substr(0, 6) == "edited"; |
233 | 257 | });
|
234 | 258 | /* include deleted comments */
|
235 |
| - editedComments = editedComments.concat(Array.from(document.querySelectorAll(".Comment div span")).filter(function (x, y, z) { |
236 |
| - return x.parentElement.querySelector("a.showOriginal") === null && |
237 |
| - x.innerText.substr(0, 15) == "Comment deleted"; |
238 |
| - })); |
239 |
| - } |
| 259 | + editedComments = editedComments.concat( |
| 260 | + Array.from(document.querySelectorAll(".Comment div span")).filter(function (x, y, z) { |
| 261 | + return ( |
| 262 | + x.parentElement.querySelector("a.showOriginal") === null && |
| 263 | + x.innerText.substr(0, 15) == "Comment deleted" |
| 264 | + ); |
| 265 | + }) |
| 266 | + ); |
| 267 | + } else { |
240 | 268 | /* Old Reddit */
|
241 |
| - else { |
242 | 269 | /* edited comments and submissions */
|
243 | 270 | editedComments = Array.from(document.querySelectorAll("time")).filter(function (x, y, z) {
|
244 |
| - return Array.from(x.classList).indexOf("found") < 0 && |
245 |
| - x.title.substr(0,11) == "last edited"; |
| 271 | + return Array.from(x.classList).indexOf("found") < 0 && x.title.substr(0, 11) == "last edited"; |
246 | 272 | });
|
247 | 273 | }
|
248 | 274 | /* create links */
|
249 |
| - editedComments.forEach(function (x, y, z) { createLink(x); }); |
| 275 | + editedComments.forEach(function (x, y, z) { |
| 276 | + createLink(x); |
| 277 | + }); |
250 | 278 | }
|
251 | 279 |
|
252 | 280 | /* check for new comments when you scroll */
|
253 |
| - window.addEventListener('scroll', function() { |
254 |
| - if (!scriptTimeout) { |
255 |
| - scriptTimeout = setTimeout(findEditedComments, 1000); |
256 |
| - } |
257 |
| - }, true); |
| 281 | + window.addEventListener( |
| 282 | + "scroll", |
| 283 | + function () { |
| 284 | + if (!scriptTimeout) { |
| 285 | + scriptTimeout = setTimeout(findEditedComments, 1000); |
| 286 | + } |
| 287 | + }, |
| 288 | + true |
| 289 | + ); |
258 | 290 |
|
259 | 291 | findEditedComments();
|
260 | 292 | })();
|
0 commit comments