diff --git a/core.ts b/core.ts index fa5d509..378d302 100644 --- a/core.ts +++ b/core.ts @@ -254,6 +254,7 @@ function _loadLocalizationData() { if (response.result.length > 0) { SCLocalizationTranslating = true; WebLocalizationUpdateReplaceWords(response.result); + window.postMessage({ type: 'TOGGLED-SC-BOX-TRANSLATE', action: 'on' }, '*'); } }); } @@ -283,8 +284,10 @@ window.addEventListener('message', async (event) => { if (action === 'translate') { try { + _saveLocalizationSwitchStater(true) + await new Promise(resolve => setTimeout(resolve, 100)); SCLocalizationEnableSplitMode = true; - chrome.runtime.sendMessage({ action: "_loadLocalizationData", url: "manual" }, function (response) { + chrome.runtime.sendMessage({ action: "_loadLocalizationData", url: window.location.href }, function (response) { SCLocalizationTranslating = true; window.postMessage({ type: 'TOGGLED-SC-BOX-TRANSLATE', action: 'on' }, '*'); WebLocalizationUpdateReplaceWords(response.result); @@ -295,6 +298,7 @@ window.addEventListener('message', async (event) => { } } else if (action === 'undoTranslate') { try { + _saveLocalizationSwitchStater(false) response = await undoTranslate(); } catch (error: any) { response = { success: false, error: error.message }; diff --git a/dist/chrome/content_scripts/content-0.js b/dist/chrome/content_scripts/content-0.js index f28b7c8..34b6caf 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(s){chrome.runtime.sendMessage({action:"_setTranslateSwitch",url:window.location.href,enableManual:s},function(e){console.log("SET translate switch ===",window.location.href,"enableManual === ",s)})}function t(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="保存"),i().then(s=>{})}async function i(){async function s(e,n){if(e.nodeType===Node.TEXT_NODE){let s=e.nodeValue||"",a=o(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 u(s,e){let n,a;if("INPUT"===s.tagName){if(n="button"===s.type||"submit"===s.type?"value":"placeholder",a=o(s[n]),s[n]===a)return;let t=e.getAttribute("data-original-value")||"";s.setAttribute("data-original-value",t+s[n])}else{if(a=o(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 o(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="升级包 "+o(s[0])+" 到 "+o(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,o(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?u(n,e):n.nodeType===Node.ELEMENT_NODE&&("INPUT"===n.tagName?u(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(){i().then(s=>{})},200)}),chrome.runtime.sendMessage({action:"_loadLocalizationData",url:window.location.href},function(s){s.result.length>0&&(n=!0,t(s.result))}),chrome.runtime.onMessage.addListener((s,i,u)=>{if("_toggleTranslation"===s.action){if(n){n=!1,r(),a(!1);return}n=!0,e=!0,window.postMessage({type:"TOGGLED-SC-BOX-TRANSLATE",action:"on"},"*"),t(s.data),a(!0)}}),window.addEventListener("message",async s=>{if(s.source!==window||!s.data||"SC_TRANSLATE_REQUEST"!==s.data.type)return;let{action:a}=s.data;if("translate"===a)try{e=!0,chrome.runtime.sendMessage({action:"_loadLocalizationData",url:"manual"},function(s){n=!0,window.postMessage({type:"TOGGLED-SC-BOX-TRANSLATE",action:"on"},"*"),t(s.result)})}catch(s){s.message}else if("undoTranslate"===a)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.9",n.ruid="bundler=rspack@1.3.9",n(242),n(148)})(); \ No newline at end of file +(()=>{var s={242:function(){let s={},e=!1,n=!1;function a(s){chrome.runtime.sendMessage({action:"_setTranslateSwitch",url:window.location.href,enableManual:s},function(e){console.log("SET translate switch ===",window.location.href,"enableManual === ",s)})}function t(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="保存"),i().then(s=>{})}async function i(){async function s(e,n){if(e.nodeType===Node.TEXT_NODE){let s=e.nodeValue||"",a=o(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 u(s,e){let n,a;if("INPUT"===s.tagName){if(n="button"===s.type||"submit"===s.type?"value":"placeholder",a=o(s[n]),s[n]===a)return;let t=e.getAttribute("data-original-value")||"";s.setAttribute("data-original-value",t+s[n])}else{if(a=o(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 o(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="升级包 "+o(s[0])+" 到 "+o(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,o(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?u(n,e):n.nodeType===Node.ELEMENT_NODE&&("INPUT"===n.tagName?u(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(){i().then(s=>{})},200)}),chrome.runtime.sendMessage({action:"_loadLocalizationData",url:window.location.href},function(s){s.result.length>0&&(n=!0,t(s.result),window.postMessage({type:"TOGGLED-SC-BOX-TRANSLATE",action:"on"},"*"))}),chrome.runtime.onMessage.addListener((s,i,u)=>{if("_toggleTranslation"===s.action){if(n){n=!1,r(),a(!1);return}n=!0,e=!0,window.postMessage({type:"TOGGLED-SC-BOX-TRANSLATE",action:"on"},"*"),t(s.data),a(!0)}}),window.addEventListener("message",async s=>{if(s.source!==window||!s.data||"SC_TRANSLATE_REQUEST"!==s.data.type)return;let{action:i}=s.data;if("translate"===i)try{a(!0),await new Promise(s=>setTimeout(s,100)),e=!0,chrome.runtime.sendMessage({action:"_loadLocalizationData",url:window.location.href},function(s){n=!0,window.postMessage({type:"TOGGLED-SC-BOX-TRANSLATE",action:"on"},"*"),t(s.result)})}catch(s){s.message}else if("undoTranslate"===i)try{a(!1),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.9",n.ruid="bundler=rspack@1.3.9",n(242),n(148)})(); \ No newline at end of file