2023-10-14 20:26:23 +08:00
|
|
|
let SCLocalizationReplaceLocalesMap = {};
|
|
|
|
let SCLocalizationEnableSplitMode = false;
|
2023-10-10 14:25:32 +08:00
|
|
|
|
|
|
|
function InitWebLocalization() {
|
|
|
|
// init script
|
|
|
|
LocalizationWatchUpdate();
|
|
|
|
// load Data
|
|
|
|
_loadLocalizationData();
|
|
|
|
}
|
|
|
|
|
|
|
|
function LocalizationWatchUpdate() {
|
|
|
|
const m = window.MutationObserver || window.WebKitMutationObserver;
|
|
|
|
const observer = new m(function (mutations, observer) {
|
|
|
|
for (let mutationRecord of mutations) {
|
|
|
|
for (let node of mutationRecord.addedNodes) {
|
|
|
|
traverseElement(node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
observer.observe(document.body, {
|
|
|
|
subtree: true,
|
|
|
|
characterData: true,
|
|
|
|
childList: true,
|
|
|
|
});
|
2023-10-14 20:26:23 +08:00
|
|
|
|
|
|
|
if (window.location.href.includes("robertsspaceindustries.com")) {
|
|
|
|
console.log("SCLocalizationEnableSplitMode = true");
|
|
|
|
SCLocalizationEnableSplitMode = true;
|
|
|
|
}
|
|
|
|
|
2023-10-10 14:25:32 +08:00
|
|
|
if (window.location.hostname.includes("www.erkul.games") || window.location.hostname.includes("ccugame.app")) {
|
|
|
|
document.body.addEventListener("click", function (event) {
|
|
|
|
setTimeout(function () {
|
|
|
|
allTranslate().then(_ => {
|
|
|
|
});
|
|
|
|
}, 200);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function WebLocalizationUpdateReplaceWords(w) {
|
|
|
|
let replaceWords = w.sort(function (a, b) {
|
|
|
|
return b.word.length - a.word.length;
|
|
|
|
});
|
2023-10-14 20:26:23 +08:00
|
|
|
replaceWords.forEach(({word, replacement}) => {
|
|
|
|
SCLocalizationReplaceLocalesMap[word] = replacement;
|
2023-10-10 14:25:32 +08:00
|
|
|
});
|
2023-10-15 00:41:55 +08:00
|
|
|
if (window.location.hostname.startsWith("issue-council.robertsspaceindustries.com")) {
|
|
|
|
SCLocalizationReplaceLocalesMap["save"] = "保存";
|
|
|
|
}
|
2023-10-10 14:25:32 +08:00
|
|
|
allTranslate().then(_ => {
|
|
|
|
});
|
|
|
|
// console.log("WebLocalizationUpdateReplaceWords ==" + w)
|
|
|
|
}
|
|
|
|
|
|
|
|
async function allTranslate() {
|
|
|
|
async function replaceTextNode(node1) {
|
|
|
|
if (node1.nodeType === Node.TEXT_NODE) {
|
2023-10-14 20:26:23 +08:00
|
|
|
node1.nodeValue = GetSCLocalizationTranslateString(node1.nodeValue);
|
2023-10-10 14:25:32 +08:00
|
|
|
} else {
|
|
|
|
for (let i = 0; i < node1.childNodes.length; i++) {
|
|
|
|
await replaceTextNode(node1.childNodes[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
await replaceTextNode(document.body);
|
|
|
|
}
|
|
|
|
|
|
|
|
function traverseElement(el) {
|
|
|
|
if (!shouldTranslateEl(el)) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const child of el.childNodes) {
|
|
|
|
if (["RELATIVE-TIME", "TIME-AGO"].includes(el.tagName)) {
|
|
|
|
translateRelativeTimeEl(el);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (child.nodeType === Node.TEXT_NODE) {
|
|
|
|
translateElement(child);
|
|
|
|
} else if (child.nodeType === Node.ELEMENT_NODE) {
|
|
|
|
if (child.tagName === "INPUT") {
|
|
|
|
translateElement(child);
|
|
|
|
} else {
|
|
|
|
traverseElement(child);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// pass
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function translateElement(el) {
|
|
|
|
// Get the text field name
|
|
|
|
let k;
|
|
|
|
if (el.tagName === "INPUT") {
|
|
|
|
if (el.type === 'button' || el.type === 'submit') {
|
|
|
|
k = 'value';
|
|
|
|
} else {
|
|
|
|
k = 'placeholder';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
k = 'data';
|
|
|
|
}
|
2023-10-14 20:26:23 +08:00
|
|
|
el[k] = GetSCLocalizationTranslateString(el[k]);
|
2023-10-10 14:25:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function translateRelativeTimeEl(el) {
|
|
|
|
const lang = (navigator.language || navigator.userLanguage);
|
|
|
|
const datetime = $(el).attr('datetime');
|
|
|
|
$(el).text(timeago.format(datetime, lang.replace('-', '_')));
|
|
|
|
}
|
|
|
|
|
|
|
|
function shouldTranslateEl(el) {
|
|
|
|
const blockIds = [];
|
|
|
|
const blockClass = [
|
|
|
|
"css-truncate" // 过滤文件目录
|
|
|
|
];
|
|
|
|
const blockTags = ["IMG", "svg", "mat-icon"];
|
|
|
|
if (blockTags.includes(el.tagName)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (el.id && blockIds.includes(el.id)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (el.classList) {
|
|
|
|
for (let clazz of blockClass) {
|
|
|
|
if (el.classList.contains(clazz)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-10-14 20:26:23 +08:00
|
|
|
function GetSCLocalizationTranslateString(txtSrc) {
|
|
|
|
const key = txtSrc.toLowerCase().replace(/\xa0/g, ' ').replace(/\s{2,}/g, ' ').trim();
|
|
|
|
const sourceKey = txtSrc.replace(/\xa0/g, ' ').replace(/\s{2,}/g, ' ').trim();
|
|
|
|
let noTheKey = key.replace("the ", "");
|
|
|
|
let noHorizontalKey = key.replace("- ", "");
|
|
|
|
|
|
|
|
if (SCLocalizationReplaceLocalesMap[key]) {
|
|
|
|
txtSrc = SCLocalizationReplaceLocalesMap[key]
|
|
|
|
} else if (SCLocalizationEnableSplitMode) {
|
|
|
|
if (sourceKey.includes(" - ")) {
|
|
|
|
let nodeValue = txtSrc
|
2023-10-14 23:47:15 +08:00
|
|
|
let splitKey = sourceKey.split(" - ");
|
|
|
|
if (splitKey[0].toLowerCase() === "upgrade" && key.includes("to") && key.endsWith("edition")) {
|
|
|
|
// 升级包规则
|
|
|
|
let noVersionStr = key.replace("STANDARD EDITION".toLowerCase(), "").replace("upgrade", "").replace("WARBOND EDITION".toLowerCase(), "")
|
|
|
|
let shipNames = noVersionStr.split(" to ")
|
|
|
|
let finalString = "升级包 " + GetSCLocalizationTranslateString(shipNames[0]) + " 到 " + GetSCLocalizationTranslateString(shipNames[1]);
|
|
|
|
if (key.endsWith("WARBOND EDITION".toLowerCase())) {
|
|
|
|
finalString = finalString + " 战争债券版"
|
2023-10-14 20:26:23 +08:00
|
|
|
} else {
|
2023-10-14 23:47:15 +08:00
|
|
|
finalString = finalString + " 标准版"
|
2023-10-14 20:26:23 +08:00
|
|
|
}
|
2023-10-14 23:47:15 +08:00
|
|
|
txtSrc = finalString
|
|
|
|
} else {
|
|
|
|
// 机库通用规则
|
|
|
|
splitKey.forEach(function (splitKey) {
|
|
|
|
if (SCLocalizationReplaceLocalesMap[splitKey.toLowerCase()]) {
|
|
|
|
nodeValue = nodeValue.replace(splitKey, SCLocalizationReplaceLocalesMap[splitKey.toLowerCase()])
|
|
|
|
} else {
|
|
|
|
nodeValue = nodeValue.replace(splitKey, GetSCLocalizationTranslateString(splitKey))
|
|
|
|
}
|
|
|
|
});
|
|
|
|
txtSrc = nodeValue
|
|
|
|
}
|
2023-10-14 20:36:42 +08:00
|
|
|
} else if (key.endsWith("starter pack") || key.endsWith("starter package")) {
|
2023-10-14 20:26:23 +08:00
|
|
|
let shipName = key.replace("starter package", "").replace("starter pack", "").trim()
|
|
|
|
if (SCLocalizationReplaceLocalesMap[shipName.toLowerCase()]) {
|
|
|
|
shipName = SCLocalizationReplaceLocalesMap[shipName.toLowerCase()];
|
|
|
|
}
|
|
|
|
txtSrc = shipName + " 新手包";
|
|
|
|
} else if (key.startsWith("the ") && SCLocalizationReplaceLocalesMap[noTheKey]) {
|
|
|
|
txtSrc = SCLocalizationReplaceLocalesMap[noTheKey];
|
|
|
|
} else if (key.startsWith("- ") && SCLocalizationReplaceLocalesMap[noHorizontalKey]) {
|
2023-10-14 23:47:15 +08:00
|
|
|
txtSrc = "- " + SCLocalizationReplaceLocalesMap[noHorizontalKey];
|
2023-10-14 20:26:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return txtSrc
|
|
|
|
}
|
|
|
|
|
2023-10-10 14:25:32 +08:00
|
|
|
InitWebLocalization();
|
|
|
|
|
|
|
|
function _loadLocalizationData() {
|
2023-10-14 20:26:23 +08:00
|
|
|
chrome.runtime.sendMessage({action: "_loadLocalizationData", url: window.location.href}, function (response) {
|
2023-10-10 14:25:32 +08:00
|
|
|
WebLocalizationUpdateReplaceWords(response.result);
|
|
|
|
});
|
|
|
|
}
|