Fix spoiler content accidentally get leaked in document.title

- Also add quotes
- Add comment to why use 64 chars (soft) limit
This commit is contained in:
Lim Chee Aun 2022-12-15 17:14:33 +08:00
parent 2ddc1b8005
commit 94dd2bf627

View file

@ -111,18 +111,25 @@ export default ({ id }) => {
}, [heroStatus]);
const heroContentText = useMemo(() => {
if (!heroStatus) return '';
const { content } = heroStatus;
const div = document.createElement('div');
div.innerHTML = content;
let text = div.innerText.trim();
const { spoilerText, content } = heroStatus;
let text;
if (spoilerText) {
text = spoilerText;
} else {
const div = document.createElement('div');
div.innerHTML = content;
text = div.innerText.trim();
}
if (text.length > 64) {
// "The title should ideally be less than 64 characters in length"
// https://www.w3.org/Provider/Style/TITLE.html
text = text.slice(0, 64) + '…';
}
return text;
}, [heroStatus]);
useTitle(
heroDisplayName && heroContentText
? `${heroDisplayName}: ${heroContentText}`
? `${heroDisplayName}: "${heroContentText}"`
: 'Status',
);