From 0542b09e0f0de2ed8c3ca3472862a8891e174d2f Mon Sep 17 00:00:00 2001 From: Bauke Date: Sun, 25 Jun 2023 13:28:26 +0200 Subject: [PATCH] Rework the Hide Votes logic so it explicitly handles our own and other's votes. --- source/content-scripts/features/hide-votes.ts | 140 +++++++++++++----- 1 file changed, 102 insertions(+), 38 deletions(-) diff --git a/source/content-scripts/features/hide-votes.ts b/source/content-scripts/features/hide-votes.ts index 90917f3..cce483a 100644 --- a/source/content-scripts/features/hide-votes.ts +++ b/source/content-scripts/features/hide-votes.ts @@ -9,52 +9,116 @@ export function runHideVotesFeature(data: HideVotesData) { function hideVotes(data: HideVotesData): number { let count = 0; - if (data.otherComments) { - const commentVotes = querySelectorAll( - '.btn-post-action[data-ic-put-to*="/vote"]:not(.trx-votes-hidden)', - '.btn-post-action[data-ic-delete-from*="/vote"]:not(.trx-votes-hidden)', - ); - count += commentVotes.length; + // Get the username of the currently logged in user. When not logged in, set + // it to "" which isn't a valid username, so matching against it + // will always return false. Meaning all comments are never the current user's. + const currentUser = + document.querySelector(".logged-in-user-username")?.textContent?.trim() ?? + ""; - for (const vote of commentVotes) { - vote.classList.add("trx-votes-hidden"); - if (!vote.textContent!.includes(" ")) { - continue; - } - - vote.textContent = vote.textContent!.slice( - 0, - vote.textContent!.indexOf(" "), - ); - } - } - - if (data.ownComments) { - const ownComments = querySelectorAll(".comment-votes"); - count += ownComments.length; - for (const vote of ownComments) { - vote.classList.add("trx-hidden"); - } + if (data.otherComments || data.ownComments) { + count += hideCommentVotes(data, currentUser); } if (data.otherTopics || data.ownTopics) { - const selectors: string[] = []; + count += hideTopicVotes(data, currentUser); + } - // Topics by other people will be encapsulated with a `