mirror of
https://ghfast.top/https://github.com/StarCitizenToolBox/StarCitizenBoxBrowserEx.git
synced 2025-05-09 21:51:25 +08:00
feat: add manual URL handling for localization
This commit is contained in:
parent
13e0e78e48
commit
78b138f039
@ -47,7 +47,7 @@ async function _initLocalization(url: string): Promise<ReplaceWord[]> {
|
||||
// TODO check version
|
||||
let data: Record<string, any> = {};
|
||||
|
||||
if (url.includes("robertsspaceindustries.com") || url.includes("manual")) {
|
||||
if (url.includes("robertsspaceindustries.com")) {
|
||||
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["orgs"] = await _getJsonData("orgs.json", {cacheKey: "orgs", version: v.orgs});
|
||||
@ -57,6 +57,14 @@ async function _initLocalization(url: string): Promise<ReplaceWord[]> {
|
||||
data["UEX"] = await _getJsonData("zh-CN-uex.json", {cacheKey: "uex", version: v.uex});
|
||||
} else if (url.includes("erkul.games")) {
|
||||
data["DPS"] = await _getJsonData("zh-CN-dps.json", {cacheKey: "dps", version: v.dps});
|
||||
} else if (url.includes("manual")) {
|
||||
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["orgs"] = await _getJsonData("orgs.json", {cacheKey: "orgs", version: v.orgs});
|
||||
data["address"] = await _getJsonData("addresses.json", {cacheKey: "orgs", version: v.addresses});
|
||||
data["hangar"] = await _getJsonData("hangar.json", {cacheKey: "hangar", version: v.hangar});
|
||||
data["UEX"] = await _getJsonData("zh-CN-uex.json", {cacheKey: "uex", version: v.uex});
|
||||
data["DPS"] = await _getJsonData("zh-CN-dps.json", {cacheKey: "dps", version: v.dps});
|
||||
}
|
||||
// update data
|
||||
let replaceWords: ReplaceWord[] = [];
|
||||
@ -65,7 +73,7 @@ async function _initLocalization(url: string): Promise<ReplaceWord[]> {
|
||||
replaceWords.push(...getLocalizationResource(data, key));
|
||||
}
|
||||
|
||||
if (url.includes("robertsspaceindustries.com") || url.includes("manual")) {
|
||||
if (url.includes("robertsspaceindustries.com")) {
|
||||
const org = "https://robertsspaceindustries.com/orgs";
|
||||
const citizens = "https://robertsspaceindustries.com/citizens";
|
||||
const organization = "https://robertsspaceindustries.com/account/organization";
|
||||
@ -106,6 +114,20 @@ async function _initLocalization(url: string): Promise<ReplaceWord[]> {
|
||||
addLocalizationResource("UEX");
|
||||
} else if (url.includes("erkul.games")) {
|
||||
addLocalizationResource("DPS");
|
||||
} else if (url.includes("manual")) {
|
||||
addLocalizationResource("zh-CN");
|
||||
replaceWords.push({"word": 'members', "replacement": '名成员'});
|
||||
addLocalizationResource("orgs");
|
||||
addLocalizationResource("address");
|
||||
replaceWords.push(
|
||||
{"word": 'Total recruits: ', "replacement": '总邀请数:'},
|
||||
{"word": 'Prospects ', "replacement": '未完成的邀请'},
|
||||
{"word": 'Recruits', "replacement": '已完成的邀请'}
|
||||
);
|
||||
addLocalizationResource("concierge");
|
||||
addLocalizationResource("hangar");
|
||||
addLocalizationResource("UEX");
|
||||
addLocalizationResource("DPS");
|
||||
}
|
||||
return replaceWords;
|
||||
}
|
||||
|
8
core.ts
8
core.ts
@ -57,8 +57,6 @@ function WebLocalizationUpdateReplaceWords(w) {
|
||||
}
|
||||
|
||||
async function allTranslate() {
|
||||
SCLocalizationTranslating = true;
|
||||
|
||||
async function replaceTextNode(node: Node, parentNode?: Element) {
|
||||
if (node.nodeType === Node.TEXT_NODE) {
|
||||
// 保存原始文本内容
|
||||
@ -244,7 +242,11 @@ InitWebLocalization();
|
||||
|
||||
function _loadLocalizationData() {
|
||||
chrome.runtime.sendMessage({ action: "_loadLocalizationData", url: window.location.href }, function (response) {
|
||||
console.log("response ===" + JSON.stringify(response));
|
||||
if (response.result.length > 0) {
|
||||
SCLocalizationTranslating = true;
|
||||
WebLocalizationUpdateReplaceWords(response.result);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@ -255,6 +257,7 @@ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
|
||||
undoTranslate();
|
||||
return;
|
||||
}
|
||||
SCLocalizationTranslating = true;
|
||||
SCLocalizationEnableSplitMode = true;
|
||||
WebLocalizationUpdateReplaceWords(request.data);
|
||||
}
|
||||
@ -271,6 +274,7 @@ window.addEventListener('message', async (event) => {
|
||||
try {
|
||||
SCLocalizationEnableSplitMode = true;
|
||||
chrome.runtime.sendMessage({ action: "_loadLocalizationData", url: "manual" }, function (response) {
|
||||
SCLocalizationTranslating = true;
|
||||
WebLocalizationUpdateReplaceWords(response.result);
|
||||
});
|
||||
response = { success: true };
|
||||
|
2
dist/chrome/background/service_worker.js
vendored
2
dist/chrome/background/service_worker.js
vendored
@ -1 +1 @@
|
||||
(()=>{var e={},t={};function s(r){var n=t[r];if(void 0!==n)return n.exports;var o=t[r]={exports:{}};return e[r](o,o.exports,s),o.exports}s.rv=()=>"1.3.8",s.ruid="bundler=rspack@1.3.8";let r=null;async function n(){r=await a("versions.json"),console.log("Localization Version ==="),console.log(r)}async function o(e){if(console.log("url ==="+e),null==r)return await n(),o(e);let t=r,s={};e.includes("robertsspaceindustries.com")||e.includes("manual")?(s["zh-CN"]=await a("zh-CN-rsi.json",{cacheKey:"zh-CN",version:t.rsi}),s.concierge=await a("concierge.json",{cacheKey:"concierge",version:t.concierge}),s.orgs=await a("orgs.json",{cacheKey:"orgs",version:t.orgs}),s.address=await a("addresses.json",{cacheKey:"orgs",version:t.addresses}),s.hangar=await a("hangar.json",{cacheKey:"hangar",version:t.hangar})):e.includes("uexcorp.space")?s.UEX=await a("zh-CN-uex.json",{cacheKey:"uex",version:t.uex}):e.includes("erkul.games")&&(s.DPS=await a("zh-CN-dps.json",{cacheKey:"dps",version:t.dps}));let c=[];function i(e){c.push(...function(e,t){let s=[],r=e[t];if("object"==typeof r)for(let[e,t]of Object.entries(r)){let r=e.toString().trim().toLowerCase().replace(/\xa0/g," ").replace(/\s{2,}/g," ");s.push({word:r,replacement:String(t)})}return s}(s,e))}if(e.includes("robertsspaceindustries.com")||e.includes("manual")){if(e.startsWith("https://robertsspaceindustries.com/spectrum/community/"))return[];i("zh-CN"),(e.startsWith("https://robertsspaceindustries.com/orgs")||e.startsWith("https://robertsspaceindustries.com/citizens")||e.startsWith("https://robertsspaceindustries.com/account/organization"))&&(c.push({word:"members",replacement:"名成员"}),i("orgs")),e.startsWith("https://robertsspaceindustries.com/account/addresses")&&i("address"),e.startsWith("https://robertsspaceindustries.com/account/referral-program")&&c.push({word:"Total recruits: ",replacement:"总邀请数:"},{word:"Prospects ",replacement:"未完成的邀请"},{word:"Recruits",replacement:"已完成的邀请"}),e.startsWith("https://robertsspaceindustries.com/account/concierge")&&(c=[],i("concierge")),e.startsWith("https://robertsspaceindustries.com/account/pledges")&&i("hangar")}else e.includes("uexcorp.space")?i("UEX"):e.includes("erkul.games")&&i("DPS");return c}async function a(e,t={}){let{cacheKey:s="",version:r=null}=t;if(s&&""!==s){let e=await c(`${s}_version`),t=await c(s);if(t&&"object"==typeof t&&Object.keys(t).length>0&&e===r)return t}let n=new Date,o=await fetch("https://git.scbox.xkeyc.cn/SCToolBox/ScWeb_Chinese_Translate/raw/branch/main/json/locales/"+e,{method:"GET",mode:"cors"}),l=new Date,u=await o.json();if(s&&""!==s){let e=l.getTime()-n.getTime();console.log(`update ${s} v == ${r} time == ${e/1e3}s`),await i(s,u),await i(`${s}_version`,r)}return u}function c(e){return new Promise(t=>{chrome.storage.local.get([e],s=>{t(s[e]||null)})})}function i(e,t){return new Promise(s=>{let r={};r[e]=t,chrome.storage.local.set(r,()=>{s()})})}chrome.runtime.onInstalled.addListener(function(){n().then(e=>{}),console.log("SWTT init")}),chrome.runtime.onMessage.addListener(function(e,t,s){if("_loadLocalizationData"===e.action)return o(e.url).then(e=>{s({result:e})}),!0}),chrome.runtime.onInstalled.addListener(function(){chrome.contextMenus.create({id:"translate",title:"切换翻译",contexts:["all"]})}),chrome.contextMenus.onClicked.addListener((e,t)=>{console.log("contextMenus",e,t);let s="manual";t&&t.url&&["robertsspaceindustries.com","erkul.games","uexcorp.space"].find(e=>t.url.includes(e))&&(s=t.url),o(s).then(e=>{t&&void 0!==t.id&&chrome.tabs.sendMessage(t.id,{action:"_toggleTranslation",data:e})})})})();
|
||||
(()=>{var e={},s={};function r(t){var n=s[t];if(void 0!==n)return n.exports;var o=s[t]={exports:{}};return e[t](o,o.exports,r),o.exports}r.rv=()=>"1.3.8",r.ruid="bundler=rspack@1.3.8";let t=null;async function n(){t=await a("versions.json"),console.log("Localization Version ==="),console.log(t)}async function o(e){if(console.log("url ==="+e),null==t)return await n(),o(e);let s=t,r={};e.includes("robertsspaceindustries.com")?(r["zh-CN"]=await a("zh-CN-rsi.json",{cacheKey:"zh-CN",version:s.rsi}),r.concierge=await a("concierge.json",{cacheKey:"concierge",version:s.concierge}),r.orgs=await a("orgs.json",{cacheKey:"orgs",version:s.orgs}),r.address=await a("addresses.json",{cacheKey:"orgs",version:s.addresses}),r.hangar=await a("hangar.json",{cacheKey:"hangar",version:s.hangar})):e.includes("uexcorp.space")?r.UEX=await a("zh-CN-uex.json",{cacheKey:"uex",version:s.uex}):e.includes("erkul.games")?r.DPS=await a("zh-CN-dps.json",{cacheKey:"dps",version:s.dps}):e.includes("manual")&&(r["zh-CN"]=await a("zh-CN-rsi.json",{cacheKey:"zh-CN",version:s.rsi}),r.concierge=await a("concierge.json",{cacheKey:"concierge",version:s.concierge}),r.orgs=await a("orgs.json",{cacheKey:"orgs",version:s.orgs}),r.address=await a("addresses.json",{cacheKey:"orgs",version:s.addresses}),r.hangar=await a("hangar.json",{cacheKey:"hangar",version:s.hangar}),r.UEX=await a("zh-CN-uex.json",{cacheKey:"uex",version:s.uex}),r.DPS=await a("zh-CN-dps.json",{cacheKey:"dps",version:s.dps}));let c=[];function i(e){c.push(...function(e,s){let r=[],t=e[s];if("object"==typeof t)for(let[e,s]of Object.entries(t)){let t=e.toString().trim().toLowerCase().replace(/\xa0/g," ").replace(/\s{2,}/g," ");r.push({word:t,replacement:String(s)})}return r}(r,e))}if(e.includes("robertsspaceindustries.com")){if(e.startsWith("https://robertsspaceindustries.com/spectrum/community/"))return[];i("zh-CN"),(e.startsWith("https://robertsspaceindustries.com/orgs")||e.startsWith("https://robertsspaceindustries.com/citizens")||e.startsWith("https://robertsspaceindustries.com/account/organization"))&&(c.push({word:"members",replacement:"名成员"}),i("orgs")),e.startsWith("https://robertsspaceindustries.com/account/addresses")&&i("address"),e.startsWith("https://robertsspaceindustries.com/account/referral-program")&&c.push({word:"Total recruits: ",replacement:"总邀请数:"},{word:"Prospects ",replacement:"未完成的邀请"},{word:"Recruits",replacement:"已完成的邀请"}),e.startsWith("https://robertsspaceindustries.com/account/concierge")&&(c=[],i("concierge")),e.startsWith("https://robertsspaceindustries.com/account/pledges")&&i("hangar")}else e.includes("uexcorp.space")?i("UEX"):e.includes("erkul.games")?i("DPS"):e.includes("manual")&&(i("zh-CN"),c.push({word:"members",replacement:"名成员"}),i("orgs"),i("address"),c.push({word:"Total recruits: ",replacement:"总邀请数:"},{word:"Prospects ",replacement:"未完成的邀请"},{word:"Recruits",replacement:"已完成的邀请"}),i("concierge"),i("hangar"),i("UEX"),i("DPS"));return c}async function a(e,s={}){let{cacheKey:r="",version:t=null}=s;if(r&&""!==r){let e=await c(`${r}_version`),s=await c(r);if(s&&"object"==typeof s&&Object.keys(s).length>0&&e===t)return s}let n=new Date,o=await fetch("https://git.scbox.xkeyc.cn/SCToolBox/ScWeb_Chinese_Translate/raw/branch/main/json/locales/"+e,{method:"GET",mode:"cors"}),l=new Date,u=await o.json();if(r&&""!==r){let e=l.getTime()-n.getTime();console.log(`update ${r} v == ${t} time == ${e/1e3}s`),await i(r,u),await i(`${r}_version`,t)}return u}function c(e){return new Promise(s=>{chrome.storage.local.get([e],r=>{s(r[e]||null)})})}function i(e,s){return new Promise(r=>{let t={};t[e]=s,chrome.storage.local.set(t,()=>{r()})})}chrome.runtime.onInstalled.addListener(function(){n().then(e=>{}),console.log("SWTT init")}),chrome.runtime.onMessage.addListener(function(e,s,r){if("_loadLocalizationData"===e.action)return o(e.url).then(e=>{r({result:e})}),!0}),chrome.runtime.onInstalled.addListener(function(){chrome.contextMenus.create({id:"translate",title:"切换翻译",contexts:["all"]})}),chrome.contextMenus.onClicked.addListener((e,s)=>{console.log("contextMenus",e,s);let r="manual";s&&s.url&&["robertsspaceindustries.com","erkul.games","uexcorp.space"].find(e=>s.url.includes(e))&&(r=s.url),o(r).then(e=>{s&&void 0!==s.id&&chrome.tabs.sendMessage(s.id,{action:"_toggleTranslation",data:e})})})})();
|
2
dist/chrome/content_scripts/content-0.js
vendored
2
dist/chrome/content_scripts/content-0.js
vendored
File diff suppressed because one or more lines are too long
Loading…
x
Reference in New Issue
Block a user