Skip to content

Commit 116778d

Browse files
authored
fix: Fixes support for profile pages
1 parent da2e236 commit 116778d

File tree

1 file changed

+150
-118
lines changed

1 file changed

+150
-118
lines changed

script.js

Lines changed: 150 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
// ==UserScript==
22
// @name Unedit and Undelete for Reddit
33
// @namespace http://tampermonkey.net/
4-
// @version 3.6.0
4+
// @version 3.6.1
55
// @description Creates the option next to edited and deleted Reddit comments/posts to show the original comment from before it was edited
66
// @author u/DenverCoder1
77
// @match *://*reddit.com/*
88
// @include https://*.reddit.com/*
99
// @include https://reddit.com/*
1010
// @grant none
1111
// @require https://cdn.jsdelivr.net/npm/showdown@1.9.0/dist/showdown.min.js
12+
// @license MIT
1213
// ==/UserScript==
1314

15+
/* jshint esversion: 8 */
16+
1417
(function () {
15-
'use strict';
18+
"use strict";
1619

1720
/* check if website is an old reddit url or has an old reddit image header */
1821
var isOldReddit = /old\.reddit/.test(window.location.href) || !!document.querySelector("#header-img");
@@ -31,28 +34,26 @@
3134
var id = "";
3235
try {
3336
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 {
4142
id = e.parentElement.parentElement.parentElement.id;
4243
/* old reddit submission */
4344
if (id === "" && isInSubmission(e)) {
4445
id = window.location.href.match(/comments\/([A-Za-z0-9]{5,8})\//)[1];
45-
}
46+
} else {
4647
/* old reddit comment */
47-
else {
4848
id = id.split("_").slice(1).join("_");
4949
}
5050
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");
5254
}
5355
}
54-
}
55-
catch (error) {
56+
} catch (error) {
5657
return null;
5758
}
5859
return id;
@@ -66,28 +67,26 @@
6667
if (!old) {
6768
var baseEl = document.getElementById(id);
6869
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;
7373
}
74-
baseEl = document.querySelector(".Comment."+id);
74+
baseEl = document.querySelector(".Comment." + id);
7575
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;
8079
}
81-
}
80+
} else {
8281
/* 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;
8687
}
87-
if (!el) { el = document.querySelector('.report-'+id).parentElement.parentElement; }
8888
}
89-
}
90-
catch (error) {
89+
} catch (error) {
9190
return null;
9291
}
9392
return el;
@@ -115,7 +114,7 @@
115114
var origBody = document.createElement("p");
116115
origBody.className = "og";
117116
/* 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);
119118
/* paragraph styling */
120119
origBody.style.opacity = 0.96;
121120
origBody.style.fontSize = "14px";
@@ -125,9 +124,9 @@
125124
origBody.style.lineHeight = "20px";
126125
commentBodyElement.appendChild(origBody);
127126
/* scroll into view */
128-
setTimeout(function(){
127+
setTimeout(function () {
129128
if (!isInViewport(origBody)) {
130-
origBody.scrollIntoView({behavior: "smooth"});
129+
origBody.scrollIntoView({ behavior: "smooth" });
131130
}
132131
}, 500);
133132
}
@@ -144,74 +143,97 @@
144143
x.parentElement.appendChild(l);
145144
x.className += " found";
146145
/* 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+
);
215237
}
216238

217239
/* locate comments and call function to add links to each */
@@ -225,36 +247,46 @@
225247
/* Redesign */
226248
if (!isOldReddit) {
227249
/* 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+
);
229254
/* edited comments */
230255
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";
233257
});
234258
/* 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 {
240268
/* Old Reddit */
241-
else {
242269
/* edited comments and submissions */
243270
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";
246272
});
247273
}
248274
/* create links */
249-
editedComments.forEach(function (x, y, z) { createLink(x); });
275+
editedComments.forEach(function (x, y, z) {
276+
createLink(x);
277+
});
250278
}
251279

252280
/* 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+
);
258290

259291
findEditedComments();
260292
})();

0 commit comments

Comments
 (0)