xkeyC 2024-01-30 21:53:49 +08:00
parent 27dbf46324
commit 7ab69ab30a
2 changed files with 40 additions and 30 deletions

View File

@ -1,14 +1,15 @@
let dataVersion = null let dataVersion = null
chrome.runtime.onInstalled.addListener(function () { chrome.runtime.onInstalled.addListener(function () {
_checkVersion().then(_ => { }); _checkVersion().then(_ => {
});
console.log("SWTT init"); console.log("SWTT init");
}); });
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) { chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
if (request.action === "_loadLocalizationData") { if (request.action === "_loadLocalizationData") {
_initLocalization(request.url).then(data => { _initLocalization(request.url).then(data => {
sendResponse({ result: data }); sendResponse({result: data});
}); });
return true; return true;
} }
@ -31,15 +32,15 @@ async function _initLocalization(url) {
let data = {}; let data = {};
if (url.includes("robertsspaceindustries.com")) { if (url.includes("robertsspaceindustries.com")) {
data["zh-CN"] = await _getJsonData("zh-CN-rsi.json", { cacheKey: "zh-CN", version: v.rsi }); data["zh-CN"] = await _getJsonData("zh-CN-rsi.json", {cacheKey: "zh-CN", version: v.rsi});
data["concierge"] = await _getJsonData("concierge.json", { cacheKey: "concierge", version: v.concierge }); data["concierge"] = await _getJsonData("concierge.json", {cacheKey: "concierge", version: v.concierge});
data["orgs"] = await _getJsonData("orgs.json", v.orgs); data["orgs"] = await _getJsonData("orgs.json", v.orgs);
data["address"] = await _getJsonData("addresses.json", { cacheKey: "orgs", version: v.addresses }); data["address"] = await _getJsonData("addresses.json", {cacheKey: "orgs", version: v.addresses});
data["hangar"] = await _getJsonData("hangar.json", { cacheKey: "hangar", version: v.hangar }); data["hangar"] = await _getJsonData("hangar.json", {cacheKey: "hangar", version: v.hangar});
} else if (url.includes("uexcorp.space")) { } else if (url.includes("uexcorp.space")) {
data["UEX"] = await _getJsonData("zh-CN-uex.json", { cacheKey: "uex", version: v.uex }); data["UEX"] = await _getJsonData("zh-CN-uex.json", {cacheKey: "uex", version: v.uex});
} else if (url.includes("erkul.games")) { } else if (url.includes("erkul.games")) {
data["DPS"] = await _getJsonData("zh-CN-dps.json", { cacheKey: "dps", version: v.dps }); data["DPS"] = await _getJsonData("zh-CN-dps.json", {cacheKey: "dps", version: v.dps});
} }
// update data // update data
let replaceWords = []; let replaceWords = [];
@ -56,9 +57,13 @@ async function _initLocalization(url) {
const referral = "https://robertsspaceindustries.com/account/referral-program"; const referral = "https://robertsspaceindustries.com/account/referral-program";
const address = "https://robertsspaceindustries.com/account/addresses"; const address = "https://robertsspaceindustries.com/account/addresses";
const hangar = "https://robertsspaceindustries.com/account/pledges"; const hangar = "https://robertsspaceindustries.com/account/pledges";
const spectrum = "https://robertsspaceindustries.com/spectrum/community/";
if (url.startsWith(spectrum)) {
return;
}
addLocalizationResource("zh-CN"); addLocalizationResource("zh-CN");
if (url.startsWith(org) || url.startsWith(citizens) || url.startsWith(organization)) { if (url.startsWith(org) || url.startsWith(citizens) || url.startsWith(organization)) {
replaceWords.push({ "word": 'members', "replacement": '名成员' }); replaceWords.push({"word": 'members', "replacement": '名成员'});
addLocalizationResource("orgs"); addLocalizationResource("orgs");
} }
if (url.startsWith(address)) { if (url.startsWith(address)) {
@ -67,9 +72,9 @@ async function _initLocalization(url) {
if (url.startsWith(referral)) { if (url.startsWith(referral)) {
replaceWords.push( replaceWords.push(
{ "word": 'Total recruits: ', "replacement": '总邀请数:' }, {"word": 'Total recruits: ', "replacement": '总邀请数:'},
{ "word": 'Prospects ', "replacement": '未完成的邀请' }, {"word": 'Prospects ', "replacement": '未完成的邀请'},
{ "word": 'Recruits', "replacement": '已完成的邀请' } {"word": 'Recruits', "replacement": '已完成的邀请'}
); );
} }
@ -101,13 +106,13 @@ function getLocalizationResource(localizationResource, key) {
.toLowerCase() .toLowerCase()
.replace(/\xa0/g, ' ') .replace(/\xa0/g, ' ')
.replace(/\s{2,}/g, ' '); .replace(/\s{2,}/g, ' ');
localizations.push({ "word": trimmedKey, "replacement": v.toString() }); localizations.push({"word": trimmedKey, "replacement": v.toString()});
} }
} }
return localizations; return localizations;
} }
async function _getJsonData(fileName, { cacheKey = "", version = null } = {}) { async function _getJsonData(fileName, {cacheKey = "", version = null} = {}) {
url = "https://git.sctoolbox.sccsgo.com/SCToolBox/ScWeb_Chinese_Translate/raw/branch/main/json/locales/" + fileName; url = "https://git.sctoolbox.sccsgo.com/SCToolBox/ScWeb_Chinese_Translate/raw/branch/main/json/locales/" + fileName;
if (cacheKey && cacheKey !== "") { if (cacheKey && cacheKey !== "") {
const localVersion = await getLocalData(`${cacheKey}_version`); const localVersion = await getLocalData(`${cacheKey}_version`);
@ -117,7 +122,7 @@ async function _getJsonData(fileName, { cacheKey = "", version = null } = {}) {
} }
} }
const startTime = new Date(); const startTime = new Date();
const response = await fetch(url, { method: 'GET', mode: 'cors' }); const response = await fetch(url, {method: 'GET', mode: 'cors'});
const endTime = new Date(); const endTime = new Date();
const data = await response.json(); const data = await response.json();
if (cacheKey && cacheKey !== "") { if (cacheKey && cacheKey !== "") {

View File

@ -1,14 +1,15 @@
let dataVersion = null let dataVersion = null
chrome.runtime.onInstalled.addListener(function () { chrome.runtime.onInstalled.addListener(function () {
_checkVersion().then(_ => { }); _checkVersion().then(_ => {
});
console.log("SWTT init"); console.log("SWTT init");
}); });
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) { chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
if (request.action === "_loadLocalizationData") { if (request.action === "_loadLocalizationData") {
_initLocalization(request.url).then(data => { _initLocalization(request.url).then(data => {
sendResponse({ result: data }); sendResponse({result: data});
}); });
return true; return true;
} }
@ -31,15 +32,15 @@ async function _initLocalization(url) {
let data = {}; let data = {};
if (url.includes("robertsspaceindustries.com")) { if (url.includes("robertsspaceindustries.com")) {
data["zh-CN"] = await _getJsonData("zh-CN-rsi.json", { cacheKey: "zh-CN", version: v.rsi }); data["zh-CN"] = await _getJsonData("zh-CN-rsi.json", {cacheKey: "zh-CN", version: v.rsi});
data["concierge"] = await _getJsonData("concierge.json", { cacheKey: "concierge", version: v.concierge }); data["concierge"] = await _getJsonData("concierge.json", {cacheKey: "concierge", version: v.concierge});
data["orgs"] = await _getJsonData("orgs.json", v.orgs); data["orgs"] = await _getJsonData("orgs.json", v.orgs);
data["address"] = await _getJsonData("addresses.json", { cacheKey: "orgs", version: v.addresses }); data["address"] = await _getJsonData("addresses.json", {cacheKey: "orgs", version: v.addresses});
data["hangar"] = await _getJsonData("hangar.json", { cacheKey: "hangar", version: v.hangar }); data["hangar"] = await _getJsonData("hangar.json", {cacheKey: "hangar", version: v.hangar});
} else if (url.includes("uexcorp.space")) { } else if (url.includes("uexcorp.space")) {
data["UEX"] = await _getJsonData("zh-CN-uex.json", { cacheKey: "uex", version: v.uex }); data["UEX"] = await _getJsonData("zh-CN-uex.json", {cacheKey: "uex", version: v.uex});
} else if (url.includes("erkul.games")) { } else if (url.includes("erkul.games")) {
data["DPS"] = await _getJsonData("zh-CN-dps.json", { cacheKey: "dps", version: v.dps }); data["DPS"] = await _getJsonData("zh-CN-dps.json", {cacheKey: "dps", version: v.dps});
} }
// update data // update data
let replaceWords = []; let replaceWords = [];
@ -56,9 +57,13 @@ async function _initLocalization(url) {
const referral = "https://robertsspaceindustries.com/account/referral-program"; const referral = "https://robertsspaceindustries.com/account/referral-program";
const address = "https://robertsspaceindustries.com/account/addresses"; const address = "https://robertsspaceindustries.com/account/addresses";
const hangar = "https://robertsspaceindustries.com/account/pledges"; const hangar = "https://robertsspaceindustries.com/account/pledges";
const spectrum = "https://robertsspaceindustries.com/spectrum/community/";
if (url.startsWith(spectrum)) {
return;
}
addLocalizationResource("zh-CN"); addLocalizationResource("zh-CN");
if (url.startsWith(org) || url.startsWith(citizens) || url.startsWith(organization)) { if (url.startsWith(org) || url.startsWith(citizens) || url.startsWith(organization)) {
replaceWords.push({ "word": 'members', "replacement": '名成员' }); replaceWords.push({"word": 'members', "replacement": '名成员'});
addLocalizationResource("orgs"); addLocalizationResource("orgs");
} }
if (url.startsWith(address)) { if (url.startsWith(address)) {
@ -67,9 +72,9 @@ async function _initLocalization(url) {
if (url.startsWith(referral)) { if (url.startsWith(referral)) {
replaceWords.push( replaceWords.push(
{ "word": 'Total recruits: ', "replacement": '总邀请数:' }, {"word": 'Total recruits: ', "replacement": '总邀请数:'},
{ "word": 'Prospects ', "replacement": '未完成的邀请' }, {"word": 'Prospects ', "replacement": '未完成的邀请'},
{ "word": 'Recruits', "replacement": '已完成的邀请' } {"word": 'Recruits', "replacement": '已完成的邀请'}
); );
} }
@ -101,13 +106,13 @@ function getLocalizationResource(localizationResource, key) {
.toLowerCase() .toLowerCase()
.replace(/\xa0/g, ' ') .replace(/\xa0/g, ' ')
.replace(/\s{2,}/g, ' '); .replace(/\s{2,}/g, ' ');
localizations.push({ "word": trimmedKey, "replacement": v.toString() }); localizations.push({"word": trimmedKey, "replacement": v.toString()});
} }
} }
return localizations; return localizations;
} }
async function _getJsonData(fileName, { cacheKey = "", version = null } = {}) { async function _getJsonData(fileName, {cacheKey = "", version = null} = {}) {
url = "https://git.sctoolbox.sccsgo.com/SCToolBox/ScWeb_Chinese_Translate/raw/branch/main/json/locales/" + fileName; url = "https://git.sctoolbox.sccsgo.com/SCToolBox/ScWeb_Chinese_Translate/raw/branch/main/json/locales/" + fileName;
if (cacheKey && cacheKey !== "") { if (cacheKey && cacheKey !== "") {
const localVersion = await getLocalData(`${cacheKey}_version`); const localVersion = await getLocalData(`${cacheKey}_version`);
@ -117,7 +122,7 @@ async function _getJsonData(fileName, { cacheKey = "", version = null } = {}) {
} }
} }
const startTime = new Date(); const startTime = new Date();
const response = await fetch(url, { method: 'GET', mode: 'cors' }); const response = await fetch(url, {method: 'GET', mode: 'cors'});
const endTime = new Date(); const endTime = new Date();
const data = await response.json(); const data = await response.json();
if (cacheKey && cacheKey !== "") { if (cacheKey && cacheKey !== "") {