2023-06-23 10:52:03 +00:00
|
|
|
import {type JSX, render} from "preact";
|
2023-07-15 13:20:05 +00:00
|
|
|
import {
|
|
|
|
extractGroups,
|
|
|
|
initializeGlobals,
|
|
|
|
log,
|
|
|
|
userIsLoggedIn,
|
|
|
|
} from "../utilities/exports.js";
|
2023-07-11 17:07:13 +00:00
|
|
|
import {
|
|
|
|
Data,
|
|
|
|
Feature,
|
|
|
|
MiscellaneousFeature,
|
|
|
|
fromStorage,
|
|
|
|
} from "../storage/exports.js";
|
2023-06-23 10:52:03 +00:00
|
|
|
import {
|
|
|
|
AutocompleteFeature,
|
|
|
|
BackToTopFeature,
|
|
|
|
JumpToNewCommentFeature,
|
|
|
|
UserLabelsFeature,
|
|
|
|
runAnonymizeUsernamesFeature,
|
2023-07-11 17:07:13 +00:00
|
|
|
runCommentAnchorFixFeature,
|
2023-07-14 18:10:08 +00:00
|
|
|
runGroupListSubscribeButtonFeature,
|
2023-08-29 10:54:30 +00:00
|
|
|
runHideOwnUsernameFeature,
|
2023-06-30 09:36:26 +00:00
|
|
|
runHideTopicsFeature,
|
2023-06-23 10:52:03 +00:00
|
|
|
runHideVotesFeature,
|
|
|
|
runMarkdownToolbarFeature,
|
|
|
|
runThemedLogoFeature,
|
2023-10-13 11:33:26 +00:00
|
|
|
runShowTopicAuthorFeature,
|
2023-07-13 14:16:37 +00:00
|
|
|
runTopicInfoIgnore,
|
2023-07-16 12:03:20 +00:00
|
|
|
runUnignoreAllButtonFeature,
|
2023-06-23 10:52:03 +00:00
|
|
|
runUsernameColorsFeature,
|
|
|
|
} from "./features/exports.js";
|
|
|
|
|
|
|
|
async function initialize() {
|
|
|
|
const start = window.performance.now();
|
|
|
|
initializeGlobals();
|
|
|
|
const enabledFeatures = await fromStorage(Data.EnabledFeatures);
|
2023-07-14 18:10:08 +00:00
|
|
|
const miscEnabled = await fromStorage(Data.MiscellaneousEnabledFeatures);
|
2023-07-05 10:50:43 +00:00
|
|
|
window.TildesReExtended.debug = enabledFeatures.value.has(Feature.Debug);
|
2023-06-23 10:52:03 +00:00
|
|
|
|
|
|
|
// Any features that will use the knownGroups data should be added to this
|
|
|
|
// array so that when groups are changed on Tildes, TRX can still update
|
|
|
|
// them without having to change the hardcoded values.
|
|
|
|
const usesKnownGroups = new Set<Feature>([Feature.Autocomplete]);
|
|
|
|
const knownGroups = await fromStorage(Data.KnownGroups);
|
2023-06-30 09:36:26 +00:00
|
|
|
const userLabels = await fromStorage(Feature.UserLabels);
|
2023-06-23 10:52:03 +00:00
|
|
|
|
2023-07-15 13:20:05 +00:00
|
|
|
const isLoggedIn = userIsLoggedIn();
|
|
|
|
if (!isLoggedIn) {
|
|
|
|
log("User is not logged in, running with limited features enabled.", true);
|
|
|
|
}
|
|
|
|
|
2023-06-23 10:52:03 +00:00
|
|
|
// Only when any of the features that uses this data are enabled, try to save
|
|
|
|
// the groups.
|
|
|
|
if (
|
|
|
|
Array.from(usesKnownGroups).some((feature) =>
|
|
|
|
enabledFeatures.value.has(feature),
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
const extractedGroups = extractGroups();
|
|
|
|
if (extractedGroups !== undefined) {
|
|
|
|
knownGroups.value = new Set(extractedGroups);
|
|
|
|
await knownGroups.save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const anonymizeUsernamesEnabled = enabledFeatures.value.has(
|
|
|
|
Feature.AnonymizeUsernames,
|
|
|
|
);
|
|
|
|
|
|
|
|
const observerFeatures: Array<() => void | Promise<void>> = [];
|
|
|
|
const observer = new window.MutationObserver(async () => {
|
|
|
|
log("Page mutation detected, rerunning features.");
|
|
|
|
observer.disconnect();
|
|
|
|
await Promise.all(observerFeatures.map(async (feature) => feature()));
|
|
|
|
startObserver();
|
|
|
|
});
|
|
|
|
|
|
|
|
function startObserver() {
|
|
|
|
observer.observe(document.body, {
|
|
|
|
attributes: true,
|
|
|
|
childList: true,
|
|
|
|
subtree: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (anonymizeUsernamesEnabled) {
|
2023-08-13 09:40:40 +00:00
|
|
|
const anonymizeUsernamesData = await fromStorage(
|
|
|
|
Feature.AnonymizeUsernames,
|
|
|
|
);
|
|
|
|
observerFeatures.push(async () => {
|
|
|
|
await runAnonymizeUsernamesFeature(anonymizeUsernamesData.value);
|
2023-06-23 10:52:03 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-06-30 09:36:26 +00:00
|
|
|
if (enabledFeatures.value.has(Feature.HideTopics)) {
|
|
|
|
observerFeatures.push(async () => {
|
|
|
|
await runHideTopicsFeature(userLabels);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-06-23 10:52:03 +00:00
|
|
|
if (enabledFeatures.value.has(Feature.HideVotes)) {
|
|
|
|
observerFeatures.push(async () => {
|
|
|
|
const data = await fromStorage(Feature.HideVotes);
|
|
|
|
runHideVotesFeature(data.value);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-07-15 13:20:05 +00:00
|
|
|
if (enabledFeatures.value.has(Feature.MarkdownToolbar) && isLoggedIn) {
|
2023-06-23 10:52:03 +00:00
|
|
|
observerFeatures.push(() => {
|
|
|
|
runMarkdownToolbarFeature();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (enabledFeatures.value.has(Feature.ThemedLogo)) {
|
|
|
|
observerFeatures.push(() => {
|
|
|
|
runThemedLogoFeature();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (enabledFeatures.value.has(Feature.UsernameColors)) {
|
|
|
|
observerFeatures.push(async () => {
|
|
|
|
const data = await fromStorage(Feature.UsernameColors);
|
2023-07-10 10:14:39 +00:00
|
|
|
const randomizeUsernameColors = await fromStorage(
|
|
|
|
Data.RandomizeUsernameColors,
|
|
|
|
);
|
|
|
|
await runUsernameColorsFeature(
|
|
|
|
data,
|
|
|
|
anonymizeUsernamesEnabled,
|
|
|
|
randomizeUsernameColors.value,
|
|
|
|
);
|
2023-06-23 10:52:03 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize all the observer-dependent features first.
|
|
|
|
await Promise.all(observerFeatures.map(async (feature) => feature()));
|
|
|
|
|
|
|
|
// Object to hold the active components we are going to render.
|
|
|
|
const components: Record<string, JSX.Element | undefined> = {};
|
|
|
|
|
2023-07-15 13:20:05 +00:00
|
|
|
if (enabledFeatures.value.has(Feature.Autocomplete) && isLoggedIn) {
|
2023-06-23 10:52:03 +00:00
|
|
|
components.autocomplete = (
|
|
|
|
<AutocompleteFeature
|
|
|
|
anonymizeUsernamesEnabled={anonymizeUsernamesEnabled}
|
|
|
|
knownGroups={knownGroups.value}
|
2023-06-24 12:00:27 +00:00
|
|
|
userLabels={userLabels}
|
2023-06-23 10:52:03 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (enabledFeatures.value.has(Feature.BackToTop)) {
|
|
|
|
components.backToTop = <BackToTopFeature />;
|
|
|
|
}
|
|
|
|
|
2023-07-15 13:20:05 +00:00
|
|
|
if (enabledFeatures.value.has(Feature.JumpToNewComment) && isLoggedIn) {
|
2023-06-23 10:52:03 +00:00
|
|
|
components.jumpToNewComment = <JumpToNewCommentFeature />;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (enabledFeatures.value.has(Feature.UserLabels)) {
|
2023-10-19 12:09:00 +00:00
|
|
|
const onSiteNewLabelEnabled = await fromStorage(Data.OnSiteNewLabel);
|
2023-06-23 10:52:03 +00:00
|
|
|
components.userLabels = (
|
|
|
|
<UserLabelsFeature
|
|
|
|
anonymizeUsernamesEnabled={anonymizeUsernamesEnabled}
|
2023-10-19 12:09:00 +00:00
|
|
|
onSiteNewLabelEnabled={onSiteNewLabelEnabled.value}
|
2023-06-23 10:52:03 +00:00
|
|
|
userLabels={userLabels}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-10-07 11:41:59 +00:00
|
|
|
if (enabledFeatures.value.has(Feature.Miscellaneous)) {
|
|
|
|
if (miscEnabled.value.has(MiscellaneousFeature.CommentAnchorFix)) {
|
|
|
|
runCommentAnchorFixFeature();
|
|
|
|
}
|
2023-07-13 14:16:37 +00:00
|
|
|
|
2023-10-07 11:41:59 +00:00
|
|
|
if (
|
|
|
|
miscEnabled.value.has(MiscellaneousFeature.GroupListSubscribeButtons) &&
|
|
|
|
isLoggedIn
|
|
|
|
) {
|
|
|
|
runGroupListSubscribeButtonFeature();
|
|
|
|
}
|
2023-07-14 18:10:08 +00:00
|
|
|
|
2023-10-07 11:41:59 +00:00
|
|
|
if (
|
|
|
|
miscEnabled.value.has(MiscellaneousFeature.HideOwnUsername) &&
|
|
|
|
isLoggedIn
|
|
|
|
) {
|
|
|
|
runHideOwnUsernameFeature();
|
|
|
|
}
|
2023-08-29 10:54:30 +00:00
|
|
|
|
2023-10-13 11:33:26 +00:00
|
|
|
if (miscEnabled.value.has(MiscellaneousFeature.ShowTopicAuthor)) {
|
|
|
|
runShowTopicAuthorFeature();
|
|
|
|
}
|
|
|
|
|
2023-10-07 11:41:59 +00:00
|
|
|
if (
|
|
|
|
miscEnabled.value.has(MiscellaneousFeature.TopicInfoIgnore) &&
|
|
|
|
isLoggedIn
|
|
|
|
) {
|
|
|
|
runTopicInfoIgnore();
|
|
|
|
}
|
2023-07-13 14:16:37 +00:00
|
|
|
|
2023-10-07 11:41:59 +00:00
|
|
|
if (
|
|
|
|
miscEnabled.value.has(MiscellaneousFeature.UnignoreAllButton) &&
|
|
|
|
isLoggedIn
|
|
|
|
) {
|
|
|
|
runUnignoreAllButtonFeature();
|
|
|
|
}
|
2023-07-16 12:03:20 +00:00
|
|
|
}
|
|
|
|
|
2023-06-23 10:52:03 +00:00
|
|
|
// Insert a placeholder at the end of the body first, then render the rest
|
|
|
|
// and use that as the replacement element. Otherwise render() would put it
|
|
|
|
// at the beginning of the body which causes a bunch of different issues.
|
|
|
|
const replacement = document.createElement("div");
|
|
|
|
document.body.append(replacement);
|
|
|
|
|
|
|
|
// The jump to new comment button must come right before
|
|
|
|
// the back to top button. The CSS depends on them being in this order.
|
|
|
|
render(
|
|
|
|
<div id="trx-container">
|
|
|
|
{components.jumpToNewComment} {components.backToTop}
|
|
|
|
{components.autocomplete} {components.userLabels}
|
|
|
|
</div>,
|
|
|
|
document.body,
|
|
|
|
replacement,
|
|
|
|
);
|
|
|
|
|
|
|
|
// Start the mutation observer only when some features depend on it are enabled.
|
|
|
|
if (observerFeatures.length > 0) {
|
|
|
|
startObserver();
|
|
|
|
}
|
|
|
|
|
|
|
|
const initializedIn = window.performance.now() - start;
|
|
|
|
log(`Initialized in approximately ${initializedIn} milliseconds.`);
|
|
|
|
}
|
|
|
|
|
|
|
|
document.addEventListener("DOMContentLoaded", initialize);
|