diff --git a/README.md b/README.md index ad32132..d437b2c 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ SC网站翻译项目:[CxJuice/ScWeb_Chinese_Translate](https://github.com/CxJuice/ScWeb_Chinese_Translate) -## 本插件仅供大致浏览使用,不对任何有关本插件产生的问题负责!在涉及账号操作前请注意确认网站的原本内容! +## **本插件仅供大致浏览使用,不对任何有关本插件产生的问题负责!在涉及账号操作前请注意确认网站的原本内容!** ![img](https://github.com/xkeyC/StarCitizenBoxBrowserEx/assets/39891083/9580f52a-13ea-4234-a0d3-b8d06f06dda2) @@ -26,16 +26,31 @@ SC网站翻译项目:[CxJuice/ScWeb_Chinese_Translate](https://github.com/CxJu #### 调用翻译Hook ```tsx - const [translateApiAvailable, setTranslateApiAvailable] = useState(false); + enum SCBoxTranslateStatus { + Available, + Translated, + NotAvailable, + } + + const [translateApiAvailable, setTranslateApiAvailable] = useState(SCBoxTranslateStatus.NotAvailable); useEffect(() => { - // 在插件加载后会向页面发送消息 function handleMessage(event: MessageEvent) { if (event.source !== window) return; + // 在插件加载后会向页面发送消息 if (event.data?.type === 'SC-BOX-TRANSLATE-API-AVAILABLE') { - // 你也可以在此直接执行触发翻译操作 - console.log('translateApiAvailable', event.data); - setTranslateApiAvailable(true); + setTranslateApiAvailable(SCBoxTranslateStatus.Available); + } + // 在翻译状态改变时插件也会向页面发送消息 + if (event.data?.type === 'TOGGLED-SC-BOX-TRANSLATE') { + switch (event.data.action) { + case 'on': + setTranslateApiAvailable(SCBoxTranslateStatus.Translated); + return; + case 'off': + setTranslateApiAvailable(SCBoxTranslateStatus.Available); + return; + } } } diff --git a/core.ts b/core.ts index 322ac4f..7d2eee6 100644 --- a/core.ts +++ b/core.ts @@ -101,6 +101,8 @@ async function undoTranslate(): Promise<{success: boolean}> { } }); + window.postMessage({ type: 'TOGGLED-SC-BOX-TRANSLATE', action: 'off' }, '*'); + return Promise.resolve({ success: true }); } @@ -255,13 +257,12 @@ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => { if (SCLocalizationTranslating) { SCLocalizationTranslating = false; undoTranslate(); - window.postMessage({ type: 'TOGGLED-SC-BOX-TRANSLATE', action: 'off' }, '*'); return; } SCLocalizationTranslating = true; SCLocalizationEnableSplitMode = true; - WebLocalizationUpdateReplaceWords(request.data); window.postMessage({ type: 'TOGGLED-SC-BOX-TRANSLATE', action: 'on' }, '*'); + WebLocalizationUpdateReplaceWords(request.data); } }); @@ -277,6 +278,7 @@ window.addEventListener('message', async (event) => { SCLocalizationEnableSplitMode = true; chrome.runtime.sendMessage({ action: "_loadLocalizationData", url: "manual" }, function (response) { SCLocalizationTranslating = true; + window.postMessage({ type: 'TOGGLED-SC-BOX-TRANSLATE', action: 'on' }, '*'); WebLocalizationUpdateReplaceWords(response.result); }); response = { success: true }; diff --git a/dist/chrome/content_scripts/content-0.js b/dist/chrome/content_scripts/content-0.js index 03d6980..361623a 100644 --- a/dist/chrome/content_scripts/content-0.js +++ b/dist/chrome/content_scripts/content-0.js @@ -1 +1 @@ -(()=>{var s={242:function(){let s={},e=!1,n=!1;function a(e){e.sort(function(s,e){return e.word.length-s.word.length}).forEach(({word:e,replacement:n})=>{s[e]=n}),window.location.hostname.startsWith("issue-council.robertsspaceindustries.com")&&(s.save="保存"),t().then(s=>{})}async function t(){async function s(e,n){if(e.nodeType===Node.TEXT_NODE){let s=e.nodeValue||"",a=u(s);if(s!==a&&n){let t=n.getAttribute("data-original-value")||"";n.setAttribute("data-original-value",t+s),e.nodeValue=a}}else for(let n=0;n{s.innerText=s.getAttribute("data-original-value")||"",s.removeAttribute("data-original-value")}),document.querySelectorAll('input[type="button"], input[type="submit"], input[type="text"], input[type="password"]').forEach(s=>{s.hasAttribute("data-original-value")&&("button"===s.type||"submit"===s.type?s.value=s.getAttribute("data-original-value")||"":s.placeholder=s.getAttribute("data-original-value")||"",s.removeAttribute("data-original-value"))}),Promise.resolve({success:!0})}function i(s,e){let n,a;if("INPUT"===s.tagName){if(n="button"===s.type||"submit"===s.type?"value":"placeholder",a=u(s[n]),s[n]===a)return;let t=e.getAttribute("data-original-value")||"";s.setAttribute("data-original-value",t+s[n])}else{if(a=u(s[n="data"]),s[n]===a)return;let t=e.getAttribute("data-original-value")||"";e.setAttribute("data-original-value",t+s[n])}s[n]=a}function u(n){let a=n.toLowerCase().replace(/\xa0/g," ").replace(/\s{2,}/g," ").trim(),t=n.replace(/\xa0/g," ").replace(/\s{2,}/g," ").trim(),r=a.replace("the ",""),i=a.replace("- ","");if(s[a])n=s[a];else if(e)if(t.includes(" - ")){let e=n,r=t.split(" - ");if("upgrade"===r[0].toLowerCase()&&a.includes("to")&&a.endsWith("edition")){let s=a.replace("standard edition","").replace("upgrade","").replace("warbond edition","").split(" to "),e="升级包 "+u(s[0])+" 到 "+u(s[1]);a.endsWith("warbond edition")?e+=" 战争债券版":e+=" 标准版",n=e}else r.forEach(function(n){e=s[n.toLowerCase()]?e.replace(n,s[n.toLowerCase()]):e.replace(n,u(n))}),n=e}else if(a.endsWith("starter pack")||a.endsWith("starter package")){let e=a.replace("starter package","").replace("starter pack","").trim();s[e.toLowerCase()]&&(e=s[e.toLowerCase()]),n=e+" 新手包"}else a.startsWith("the ")&&s[r]?n=s[r]:a.startsWith("- ")&&s[i]&&(n="- "+s[i]);return n}new(window.MutationObserver||window.WebKitMutationObserver)(function(s,e){for(let e of s)for(let s of e.addedNodes)!function s(e){if(n&&function(s){if(["IMG","svg","mat-icon"].includes(s.tagName)||s.id&&[].includes(s.id))return!1;if(s.classList){for(let e of["css-truncate"])if(s.classList.contains(e))return!1}return!0}(e))for(let n of e.childNodes){if(["RELATIVE-TIME","TIME-AGO"].includes(e.tagName))return void function(s){let e=navigator.language||navigator.language,n=$(s).attr("datetime");$(s).text(timeago.format(n,e.replace("-","_")))}(e);n.nodeType===Node.TEXT_NODE?i(n,e):n.nodeType===Node.ELEMENT_NODE&&("INPUT"===n.tagName?i(n,e):s(n))}}(s)}).observe(document.body,{subtree:!0,characterData:!0,childList:!0}),window.location.href.includes("robertsspaceindustries.com")&&(e=!0),window.location.hostname.includes("www.erkul.games")&&document.body.addEventListener("click",function(s){setTimeout(function(){t().then(s=>{})},200)}),chrome.runtime.sendMessage({action:"_loadLocalizationData",url:window.location.href},function(s){console.log("response ==="+JSON.stringify(s)),s.result.length>0&&(n=!0,a(s.result))}),chrome.runtime.onMessage.addListener((s,t,i)=>{if("_toggleTranslation"===s.action){if(n){n=!1,r(),window.postMessage({type:"TOGGLED-SC-BOX-TRANSLATE",action:"off"},"*");return}n=!0,e=!0,a(s.data),window.postMessage({type:"TOGGLED-SC-BOX-TRANSLATE",action:"on"},"*")}}),window.addEventListener("message",async s=>{if(s.source!==window||!s.data||"SC_TRANSLATE_REQUEST"!==s.data.type)return;let{action:t}=s.data;if("translate"===t)try{e=!0,chrome.runtime.sendMessage({action:"_loadLocalizationData",url:"manual"},function(s){n=!0,a(s.result)})}catch(s){s.message}else if("undoTranslate"===t)try{await r()}catch(s){s.message}}),window.postMessage({type:"SC-BOX-TRANSLATE-API-AVAILABLE"},"*")},148:function(s,e){(function(s){"use strict";var e=["second","minute","hour","day","week","month","year"];function n(s,n){if(0===n)return["just now","right now"];var a=e[Math.floor(n/2)];return 1=u[t]&&t=u[n]&&n"1.3.8",n.ruid="bundler=rspack@1.3.8",n(242),n(148)})(); \ No newline at end of file +(()=>{var s={242:function(){let s={},e=!1,n=!1;function a(e){e.sort(function(s,e){return e.word.length-s.word.length}).forEach(({word:e,replacement:n})=>{s[e]=n}),window.location.hostname.startsWith("issue-council.robertsspaceindustries.com")&&(s.save="保存"),t().then(s=>{})}async function t(){async function s(e,n){if(e.nodeType===Node.TEXT_NODE){let s=e.nodeValue||"",a=u(s);if(s!==a&&n){let t=n.getAttribute("data-original-value")||"";n.setAttribute("data-original-value",t+s),e.nodeValue=a}}else for(let n=0;n{s.innerText=s.getAttribute("data-original-value")||"",s.removeAttribute("data-original-value")}),document.querySelectorAll('input[type="button"], input[type="submit"], input[type="text"], input[type="password"]').forEach(s=>{s.hasAttribute("data-original-value")&&("button"===s.type||"submit"===s.type?s.value=s.getAttribute("data-original-value")||"":s.placeholder=s.getAttribute("data-original-value")||"",s.removeAttribute("data-original-value"))}),window.postMessage({type:"TOGGLED-SC-BOX-TRANSLATE",action:"off"},"*"),Promise.resolve({success:!0})}function r(s,e){let n,a;if("INPUT"===s.tagName){if(n="button"===s.type||"submit"===s.type?"value":"placeholder",a=u(s[n]),s[n]===a)return;let t=e.getAttribute("data-original-value")||"";s.setAttribute("data-original-value",t+s[n])}else{if(a=u(s[n="data"]),s[n]===a)return;let t=e.getAttribute("data-original-value")||"";e.setAttribute("data-original-value",t+s[n])}s[n]=a}function u(n){let a=n.toLowerCase().replace(/\xa0/g," ").replace(/\s{2,}/g," ").trim(),t=n.replace(/\xa0/g," ").replace(/\s{2,}/g," ").trim(),i=a.replace("the ",""),r=a.replace("- ","");if(s[a])n=s[a];else if(e)if(t.includes(" - ")){let e=n,i=t.split(" - ");if("upgrade"===i[0].toLowerCase()&&a.includes("to")&&a.endsWith("edition")){let s=a.replace("standard edition","").replace("upgrade","").replace("warbond edition","").split(" to "),e="升级包 "+u(s[0])+" 到 "+u(s[1]);a.endsWith("warbond edition")?e+=" 战争债券版":e+=" 标准版",n=e}else i.forEach(function(n){e=s[n.toLowerCase()]?e.replace(n,s[n.toLowerCase()]):e.replace(n,u(n))}),n=e}else if(a.endsWith("starter pack")||a.endsWith("starter package")){let e=a.replace("starter package","").replace("starter pack","").trim();s[e.toLowerCase()]&&(e=s[e.toLowerCase()]),n=e+" 新手包"}else a.startsWith("the ")&&s[i]?n=s[i]:a.startsWith("- ")&&s[r]&&(n="- "+s[r]);return n}new(window.MutationObserver||window.WebKitMutationObserver)(function(s,e){for(let e of s)for(let s of e.addedNodes)!function s(e){if(n&&function(s){if(["IMG","svg","mat-icon"].includes(s.tagName)||s.id&&[].includes(s.id))return!1;if(s.classList){for(let e of["css-truncate"])if(s.classList.contains(e))return!1}return!0}(e))for(let n of e.childNodes){if(["RELATIVE-TIME","TIME-AGO"].includes(e.tagName))return void function(s){let e=navigator.language||navigator.language,n=$(s).attr("datetime");$(s).text(timeago.format(n,e.replace("-","_")))}(e);n.nodeType===Node.TEXT_NODE?r(n,e):n.nodeType===Node.ELEMENT_NODE&&("INPUT"===n.tagName?r(n,e):s(n))}}(s)}).observe(document.body,{subtree:!0,characterData:!0,childList:!0}),window.location.href.includes("robertsspaceindustries.com")&&(e=!0),window.location.hostname.includes("www.erkul.games")&&document.body.addEventListener("click",function(s){setTimeout(function(){t().then(s=>{})},200)}),chrome.runtime.sendMessage({action:"_loadLocalizationData",url:window.location.href},function(s){console.log("response ==="+JSON.stringify(s)),s.result.length>0&&(n=!0,a(s.result))}),chrome.runtime.onMessage.addListener((s,t,r)=>{if("_toggleTranslation"===s.action){if(n){n=!1,i();return}n=!0,e=!0,window.postMessage({type:"TOGGLED-SC-BOX-TRANSLATE",action:"on"},"*"),a(s.data)}}),window.addEventListener("message",async s=>{if(s.source!==window||!s.data||"SC_TRANSLATE_REQUEST"!==s.data.type)return;let{action:t}=s.data;if("translate"===t)try{e=!0,chrome.runtime.sendMessage({action:"_loadLocalizationData",url:"manual"},function(s){n=!0,window.postMessage({type:"TOGGLED-SC-BOX-TRANSLATE",action:"on"},"*"),a(s.result)})}catch(s){s.message}else if("undoTranslate"===t)try{await i()}catch(s){s.message}}),window.postMessage({type:"SC-BOX-TRANSLATE-API-AVAILABLE"},"*")},148:function(s,e){(function(s){"use strict";var e=["second","minute","hour","day","week","month","year"];function n(s,n){if(0===n)return["just now","right now"];var a=e[Math.floor(n/2)];return 1=u[t]&&t=u[n]&&n"1.3.8",n.ruid="bundler=rspack@1.3.8",n(242),n(148)})(); \ No newline at end of file