mirror of
https://github.com/the1812/Bilibili-Evolved.git
synced 2025-11-04 21:22:45 +08:00
78 lines
2.4 KiB
JavaScript
78 lines
2.4 KiB
JavaScript
(async () =>
|
|
{
|
|
const languageCodeMap = {
|
|
"日本語": "ja-JP",
|
|
"English": "en-US",
|
|
};
|
|
const { map } = await import(`./i18n.${languageCodeMap[settings.i18nLanguage]}`);
|
|
// https://stackoverflow.com/questions/10730309/find-all-text-nodes-in-html-page
|
|
const getAllTextNodes = rootElement =>
|
|
{
|
|
const nodes = [];
|
|
const walker = document.createTreeWalker(rootElement, NodeFilter.SHOW_TEXT, null, false);
|
|
let node = walker.nextNode();
|
|
while (node)
|
|
{
|
|
nodes.push(node);
|
|
node = walker.nextNode();
|
|
}
|
|
return nodes;
|
|
};
|
|
const translateTextNode = textNode =>
|
|
{
|
|
const translation = map.get(textNode.nodeValue.trim());
|
|
if (translation !== undefined)
|
|
{
|
|
if (typeof translation === "string")
|
|
{
|
|
textNode.nodeValue = translation;
|
|
}
|
|
else if (Array.isArray(translation))
|
|
{
|
|
let finalTranslation = null;
|
|
for (const subTranslation of translation)
|
|
{
|
|
if (typeof subTranslation === "string")
|
|
{
|
|
finalTranslation = subTranslation;
|
|
}
|
|
else
|
|
{
|
|
const { text, selector } = subTranslation;
|
|
if (textNode.parentElement.matches(selector))
|
|
{
|
|
finalTranslation = text;
|
|
}
|
|
}
|
|
}
|
|
if (finalTranslation !== null)
|
|
{
|
|
textNode.nodeValue = finalTranslation;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
const { text, selector } = translation;
|
|
if (textNode.parentElement.matches(selector))
|
|
{
|
|
textNode.nodeValue = text;
|
|
}
|
|
}
|
|
}
|
|
};
|
|
getAllTextNodes(document.body).forEach(translateTextNode);
|
|
Observer.childListSubtree("body", records =>
|
|
{
|
|
records.forEach(it => [...it.addedNodes].forEach(node =>
|
|
{
|
|
if (node.nodeType === Node.TEXT_NODE)
|
|
{
|
|
translateTextNode(node);
|
|
}
|
|
else
|
|
{
|
|
getAllTextNodes(node).forEach(translateTextNode);
|
|
}
|
|
}));
|
|
});
|
|
})(); |