优化 机库、新手包 翻译

This commit is contained in:
xkeyC 2023-10-14 20:26:23 +08:00
parent 7b60049d54
commit c75e6e22e3
5 changed files with 98 additions and 43 deletions

3
.gitignore vendored
View File

@ -1 +1,2 @@
.DS_Store .DS_Store
/.idea/

View File

@ -1,4 +1,5 @@
let replaceLocalesMap = {}; let SCLocalizationReplaceLocalesMap = {};
let SCLocalizationEnableSplitMode = false;
function InitWebLocalization() { function InitWebLocalization() {
// init script // init script
@ -22,6 +23,12 @@ function LocalizationWatchUpdate() {
characterData: true, characterData: true,
childList: true, childList: true,
}); });
if (window.location.href.includes("robertsspaceindustries.com")) {
console.log("SCLocalizationEnableSplitMode = true");
SCLocalizationEnableSplitMode = true;
}
if (window.location.hostname.includes("www.erkul.games") || window.location.hostname.includes("ccugame.app")) { if (window.location.hostname.includes("www.erkul.games") || window.location.hostname.includes("ccugame.app")) {
document.body.addEventListener("click", function (event) { document.body.addEventListener("click", function (event) {
setTimeout(function () { setTimeout(function () {
@ -36,8 +43,8 @@ function WebLocalizationUpdateReplaceWords(w) {
let replaceWords = w.sort(function (a, b) { let replaceWords = w.sort(function (a, b) {
return b.word.length - a.word.length; return b.word.length - a.word.length;
}); });
replaceWords.forEach(({ word, replacement }) => { replaceWords.forEach(({word, replacement}) => {
replaceLocalesMap[word] = replacement; SCLocalizationReplaceLocalesMap[word] = replacement;
}); });
allTranslate().then(_ => { allTranslate().then(_ => {
}); });
@ -47,14 +54,7 @@ function WebLocalizationUpdateReplaceWords(w) {
async function allTranslate() { async function allTranslate() {
async function replaceTextNode(node1) { async function replaceTextNode(node1) {
if (node1.nodeType === Node.TEXT_NODE) { if (node1.nodeType === Node.TEXT_NODE) {
let nodeValue = node1.nodeValue; node1.nodeValue = GetSCLocalizationTranslateString(node1.nodeValue);
const key = nodeValue.trim().toLowerCase()
.replace(/\xa0/g, ' ') // replace ' '
.replace(/\s{2,}/g, ' ');
if (replaceLocalesMap[key]) {
nodeValue = replaceLocalesMap[key]
}
node1.nodeValue = nodeValue;
} else { } else {
for (let i = 0; i < node1.childNodes.length; i++) { for (let i = 0; i < node1.childNodes.length; i++) {
await replaceTextNode(node1.childNodes[i]); await replaceTextNode(node1.childNodes[i]);
@ -102,14 +102,7 @@ function translateElement(el) {
} else { } else {
k = 'data'; k = 'data';
} }
el[k] = GetSCLocalizationTranslateString(el[k]);
const txtSrc = el[k].trim();
const key = txtSrc.toLowerCase()
.replace(/\xa0/g, ' ') // replace '&nbsp;'
.replace(/\s{2,}/g, ' ');
if (replaceLocalesMap[key]) {
el[k] = el[k].replace(txtSrc, replaceLocalesMap[key])
}
} }
function translateRelativeTimeEl(el) { function translateRelativeTimeEl(el) {
@ -140,10 +133,44 @@ function shouldTranslateEl(el) {
return true; return true;
} }
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
sourceKey.split(" - ").forEach(function (splitKey) {
if (SCLocalizationReplaceLocalesMap[splitKey.toLowerCase()]) {
nodeValue = nodeValue.replace(splitKey, SCLocalizationReplaceLocalesMap[splitKey.toLowerCase()])
} else {
nodeValue = nodeValue.replace(splitKey, GetSCLocalizationTranslateString(splitKey))
}
});
txtSrc = nodeValue
} else if (key.includes("starter pack") || key.includes("starter package")) {
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]) {
txtSrc = "- "+SCLocalizationReplaceLocalesMap[noHorizontalKey];
}
}
return txtSrc
}
InitWebLocalization(); InitWebLocalization();
function _loadLocalizationData() { function _loadLocalizationData() {
chrome.runtime.sendMessage({ action: "_loadLocalizationData", url: window.location.href }, function (response) { chrome.runtime.sendMessage({action: "_loadLocalizationData", url: window.location.href}, function (response) {
WebLocalizationUpdateReplaceWords(response.result); WebLocalizationUpdateReplaceWords(response.result);
}); });
} }

View File

@ -1,7 +1,7 @@
{ {
"manifest_version": 3, "manifest_version": 3,
"name": "星际公民盒子浏览器拓展", "name": "星际公民盒子浏览器拓展",
"version": "0.0.2", "version": "0.0.3",
"description": "为星际公民网站及工具站提供汉化", "description": "为星际公民网站及工具站提供汉化",
"author": "xkeyC", "author": "xkeyC",
"icons": { "icons": {

View File

@ -1,4 +1,5 @@
let replaceLocalesMap = {}; let SCLocalizationReplaceLocalesMap = {};
let SCLocalizationEnableSplitMode = false;
function InitWebLocalization() { function InitWebLocalization() {
// init script // init script
@ -22,6 +23,12 @@ function LocalizationWatchUpdate() {
characterData: true, characterData: true,
childList: true, childList: true,
}); });
if (window.location.href.includes("robertsspaceindustries.com")) {
console.log("SCLocalizationEnableSplitMode = true");
SCLocalizationEnableSplitMode = true;
}
if (window.location.hostname.includes("www.erkul.games") || window.location.hostname.includes("ccugame.app")) { if (window.location.hostname.includes("www.erkul.games") || window.location.hostname.includes("ccugame.app")) {
document.body.addEventListener("click", function (event) { document.body.addEventListener("click", function (event) {
setTimeout(function () { setTimeout(function () {
@ -36,8 +43,8 @@ function WebLocalizationUpdateReplaceWords(w) {
let replaceWords = w.sort(function (a, b) { let replaceWords = w.sort(function (a, b) {
return b.word.length - a.word.length; return b.word.length - a.word.length;
}); });
replaceWords.forEach(({ word, replacement }) => { replaceWords.forEach(({word, replacement}) => {
replaceLocalesMap[word] = replacement; SCLocalizationReplaceLocalesMap[word] = replacement;
}); });
allTranslate().then(_ => { allTranslate().then(_ => {
}); });
@ -47,14 +54,7 @@ function WebLocalizationUpdateReplaceWords(w) {
async function allTranslate() { async function allTranslate() {
async function replaceTextNode(node1) { async function replaceTextNode(node1) {
if (node1.nodeType === Node.TEXT_NODE) { if (node1.nodeType === Node.TEXT_NODE) {
let nodeValue = node1.nodeValue; node1.nodeValue = GetSCLocalizationTranslateString(node1.nodeValue);
const key = nodeValue.trim().toLowerCase()
.replace(/\xa0/g, ' ') // replace '&nbsp;'
.replace(/\s{2,}/g, ' ');
if (replaceLocalesMap[key]) {
nodeValue = replaceLocalesMap[key]
}
node1.nodeValue = nodeValue;
} else { } else {
for (let i = 0; i < node1.childNodes.length; i++) { for (let i = 0; i < node1.childNodes.length; i++) {
await replaceTextNode(node1.childNodes[i]); await replaceTextNode(node1.childNodes[i]);
@ -102,14 +102,7 @@ function translateElement(el) {
} else { } else {
k = 'data'; k = 'data';
} }
el[k] = GetSCLocalizationTranslateString(el[k]);
const txtSrc = el[k].trim();
const key = txtSrc.toLowerCase()
.replace(/\xa0/g, ' ') // replace '&nbsp;'
.replace(/\s{2,}/g, ' ');
if (replaceLocalesMap[key]) {
el[k] = el[k].replace(txtSrc, replaceLocalesMap[key])
}
} }
function translateRelativeTimeEl(el) { function translateRelativeTimeEl(el) {
@ -140,10 +133,44 @@ function shouldTranslateEl(el) {
return true; return true;
} }
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
sourceKey.split(" - ").forEach(function (splitKey) {
if (SCLocalizationReplaceLocalesMap[splitKey.toLowerCase()]) {
nodeValue = nodeValue.replace(splitKey, SCLocalizationReplaceLocalesMap[splitKey.toLowerCase()])
} else {
nodeValue = nodeValue.replace(splitKey, GetSCLocalizationTranslateString(splitKey))
}
});
txtSrc = nodeValue
} else if (key.includes("starter pack") || key.includes("starter package")) {
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]) {
txtSrc = "- "+SCLocalizationReplaceLocalesMap[noHorizontalKey];
}
}
return txtSrc
}
InitWebLocalization(); InitWebLocalization();
function _loadLocalizationData() { function _loadLocalizationData() {
chrome.runtime.sendMessage({ action: "_loadLocalizationData", url: window.location.href }, function (response) { chrome.runtime.sendMessage({action: "_loadLocalizationData", url: window.location.href}, function (response) {
WebLocalizationUpdateReplaceWords(response.result); WebLocalizationUpdateReplaceWords(response.result);
}); });
} }

View File

@ -1,7 +1,7 @@
{ {
"manifest_version": 2, "manifest_version": 2,
"name": "星际公民盒子浏览器拓展", "name": "星际公民盒子浏览器拓展",
"version": "0.0.2", "version": "0.0.3",
"description": "为星际公民网站及工具站提供汉化", "description": "为星际公民网站及工具站提供汉化",
"author": "xkeyC", "author": "xkeyC",
"icons": { "icons": {