wayBetter for X
This commit is contained in:
commit
2f1e35ba20
720 changed files with 147465 additions and 0 deletions
55
apps/desktop/src/areas/reader/fullNumbers.ts
Normal file
55
apps/desktop/src/areas/reader/fullNumbers.ts
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
// Reader+ · Show full numbers. X abbreviates counts ("1.2K") but keeps the exact one in each
|
||||
// action's aria-label ("1234 Likes. Like"); the abbreviated text node is rewritten in place (React
|
||||
// keeps owning it) and X's text is remembered, so turning the feature off puts it back.
|
||||
|
||||
import type { FeatureModule } from '../../../../../waybetter/packages/runtime/src/featureModules.ts';
|
||||
import { formatFullCount, fullCountFromLabel, isCompactCount } from '../../inject/features.ts';
|
||||
import type { PluginApi } from '../../inject/pluginApi.ts';
|
||||
|
||||
const COUNT_ATTR = 'data-bt-count';
|
||||
const TEXT_NODE = 3;
|
||||
|
||||
/**
|
||||
* The exact count an action's label carries, in any X language: the number the label starts with
|
||||
* (English and most languages), else the only number in it (languages that put the word first).
|
||||
* A label with no number, or with several away from its start, gives null: the count stays as X wrote it.
|
||||
*/
|
||||
export function countFromLabel(label: string | null | undefined): number | null {
|
||||
const leading = fullCountFromLabel(label);
|
||||
if (leading != null) return leading;
|
||||
const groups = (label ?? '').match(/\d{1,3}(?:[.,\s ]\d{3})+|\d+/g) ?? [];
|
||||
if (groups.length !== 1) return null;
|
||||
const value = Number(groups[0]!.replace(/\D/g, ''));
|
||||
return Number.isSafeInteger(value) ? value : null;
|
||||
}
|
||||
|
||||
export const fullNumbers: FeatureModule<PluginApi> = {
|
||||
id: 'fullNumbers',
|
||||
start(ctx) {
|
||||
const doc = ctx.win.document;
|
||||
const patch = () => {
|
||||
const locale = doc.documentElement.getAttribute('lang') || undefined;
|
||||
const owners = doc.querySelectorAll('[role="group"] button[data-testid][aria-label], [role="group"] a[href$="/analytics"][aria-label]');
|
||||
for (const owner of owners) {
|
||||
const value = countFromLabel(owner.getAttribute('aria-label'));
|
||||
if (value == null) continue;
|
||||
for (const span of owner.querySelectorAll('span')) {
|
||||
for (const node of Array.from(span.childNodes)) {
|
||||
const text = node.nodeType === TEXT_NODE ? node.nodeValue : null;
|
||||
if (text == null || !isCompactCount(text)) continue;
|
||||
span.setAttribute(COUNT_ATTR, text);
|
||||
node.nodeValue = formatFullCount(value, locale);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
ctx.watch(patch).schedule();
|
||||
ctx.defer(() => {
|
||||
for (const span of doc.querySelectorAll(`[${COUNT_ATTR}]`)) {
|
||||
const original = span.getAttribute(COUNT_ATTR)!;
|
||||
for (const node of Array.from(span.childNodes)) if (node.nodeType === TEXT_NODE) node.nodeValue = original;
|
||||
span.removeAttribute(COUNT_ATTR);
|
||||
}
|
||||
});
|
||||
},
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue