Skip to content

Commit ff15ad6

Browse files
authored
feat: Support for .compact Reddit (#65)
1 parent 923c4fa commit ff15ad6

File tree

1 file changed

+43
-6
lines changed

1 file changed

+43
-6
lines changed

script.js

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@
3838
*/
3939
let isOldReddit = false;
4040

41+
/**
42+
* Whether or not we are on compact mode.
43+
* This will be set in the "load" event listener.
44+
* @type {boolean}
45+
*/
46+
let isCompact = false;
47+
4148
/**
4249
* Timeout to check for new edited comments on page.
4350
* This will be updated when scrolling.
@@ -170,7 +177,7 @@
170177
}
171178
}
172179
// old reddit
173-
else {
180+
else if (!isCompact) {
174181
// old reddit comment
175182
postId = innerEl?.closest(".thing")?.id.replace("thing_", "");
176183
// old reddit submission
@@ -198,6 +205,19 @@
198205
postId = "";
199206
}
200207
}
208+
// compact
209+
else {
210+
const thing = innerEl?.closest(".thing");
211+
if (thing) {
212+
const idClass = [...thing.classList].find((c) => c.startsWith("id-"));
213+
postId = idClass ? idClass.replace("id-", "") : "";
214+
}
215+
// if not found, check the url
216+
if (!postId) {
217+
const parsedURL = parseURL();
218+
postId = parsedURL.commentId || parsedURL.submissionId || postId;
219+
}
220+
}
201221
// if the post appears on the page after the last 3 characters are removed, remove them
202222
const reMatch = postId.match(/(t1_\w+)\w{3}/) || postId.match(/(t3_\w+)\w{3}/);
203223
if (reMatch && document.querySelector(`.${reMatch[1]}, #thing_${reMatch[1]}`)) {
@@ -248,7 +268,7 @@
248268
}
249269
}
250270
// old reddit
251-
else {
271+
else if (!isCompact) {
252272
// old reddit comments
253273
baseEl = document.querySelector(`form[id*='${postId}'] .md`);
254274
if (baseEl?.closest(".entry")) {
@@ -271,6 +291,14 @@
271291
bodyEl = document.querySelector(`.id-${postId}`);
272292
}
273293
}
294+
// compact view
295+
else {
296+
bodyEl = document.querySelector(`.id-${postId} .md, .id-${postId} form.usertext`);
297+
// if not found, check for the .usertext element containing it as part of its id
298+
if (!bodyEl) {
299+
bodyEl = document.querySelector(".showOriginal")?.parentElement;
300+
}
301+
}
274302
return bodyEl;
275303
}
276304

@@ -285,6 +313,14 @@
285313
"div[data-url]", // old reddit on submission page
286314
".Post", // redesign
287315
];
316+
// class list of .thing contains id-t3_...
317+
const thing = innerEl?.closest(".thing");
318+
if (thing) {
319+
const idClass = [...thing.classList].find((c) => c.startsWith("id-"));
320+
if (idClass) {
321+
return idClass.startsWith("id-t3_");
322+
}
323+
}
288324
return Boolean(innerEl.closest(selectors.join(", ")));
289325
}
290326

@@ -434,7 +470,7 @@
434470
const postId = getPostId(showLinkEl);
435471
showLinkEl.alt = `View original post for ID ${postId}`;
436472
if (!postId) {
437-
showLinkEl.style.color = "#dd2c00";
473+
showLinkEl.parentElement.removeChild(showLinkEl);
438474
}
439475
// click event
440476
showLinkEl.addEventListener(
@@ -679,13 +715,13 @@
679715
checkForEditedSubmissions();
680716
}
681717
}
682-
// old Reddit
718+
// old Reddit and compact Reddit
683719
else {
684720
selectors = [
685721
".entry p.tagline time:not(.found)", // Comment or Submission "last edited" timestamp
686-
".entry p.tagline em:not(.found), .entry p.tagline span:first-of-type:not(.found)", // Comment "[deleted]" author
722+
".entry p.tagline em:not(.found), .entry .tagline span:first-of-type:not(.found)", // Comment "[deleted]" author
687723
"div[data-url] p.tagline span:first-of-type:not(.found)", // Submission "[deleted]" author
688-
"div[data-url] .usertext-body em:not(.found)", // Submission "[removed]" body
724+
"div[data-url] .usertext-body em:not(.found), form.usertext em:not(.found)", // Submission "[removed]" body
689725
".entry .usertext .usertext-body > div.md > p:only-child:not(.found)", // Comment "[unavailable]" body
690726
"p#noresults", // "there doesn't seem to be anything here" page
691727
];
@@ -796,6 +832,7 @@
796832
function init() {
797833
// determine if reddit is old or redesign
798834
isOldReddit = /old\.reddit/.test(window.location.href) || !!document.querySelector("#header-img");
835+
isCompact = document.querySelector("#header-img-a")?.href?.endsWith(".compact") || false;
799836
// Reddit redesign
800837
if (!isOldReddit) {
801838
// fix styling of created paragraphs in new reddit

0 commit comments

Comments
 (0)