diff --git a/source/content-scripts.ts b/source/content-scripts.ts index 4ecfc06..e741322 100644 --- a/source/content-scripts.ts +++ b/source/content-scripts.ts @@ -11,6 +11,7 @@ import { runAnonymizeUsernamesFeature, runHideVotesFeature, runMarkdownToolbarFeature, + runThemedLogoFeature, runUsernameColorsFeature, } from './scripts/exports.js'; import Settings from './settings.js'; @@ -48,6 +49,7 @@ async function initialize() { function startObserver() { observer.observe(document.body, { + attributes: true, childList: true, subtree: true, }); @@ -71,6 +73,12 @@ async function initialize() { }); } + if (settings.features.themedLogo) { + observerFeatures.push(() => { + runThemedLogoFeature(); + }); + } + if (settings.features.usernameColors) { observerFeatures.push(() => { runUsernameColorsFeature(settings); diff --git a/source/options/components/exports.ts b/source/options/components/exports.ts index 93c830e..af54560 100644 --- a/source/options/components/exports.ts +++ b/source/options/components/exports.ts @@ -5,5 +5,6 @@ export {BackToTopSetting} from './back-to-top.js'; export {HideVotesSetting} from './hide-votes.js'; export {JumpToNewCommentSetting} from './jump-to-new-comment.js'; export {MarkdownToolbarSetting} from './markdown-toolbar.js'; +export {ThemedLogoSetting} from './themed-logo.js'; export {UserLabelsSetting} from './user-labels.js'; export {UsernameColorsSetting} from './username-colors.js'; diff --git a/source/options/components/themed-logo.ts b/source/options/components/themed-logo.ts new file mode 100644 index 0000000..ba6380c --- /dev/null +++ b/source/options/components/themed-logo.ts @@ -0,0 +1,14 @@ +import {html} from 'htm/preact'; + +import {Setting, SettingProps} from './index.js'; + +export function ThemedLogoSetting(props: SettingProps): TRXComponent { + return html` + <${Setting} ...${props}> +

+ Replaces the Tildes logo in the site header with a dynamic one that uses + the colors of your chosen Tildes theme. +

+ + `; +} diff --git a/source/options/features.ts b/source/options/features.ts index 7a8a1a7..881f41b 100644 --- a/source/options/features.ts +++ b/source/options/features.ts @@ -7,6 +7,7 @@ import { HideVotesSetting, JumpToNewCommentSetting, MarkdownToolbarSetting, + ThemedLogoSetting, UserLabelsSetting, UsernameColorsSetting, } from './components/exports.js'; @@ -62,6 +63,13 @@ export const features: Feature[] = [ title: 'Markdown Toolbar', component: () => MarkdownToolbarSetting, }, + { + availableSince: new Date('2022-02-27'), + index: 0, + key: 'themedLogo', + title: 'Themed Logo', + component: () => ThemedLogoSetting, + }, { availableSince: new Date('2019-11-10'), index: 0, diff --git a/source/scripts/exports.ts b/source/scripts/exports.ts index bb2d568..8cc7476 100644 --- a/source/scripts/exports.ts +++ b/source/scripts/exports.ts @@ -4,5 +4,6 @@ export * from './back-to-top.js'; export * from './hide-votes.js'; export * from './jump-to-new-comment.js'; export * from './markdown-toolbar.js'; +export * from './themed-logo.js'; export * from './user-labels.js'; export * from './username-colors.js'; diff --git a/source/scripts/themed-logo.ts b/source/scripts/themed-logo.ts new file mode 100644 index 0000000..6afbef0 --- /dev/null +++ b/source/scripts/themed-logo.ts @@ -0,0 +1,38 @@ +import {log, querySelector} from '../utilities/exports.js'; + +export function runThemedLogoFeature() { + themedLogo(); + log('Themed Logo: Initialized.'); +} + +const tildesLogo = ` + + + + + + + + + + + +`; + +function themedLogo() { + let themedLogo = tildesLogo; + for (const customProperty of tildesLogo.match(/var\(--.+\)/g) ?? []) { + let color = window + .getComputedStyle(document.body) + .getPropertyValue(customProperty.slice('var('.length, -1)); + if (color === '') { + color = '#f0f'; + } + + themedLogo = themedLogo.replace(customProperty, color); + } + + const encodedSvg = encodeURIComponent(themedLogo); + const siteHeader = querySelector('.site-header-logo'); + siteHeader.style.backgroundImage = `url("data:image/svg+xml,${encodedSvg}")`; +} diff --git a/source/settings.ts b/source/settings.ts index 83dbb40..dd0cefd 100644 --- a/source/settings.ts +++ b/source/settings.ts @@ -68,6 +68,7 @@ export default class Settings { hideVotes: boolean; jumpToNewComment: boolean; markdownToolbar: boolean; + themedLogo: boolean; userLabels: boolean; usernameColors: boolean; }; @@ -131,6 +132,7 @@ export default class Settings { hideVotes: false, jumpToNewComment: true, markdownToolbar: true, + themedLogo: false, userLabels: true, usernameColors: false, };