117 lines
6.2 KiB
TypeScript
117 lines
6.2 KiB
TypeScript
// Navigation+ · Focus Mode (was the Focus Mode plugin, 1.2.0). Hides what pulls you away from
|
|
// reading: like/repost/reply/view counts, trends, "Who to follow", follower counts or the whole
|
|
// right column. Alt+Shift+F turns it on and off. It only hides what X already shows, locally.
|
|
//
|
|
// Its settings stay where the plugin kept them (page storage `focus-mode` / `settings`), so an
|
|
// upgrade changes nothing for someone who used the plugin.
|
|
|
|
import type { FeatureModule } from '../../../../../waybetter/packages/runtime/src/featureModules.ts';
|
|
import type { SettingsSchema } from '../../../../../waybetter/packages/runtime/src/settingsForm.ts';
|
|
import { focalViewsParts, sidebarModuleSelectors, type ShapeNode } from '../../inject/features.ts';
|
|
import type { PluginApi } from '../../inject/pluginApi.ts';
|
|
|
|
export type FocusSettings = { enabled: boolean; metrics: boolean; trends: boolean; whoToFollow: boolean; followCounts: boolean; sidebar: boolean };
|
|
|
|
export const FOCUS_SCHEMA: SettingsSchema = [
|
|
{ key: 'enabled', type: 'toggle', default: true, label: 'enabled' },
|
|
{ type: 'divider' },
|
|
{ key: 'metrics', type: 'toggle', default: true, label: 'metrics', hint: 'metricsHint' },
|
|
{ key: 'trends', type: 'toggle', default: true, label: 'trends' },
|
|
{ key: 'whoToFollow', type: 'toggle', default: true, label: 'whoToFollow' },
|
|
{ key: 'followCounts', type: 'toggle', default: false, label: 'followCounts' },
|
|
{ key: 'sidebar', type: 'toggle', default: false, label: 'sidebar', hint: 'sidebarHint' },
|
|
];
|
|
|
|
const VIEWS = 'data-bt-fm-views';
|
|
const VIEWS_SEP = 'data-bt-fm-views-sep';
|
|
const SIDE = '[data-testid="sidebarColumn"]';
|
|
const hide = (selectors: string[]) => `${selectors.join(',\n')} { display: none !important; }`;
|
|
|
|
/** The CSS for `settings` (empty when Focus Mode is off or hides nothing). Pure. */
|
|
export function focusCss(settings: FocusSettings): string {
|
|
if (!settings.enabled) return '';
|
|
const rules: string[] = [];
|
|
if (settings.metrics) {
|
|
// The numbers in each post's action bar (replies, reposts, likes, bookmarks, views); the buttons
|
|
// keep their place, so the bar does not jump.
|
|
rules.push('article[data-testid="tweet"] [role="group"] [data-testid="app-text-transition-container"] { visibility: hidden !important; }');
|
|
// The "· 1,234 Views" unit under an opened post, number, word and separator together (marked
|
|
// below; a views link outside the action bar is hidden even before it is marked).
|
|
rules.push(hide([
|
|
`article[data-testid="tweet"] [${VIEWS}]`,
|
|
`article[data-testid="tweet"] [${VIEWS_SEP}]`,
|
|
'article[data-testid="tweet"] a[href$="/analytics"]:not([role="group"] a)',
|
|
]));
|
|
// Quote / repost / like counts under an opened post: the number goes without leaving a gap; the
|
|
// word stays as the link to who quoted, reposted or liked.
|
|
rules.push(hide(['quotes', 'retweets', 'likes'].map(kind => `article[data-testid="tweet"] a[href$="/${kind}"] [data-testid="app-text-transition-container"]`)));
|
|
}
|
|
if (settings.sidebar) {
|
|
rules.push(`${SIDE} { visibility: hidden !important; }`);
|
|
} else {
|
|
if (settings.trends) {
|
|
rules.push(hide([
|
|
...sidebarModuleSelectors('section[role="region"]', '[data-testid="trend"]'),
|
|
...sidebarModuleSelectors('div[aria-label^="Timeline: Trending"]'),
|
|
`${SIDE} div:has(> [data-testid="trend"])`,
|
|
]));
|
|
}
|
|
if (settings.whoToFollow) rules.push(hide(sidebarModuleSelectors('aside[role="complementary"]', '[data-testid="UserCell"]')));
|
|
}
|
|
if (settings.followCounts) {
|
|
// Follower / following numbers on profiles and hover cards, without a gap; the words stay as
|
|
// the links to the lists. (`/<handle>/followers` links only: an @mention links to `/<handle>`,
|
|
// profile tabs are role="tab".)
|
|
const link = (kind: string) => `a[href$="/${kind}"]:not([href="/${kind}"]):not([role="tab"])`;
|
|
const kinds = ['followers', 'verified_followers', 'following'];
|
|
rules.push(hide(kinds.map(kind => `${link(kind)} > span:first-child`)));
|
|
// X's text keeps white space (pre-wrap): the " " before "Followers" would stay as a gap.
|
|
rules.push(`${kinds.map(link).join(', ')} { white-space: nowrap !important; }`);
|
|
}
|
|
return rules.join('\n');
|
|
}
|
|
|
|
export const focusMode: FeatureModule<PluginApi> = {
|
|
id: 'navigation.focusMode',
|
|
// The plugin's words and storage, kept: nothing is retranslated and no setting is lost.
|
|
words: 'focus-mode',
|
|
storage: 'focus-mode',
|
|
settings: FOCUS_SCHEMA,
|
|
start(ctx) {
|
|
const doc = ctx.win.document;
|
|
const current = () => ctx.settings.get() as FocusSettings;
|
|
const apply = () => ctx.css(focusCss(current()));
|
|
apply();
|
|
ctx.settings.onChange(apply);
|
|
|
|
// The views unit of an opened post (focal posts print "time · date" in one link). Re-marked when
|
|
// X re-renders the count (a new node) as well as when the date appears.
|
|
const markViews = (time: Element) => {
|
|
if (!(time.textContent ?? '').includes('·')) return;
|
|
const parts = focalViewsParts(time as unknown as ShapeNode);
|
|
if (!parts) return;
|
|
for (const el of parts.unit) (el as unknown as Element).setAttribute(VIEWS, '');
|
|
for (const el of parts.separators) (el as unknown as Element).setAttribute(VIEWS_SEP, '');
|
|
};
|
|
ctx.observe('article[data-testid="tweet"] a time, article[data-testid="tweet"] [data-testid="app-text-transition-container"]', node => {
|
|
const article = node.closest('article');
|
|
if (!article) return;
|
|
for (const time of article.querySelectorAll('a time')) markViews(time);
|
|
});
|
|
ctx.defer(() => {
|
|
for (const el of doc.querySelectorAll(`[${VIEWS}], [${VIEWS_SEP}]`)) {
|
|
el.removeAttribute(VIEWS);
|
|
el.removeAttribute(VIEWS_SEP);
|
|
}
|
|
});
|
|
|
|
// One toast for the latest state, not a stack of on/off/on.
|
|
let notice: { close(): void } | null = null;
|
|
const toggle = () => {
|
|
ctx.settings.set({ enabled: !current().enabled });
|
|
notice?.close();
|
|
notice = ctx.ui.toast(current().enabled ? ctx.text('on') : ctx.text('off'), { action: { label: ctx.text('settings'), onClick: () => ctx.settings.open() } });
|
|
};
|
|
ctx.command({ id: 'focusMode.toggle', title: ctx.text('toggle'), keywords: 'focus distraction hide counts trends sidebar alt shift f', shortcut: 'Alt+Shift+F', run: toggle });
|
|
},
|
|
};
|