|
| 1 | +export default async function ({ feature, console }) { |
| 2 | + window.feature = feature |
| 3 | + |
| 4 | + ScratchTools.waitForElements(".flex-row.comment", function (comment) { |
| 5 | + let data = feature.redux |
| 6 | + .getState() |
| 7 | + .comments.comments.find( |
| 8 | + (c) => c.id.toString() === comment.id.split("-")[1] |
| 9 | + ); |
| 10 | + |
| 11 | + if (data) { |
| 12 | + let replyCount = |
| 13 | + feature.redux.getState().comments.replies[data.id]?.length || 0; |
| 14 | + let repliesLeft = 25 - replyCount; |
| 15 | + |
| 16 | + updateReply(data.id, repliesLeft); |
| 17 | + } else { |
| 18 | + let parent = findParent(Number(comment.id.split("-")[1])); |
| 19 | + |
| 20 | + if (parent) { |
| 21 | + let replyCount = |
| 22 | + feature.redux.getState().comments.replies[parent]?.length || 0; |
| 23 | + let repliesLeft = 25 - replyCount; |
| 24 | + updateReply(parent, repliesLeft); |
| 25 | + } else { |
| 26 | + console.log("nope") |
| 27 | + } |
| 28 | + } |
| 29 | + }); |
| 30 | + |
| 31 | + function findParent(replyId) { |
| 32 | + let replies = feature.redux.getState().comments.replies; |
| 33 | + let keys = Object.keys(replies); |
| 34 | + |
| 35 | + let key = keys.find((k) => replies[k].find((r) => r.id === replyId)); |
| 36 | + |
| 37 | + return key ? Number(key) : null; |
| 38 | + } |
| 39 | + |
| 40 | + function updateReply(commentId, count) { |
| 41 | + let div = document.querySelector(`.comment#comments-${commentId}`); |
| 42 | + if (!div) return; |
| 43 | + |
| 44 | + let reply = div.querySelector(".comment-reply span"); |
| 45 | + |
| 46 | + if (reply.querySelector(".ste-reply-count")) { |
| 47 | + reply.querySelector( |
| 48 | + ".ste-reply-count" |
| 49 | + ).textContent = ` (${count.toString()} left)`; |
| 50 | + } else { |
| 51 | + let span = document.createElement("span"); |
| 52 | + span.className = "ste-reply-count"; |
| 53 | + feature.self.hideOnDisable(span) |
| 54 | + span.textContent = ` (${count.toString()} left)`; |
| 55 | + reply.appendChild(span); |
| 56 | + } |
| 57 | + |
| 58 | + let data = feature.redux |
| 59 | + .getState() |
| 60 | + .comments.comments.find((c) => c.id.toString() === commentId.toString()); |
| 61 | + |
| 62 | + let replies = feature.redux.getState().comments.replies[commentId.toString()]; |
| 63 | + |
| 64 | + if (data && replies) { |
| 65 | + for (var i in replies) { |
| 66 | + updateReply(replies[i].id, count); |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | +} |
0 commit comments