diff --git a/source/content-scripts/features/username-colors.ts b/source/content-scripts/features/username-colors.ts index fc1f467..b4d32d2 100644 --- a/source/content-scripts/features/username-colors.ts +++ b/source/content-scripts/features/username-colors.ts @@ -1,12 +1,21 @@ -import { log, querySelectorAll, getColorFromStringHash, isColorBright } from "../../utilities/exports.js"; -import { type UsernameColorsData } from "../../storage/exports.js"; +import {type UsernameColorsData} from "../../storage/exports.js"; +import { + log, + querySelectorAll, + getColorFromStringHash, + isColorBright, +} from "../../utilities/exports.js"; export async function runUsernameColorsFeature( data: UsernameColorsData, anonymizeUsernamesEnabled: boolean, randomizeUsernameColorsEnabled: boolean, ) { - const count = await usernameColors(data, anonymizeUsernamesEnabled, randomizeUsernameColorsEnabled); + const count = await usernameColors( + data, + anonymizeUsernamesEnabled, + randomizeUsernameColorsEnabled, + ); log(`Username Colors: Applied ${count} colors.`); } @@ -17,7 +26,7 @@ async function usernameColors( ): Promise { const usernameColors = new Map(); for (const { - value: { color, username: usernames }, + value: {color, username: usernames}, } of data) { for (const username of usernames.split(",")) { usernameColors.set(username.trim().toLowerCase(), color); @@ -42,18 +51,21 @@ async function usernameColors( } element.classList.add("trx-username-colors"); - let color = usernameColors.get(target); - if (color) { + const color = usernameColors.get(target); + if (color !== undefined) { element.style.color = color; } else if (randomizeUsernameColorsEnabled) { + element.classList.add("trx-random-username-color"); const randomColor = await getColorFromStringHash(target); - const fontColor = isColorBright(randomColor) ? "#000" : "#FFF" + const fontColor = isColorBright(randomColor) ? "#000" : "#FFF"; element.style.setProperty("--background-color", randomColor); - element.style.setProperty("--text-color", fontColor); - element.classList.add("trx-colored-username") + // Specifically use "--link-color" here so Tildes's link color doesn't + // override ours. + element.style.setProperty("--link-color", fontColor); } else { continue; } + count += 1; } diff --git a/source/content-scripts/setup.tsx b/source/content-scripts/setup.tsx index 5478a19..35a5ff6 100644 --- a/source/content-scripts/setup.tsx +++ b/source/content-scripts/setup.tsx @@ -95,8 +95,14 @@ async function initialize() { if (enabledFeatures.value.has(Feature.UsernameColors)) { observerFeatures.push(async () => { const data = await fromStorage(Feature.UsernameColors); - const randomizeUsernameColors = await fromStorage(Data.RandomizeUsernameColors); - runUsernameColorsFeature(data, anonymizeUsernamesEnabled, randomizeUsernameColors.value); + const randomizeUsernameColors = await fromStorage( + Data.RandomizeUsernameColors, + ); + await runUsernameColorsFeature( + data, + anonymizeUsernamesEnabled, + randomizeUsernameColors.value, + ); }); } diff --git a/source/options/components/username-colors.tsx b/source/options/components/username-colors.tsx index 0cb424d..4e5368a 100644 --- a/source/options/components/username-colors.tsx +++ b/source/options/components/username-colors.tsx @@ -1,3 +1,4 @@ +import {type Value} from "@holllo/webextension-storage"; import {Component} from "preact"; import {log} from "../../utilities/exports.js"; import { @@ -9,7 +10,6 @@ import { fromStorage, } from "../../storage/exports.js"; import {Setting, type SettingProps} from "./index.js"; -import { Value } from "@holllo/webextension-storage"; type State = { previewChecked: "off" | "foreground" | "background"; @@ -31,7 +31,10 @@ export class UsernameColorsSetting extends Component { } async componentDidMount() { - this.setState({usernameColors: await fromStorage(Feature.UsernameColors), randomizeChecked: await fromStorage(Data.RandomizeUsernameColors)}); + this.setState({ + randomizeChecked: await fromStorage(Data.RandomizeUsernameColors), + usernameColors: await fromStorage(Feature.UsernameColors), + }); } addNewColor = async () => { @@ -104,12 +107,12 @@ export class UsernameColorsSetting extends Component { this.setState({previewChecked}); }; - toggleRandomized = () => { + toggleRandomized = async () => { const randomizeChecked = this.state.randomizeChecked; randomizeChecked.value = !randomizeChecked.value; - void randomizeChecked.save(); - this.setState({randomizeChecked}) - } + await randomizeChecked.save(); + this.setState({randomizeChecked}); + }; onInput = (event: Event, id: number, key: "color" | "username") => { const colorIndex = this.state.usernameColors.findIndex( @@ -174,7 +177,6 @@ export class UsernameColorsSetting extends Component { ); }); - return (

@@ -183,8 +185,9 @@ export class UsernameColorsSetting extends Component { You can enter multiple usernames separated by a comma if you want them to use the same color.
- If randomize is selected then all usernames will be given a random background color. - This will not override colors you have manually assigned. + If randomize is selected then all usernames will be given a random + background color. This will not override colors you have manually + assigned.

@@ -203,7 +206,7 @@ export class UsernameColorsSetting extends Component {