2022-02-23 13:52:06 +00:00
|
|
|
import './scss/scripts.scss';
|
|
|
|
|
2020-10-10 23:32:27 +00:00
|
|
|
import {html} from 'htm/preact';
|
|
|
|
import {render} from 'preact';
|
2022-02-23 13:52:06 +00:00
|
|
|
|
2020-10-10 23:32:27 +00:00
|
|
|
import {
|
|
|
|
AutocompleteFeature,
|
|
|
|
BackToTopFeature,
|
|
|
|
JumpToNewCommentFeature,
|
2022-02-23 13:52:06 +00:00
|
|
|
UserLabelsFeature,
|
2022-02-23 17:17:22 +00:00
|
|
|
runAnonymizeUsernamesFeature,
|
2020-10-10 23:32:27 +00:00
|
|
|
runHideVotesFeature,
|
|
|
|
runMarkdownToolbarFeature,
|
2022-02-25 00:06:24 +00:00
|
|
|
runUsernameColorsFeature,
|
2022-02-23 13:52:06 +00:00
|
|
|
} from './scripts/exports.js';
|
|
|
|
import Settings from './settings.js';
|
|
|
|
import {extractGroups, initializeGlobals, log} from './utilities/exports.js';
|
2020-10-10 23:32:27 +00:00
|
|
|
|
2022-02-23 13:52:06 +00:00
|
|
|
async function initialize() {
|
2020-10-10 23:32:27 +00:00
|
|
|
const start = window.performance.now();
|
2022-02-23 13:52:06 +00:00
|
|
|
initializeGlobals();
|
|
|
|
const settings = await Settings.fromSyncStorage();
|
|
|
|
window.TildesReExtended.debug = settings.features.debug;
|
2020-10-10 23:32:27 +00:00
|
|
|
|
|
|
|
// Any features that will use `settings.data.knownGroups` 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 = [settings.features.autocomplete];
|
|
|
|
// Only when any of the features that uses this data try to save the groups.
|
|
|
|
if (usesKnownGroups.some((value) => value)) {
|
2022-02-23 13:52:06 +00:00
|
|
|
const knownGroups = extractGroups();
|
|
|
|
if (knownGroups !== undefined) {
|
|
|
|
settings.data.knownGroups = knownGroups;
|
|
|
|
await settings.save();
|
|
|
|
}
|
2020-10-10 23:32:27 +00:00
|
|
|
}
|
|
|
|
|
2022-02-23 22:28:33 +00:00
|
|
|
const observerFeatures: Array<() => any> = [];
|
|
|
|
const observer = new window.MutationObserver(() => {
|
|
|
|
log('Page mutation detected, rerunning features.');
|
|
|
|
observer.disconnect();
|
|
|
|
for (const feature of observerFeatures) {
|
|
|
|
feature();
|
|
|
|
}
|
|
|
|
|
|
|
|
startObserver();
|
|
|
|
});
|
|
|
|
|
|
|
|
function startObserver() {
|
|
|
|
observer.observe(document.body, {
|
|
|
|
childList: true,
|
|
|
|
subtree: true,
|
|
|
|
});
|
|
|
|
}
|
2020-10-10 23:32:27 +00:00
|
|
|
|
2022-02-23 17:17:22 +00:00
|
|
|
if (settings.features.anonymizeUsernames) {
|
2022-02-23 22:28:33 +00:00
|
|
|
observerFeatures.push(() => {
|
|
|
|
runAnonymizeUsernamesFeature();
|
|
|
|
});
|
2022-02-23 17:17:22 +00:00
|
|
|
}
|
|
|
|
|
2022-02-23 22:28:33 +00:00
|
|
|
if (settings.features.hideVotes) {
|
|
|
|
observerFeatures.push(() => {
|
|
|
|
runHideVotesFeature(settings);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (settings.features.markdownToolbar) {
|
|
|
|
observerFeatures.push(() => {
|
|
|
|
runMarkdownToolbarFeature();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-02-25 00:06:24 +00:00
|
|
|
if (settings.features.usernameColors) {
|
|
|
|
observerFeatures.push(() => {
|
|
|
|
runUsernameColorsFeature(settings);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-02-23 22:28:33 +00:00
|
|
|
// Initialize all the observer-dependent features first.
|
|
|
|
for (const feature of observerFeatures) {
|
|
|
|
feature();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Object to hold the active components we are going to render.
|
|
|
|
const components: Record<string, TRXComponent | undefined> = {};
|
|
|
|
|
2020-10-10 23:32:27 +00:00
|
|
|
if (settings.features.autocomplete) {
|
|
|
|
components.autocomplete = html`
|
|
|
|
<${AutocompleteFeature} settings=${settings} />
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (settings.features.backToTop) {
|
|
|
|
components.backToTop = html`<${BackToTopFeature} />`;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (settings.features.jumpToNewComment) {
|
|
|
|
components.jumpToNewComment = html`<${JumpToNewCommentFeature} />`;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (settings.features.userLabels) {
|
|
|
|
components.userLabels = html`
|
|
|
|
<${UserLabelsFeature} settings=${settings} />
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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(
|
2022-02-23 13:52:06 +00:00
|
|
|
html`
|
|
|
|
<div id="trx-container">
|
|
|
|
${components.jumpToNewComment} ${components.backToTop}
|
|
|
|
${components.autocomplete} ${components.userLabels}
|
|
|
|
</div>
|
|
|
|
`,
|
2020-10-10 23:32:27 +00:00
|
|
|
document.body,
|
2022-02-23 13:52:06 +00:00
|
|
|
replacement,
|
2020-10-10 23:32:27 +00:00
|
|
|
);
|
|
|
|
|
2022-02-23 22:28:33 +00:00
|
|
|
// Start the mutation observer only when some features depend on it are enabled.
|
|
|
|
if (observerFeatures.length > 0) {
|
|
|
|
startObserver();
|
|
|
|
}
|
|
|
|
|
2020-10-10 23:32:27 +00:00
|
|
|
const initializedIn = window.performance.now() - start;
|
|
|
|
log(`Initialized in approximately ${initializedIn} milliseconds.`);
|
2022-02-23 13:52:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void initialize();
|