From 809943edcc01fc3c81ec59ebdfabfb7f8669656a Mon Sep 17 00:00:00 2001 From: Bauke Date: Sat, 15 Jul 2023 15:20:05 +0200 Subject: [PATCH] Gate certain features by requiring a logged in user. --- source/content-scripts/setup.tsx | 28 ++++++++++++++++++++++------ source/utilities/exports.ts | 1 + source/utilities/user.ts | 6 ++++++ 3 files changed, 29 insertions(+), 6 deletions(-) create mode 100644 source/utilities/user.ts diff --git a/source/content-scripts/setup.tsx b/source/content-scripts/setup.tsx index c933f2c..164b3be 100644 --- a/source/content-scripts/setup.tsx +++ b/source/content-scripts/setup.tsx @@ -1,5 +1,10 @@ import {type JSX, render} from "preact"; -import {extractGroups, initializeGlobals, log} from "../utilities/exports.js"; +import { + extractGroups, + initializeGlobals, + log, + userIsLoggedIn, +} from "../utilities/exports.js"; import { Data, Feature, @@ -36,6 +41,11 @@ async function initialize() { const knownGroups = await fromStorage(Data.KnownGroups); const userLabels = await fromStorage(Feature.UserLabels); + const isLoggedIn = userIsLoggedIn(); + if (!isLoggedIn) { + log("User is not logged in, running with limited features enabled.", true); + } + // Only when any of the features that uses this data are enabled, try to save // the groups. if ( @@ -89,7 +99,7 @@ async function initialize() { }); } - if (enabledFeatures.value.has(Feature.MarkdownToolbar)) { + if (enabledFeatures.value.has(Feature.MarkdownToolbar) && isLoggedIn) { observerFeatures.push(() => { runMarkdownToolbarFeature(); }); @@ -121,7 +131,7 @@ async function initialize() { // Object to hold the active components we are going to render. const components: Record = {}; - if (enabledFeatures.value.has(Feature.Autocomplete)) { + if (enabledFeatures.value.has(Feature.Autocomplete) && isLoggedIn) { components.autocomplete = ( ; } - if (enabledFeatures.value.has(Feature.JumpToNewComment)) { + if (enabledFeatures.value.has(Feature.JumpToNewComment) && isLoggedIn) { components.jumpToNewComment = ; } @@ -152,11 +162,17 @@ async function initialize() { runCommentAnchorFixFeature(); } - if (miscEnabled.value.has(MiscellaneousFeature.GroupListSubscribeButtons)) { + if ( + miscEnabled.value.has(MiscellaneousFeature.GroupListSubscribeButtons) && + isLoggedIn + ) { runGroupListSubscribeButtonFeature(); } - if (miscEnabled.value.has(MiscellaneousFeature.TopicInfoIgnore)) { + if ( + miscEnabled.value.has(MiscellaneousFeature.TopicInfoIgnore) && + isLoggedIn + ) { runTopicInfoIgnore(); } diff --git a/source/utilities/exports.ts b/source/utilities/exports.ts index 71f0f37..3294472 100644 --- a/source/utilities/exports.ts +++ b/source/utilities/exports.ts @@ -7,4 +7,5 @@ export * from "./logging.js"; export * from "./query-selectors.js"; export * from "./report-a-bug.js"; export * from "./text.js"; +export * from "./user.js"; export * from "./validators.js"; diff --git a/source/utilities/user.ts b/source/utilities/user.ts new file mode 100644 index 0000000..dfa4444 --- /dev/null +++ b/source/utilities/user.ts @@ -0,0 +1,6 @@ +/** + * Checks whether a user is logged in on or not. + */ +export function userIsLoggedIn(): boolean { + return document.querySelector(".logged-in-user-username") !== null; +}