1
Fork 0

Add a utility function for getting the logged in username.

This commit is contained in:
Bauke 2023-08-30 12:09:20 +02:00
parent ee768cfd41
commit 8ab9df1d85
Signed by: Bauke
GPG Key ID: C1C0F29952BCF558
2 changed files with 20 additions and 4 deletions

View File

@ -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 "<logged out>" 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() ??
"<logged out>";
const currentUser = getLoggedInUsername() ?? "<logged out>";
if (data.otherComments || data.ownComments) {
count += hideCommentVotes(data, currentUser);

View File

@ -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<HTMLElement>(".logged-in-user-username") ??
undefined;
return (
loggedInUsername?.dataset.trxHideOwnUsername ??
loggedInUsername?.textContent ??
undefined
);
}