71 lines
3.5 KiB
JavaScript
71 lines
3.5 KiB
JavaScript
/**
|
|
* @name Reading Time
|
|
* @description Shows an estimated reading time ("3 min read") on long posts and X Articles, counted from the text on screen at 230 words per minute.
|
|
* @version 1.2.0
|
|
* @author wayBetter
|
|
* @permissions network:none
|
|
*/
|
|
// i18n:begin
|
|
// This plugin's English words. Translations live in the app's catalogs (plugin.<file name>.*,
|
|
// docs/TRANSLATING.md); a plugin of your own can also pass { en: {...}, tr: {...} } to register.
|
|
const MESSAGES = {
|
|
"en": {
|
|
"label": "{minutes} min read"
|
|
}
|
|
};
|
|
// i18n:end
|
|
/** The words in the app's language (api.i18n); English before start() and outside the app. */
|
|
let i18n = null;
|
|
const t = (key, params = {}) => (i18n ? i18n.t(key, params) : String(MESSAGES.en[key] ?? key)
|
|
.replace(/\{(\w+), plural, one \{([^{}]*)\} other \{([^{}]*)\}\}/g, (m, name, one, other) => (Number(params[name]) === 1 ? one : other).replace('#', String(params[name])))
|
|
.replace(/\{(\w+)\}/g, (m, name) => (name in params ? String(params[name]) : m)));
|
|
const WORDS_PER_MINUTE = 230;
|
|
const MIN_WORDS = 120;
|
|
|
|
module.exports = class ReadingTime {
|
|
/** Minutes to read `text`, or 0 when it is too short to be worth a label. CJK characters count as words. */
|
|
static minutes(text) {
|
|
const value = String(text || '');
|
|
const cjk = (value.match(/[-ヿ㐀-鿿가-]/g) || []).length;
|
|
const words = (value.replace(/[-ヿ㐀-鿿가-]/g, ' ').match(/\S+/g) || []).length + cjk;
|
|
return words < MIN_WORDS ? 0 : Math.max(1, Math.round(words / WORDS_PER_MINUTE));
|
|
}
|
|
|
|
start() {
|
|
const api = window.wayBetter.api;
|
|
i18n = api.i18n || null;
|
|
if (i18n) i18n.register(MESSAGES);
|
|
this.scope = api.scope();
|
|
this.scope.css('reading-time', `
|
|
/* X lays posts out in flex columns that stretch their children: the pill keeps its own width. */
|
|
.bt-reading-time { display: inline-block; align-self: flex-start; width: max-content; max-width: 100%; box-sizing: border-box; margin: 0 0 6px; padding: 2px 8px; border-radius: 999px;
|
|
font: 600 12px/1.5 system-ui, sans-serif; background: rgba(29,155,240,.12); color: rgb(29,155,240); }
|
|
`);
|
|
// Long posts. Timelines show a truncated preview; the post page has the full text.
|
|
this.scope.observe('article [data-testid="tweetText"]', text => this.label(text));
|
|
// X Articles (long-form): label the article body once.
|
|
this.scope.observe('[data-testid="twitterArticleRichTextView"], [data-testid="longformRichTextComponent"]', body => this.label(body));
|
|
// A language switch relabels the badges already on screen.
|
|
if (i18n) this.scope.defer(i18n.onChange(() => {
|
|
for (const badge of document.querySelectorAll('.bt-reading-time')) if (badge.nextElementSibling) this.label(badge.nextElementSibling);
|
|
}));
|
|
}
|
|
|
|
stop() {
|
|
this.scope.dispose();
|
|
for (const node of document.querySelectorAll('.bt-reading-time')) node.remove();
|
|
}
|
|
|
|
label(target) {
|
|
const minutes = ReadingTime.minutes(target.textContent);
|
|
const previous = target.previousElementSibling?.classList.contains('bt-reading-time') ? target.previousElementSibling : null;
|
|
if (!minutes) { previous?.remove(); return; }
|
|
const text = t('label', { minutes });
|
|
if (previous) { if (previous.textContent !== text) previous.textContent = text; return; }
|
|
if (!target.parentNode) return;
|
|
const badge = document.createElement('div');
|
|
badge.className = 'bt-reading-time';
|
|
badge.textContent = text;
|
|
target.before(badge);
|
|
}
|
|
};
|