diff --git a/source/content-scripts/features/hide-votes.ts b/source/content-scripts/features/hide-votes.ts index 70965cf..ba65f0c 100644 --- a/source/content-scripts/features/hide-votes.ts +++ b/source/content-scripts/features/hide-votes.ts @@ -1,5 +1,9 @@ import {type HideVotesData} from "../../storage/exports.js"; -import {log, querySelectorAll} from "../../utilities/exports.js"; +import { + getLoggedInUsername, + log, + querySelectorAll, +} from "../../utilities/exports.js"; export function runHideVotesFeature(data: HideVotesData) { const counts = hideVotes(data); @@ -12,9 +16,7 @@ function hideVotes(data: HideVotesData): number { // 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() ?? - ""; + const currentUser = getLoggedInUsername() ?? ""; if (data.otherComments || data.ownComments) { count += hideCommentVotes(data, currentUser); diff --git a/source/utilities/user.ts b/source/utilities/user.ts index dfa4444..e25778d 100644 --- a/source/utilities/user.ts +++ b/source/utilities/user.ts @@ -4,3 +4,17 @@ export function userIsLoggedIn(): boolean { return document.querySelector(".logged-in-user-username") !== null; } + +/** + * Get the currently logged in user's username, if they are logged in. + */ +export function getLoggedInUsername(): string | undefined { + const loggedInUsername = + document.querySelector(".logged-in-user-username") ?? + undefined; + return ( + loggedInUsername?.dataset.trxHideOwnUsername ?? + loggedInUsername?.textContent ?? + undefined + ); +}