diff --git a/source/content-scripts/features/exports.ts b/source/content-scripts/features/exports.ts index 9daed17..5cea373 100644 --- a/source/content-scripts/features/exports.ts +++ b/source/content-scripts/features/exports.ts @@ -8,6 +8,7 @@ export * from "./markdown-toolbar.js"; export * from "./miscellaneous/comment-anchor-fix.js"; export * from "./miscellaneous/group-list-subscribe-button.js"; export * from "./miscellaneous/hide-own-username.js"; +export * from "./miscellaneous/show-topic-author.js"; export * from "./miscellaneous/topic-info-ignore.js"; export * from "./miscellaneous/unignore-all-button.js"; export * from "./themed-logo.js"; diff --git a/source/content-scripts/features/miscellaneous/show-topic-author.ts b/source/content-scripts/features/miscellaneous/show-topic-author.ts new file mode 100644 index 0000000..5b67b02 --- /dev/null +++ b/source/content-scripts/features/miscellaneous/show-topic-author.ts @@ -0,0 +1,52 @@ +import { + createElementFromString, + log, + pluralize, + querySelectorAll, +} from "../../../utilities/exports.js"; + +export function runShowTopicAuthorFeature(): void { + const count = showTopicAuthor(); + if (count > 0) { + const pluralized = `${count} ${pluralize(count, "topic")}`; + log(`Show Topic Author applied to ${pluralized}.`); + } +} + +/** + * Add topic author links to topics that don't already have a user linked in + * their topic info source. + */ +function showTopicAuthor(): number { + const topics = querySelectorAll( + ".topic-listing .topic:not([data-trx-show-topic-author])", + ); + + let count = 0; + for (const topic of topics) { + const topicInfoSource = + topic.querySelector(".topic-info-source"); + + if (topicInfoSource?.querySelector(".link-user") !== null) { + continue; + } + + const author = topic.dataset.topicPostedBy; + if (author === undefined) { + // Technically this should never happen since deleted or removed topics + // don't show in the topic listing. + continue; + } + + topicInfoSource.insertAdjacentElement( + "afterbegin", + createElementFromString( + `${author}`, + ), + ); + topic.dataset.trxShowTopicAuthor = "true"; + count++; + } + + return count; +} diff --git a/source/content-scripts/setup.tsx b/source/content-scripts/setup.tsx index 85bb6d8..03de939 100644 --- a/source/content-scripts/setup.tsx +++ b/source/content-scripts/setup.tsx @@ -24,6 +24,7 @@ import { runHideVotesFeature, runMarkdownToolbarFeature, runThemedLogoFeature, + runShowTopicAuthorFeature, runTopicInfoIgnore, runUnignoreAllButtonFeature, runUsernameColorsFeature, @@ -182,6 +183,10 @@ async function initialize() { runHideOwnUsernameFeature(); } + if (miscEnabled.value.has(MiscellaneousFeature.ShowTopicAuthor)) { + runShowTopicAuthorFeature(); + } + if ( miscEnabled.value.has(MiscellaneousFeature.TopicInfoIgnore) && isLoggedIn diff --git a/source/options/components/miscellaneous.tsx b/source/options/components/miscellaneous.tsx index c28b634..e504d81 100644 --- a/source/options/components/miscellaneous.tsx +++ b/source/options/components/miscellaneous.tsx @@ -39,6 +39,14 @@ function FeatureDescription({ ); } + if (feature === MiscellaneousFeature.ShowTopicAuthor) { + return ( +

+ Always show the topic author's username in the topic listing. +

+ ); + } + if (feature === MiscellaneousFeature.TopicInfoIgnore) { return (

diff --git a/source/scss/content-scripts.scss b/source/scss/content-scripts.scss index c9015ca..86bd60e 100644 --- a/source/scss/content-scripts.scss +++ b/source/scss/content-scripts.scss @@ -5,6 +5,7 @@ @import "scripts/hide-topics"; @import "scripts/jump-to-new-comment"; @import "scripts/markdown-toolbar"; +@import "scripts/show-topic-author"; @import "scripts/topic-info-ignore"; @import "scripts/user-labels"; @import "scripts/username-colors"; diff --git a/source/scss/scripts/_show-topic-author.scss b/source/scss/scripts/_show-topic-author.scss new file mode 100644 index 0000000..88afb54 --- /dev/null +++ b/source/scss/scripts/_show-topic-author.scss @@ -0,0 +1,8 @@ +.topic[data-trx-show-topic-author] .topic-info-source { + .topic-icon { + // Instead of adding a margin-right to the user link we add it to the topic + // favicon so when there are user labels the margin still spaces things out + // as intended. + margin-left: 0.2rem; + } +} diff --git a/source/storage/enums.ts b/source/storage/enums.ts index 6f70e83..98e333e 100644 --- a/source/storage/enums.ts +++ b/source/storage/enums.ts @@ -23,6 +23,7 @@ export enum MiscellaneousFeature { CommentAnchorFix = "comment-anchor-fix", GroupListSubscribeButtons = "group-list-subscribe-buttons", HideOwnUsername = "hide-own-username", + ShowTopicAuthor = "show-topic-author", TopicInfoIgnore = "topic-info-ignore", UnignoreAllButton = "unignore-all-button", }