Add a utility function for getting the logged in username.
This commit is contained in:
parent
ee768cfd41
commit
8ab9df1d85
|
@ -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);
|
||||
|
|
|
@ -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
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue