diff --git a/core.js b/core.js index c6c2ee0..cf474a5 100644 --- a/core.js +++ b/core.js @@ -64,7 +64,8 @@ async function allTranslate() { // 只有当文本发生变化时才保存原始文本 if (originalText !== translatedText) { - parentNode.setAttribute('data-original-value', originalText); + const originalValue = parentNode.getAttribute('data-original-value') || ""; + parentNode.setAttribute('data-original-value', originalValue + originalText); node.nodeValue = translatedText; } } else { @@ -134,6 +135,7 @@ function traverseElement(el) { function translateElement(el, parentNode) { // Get the text field name let k; + let translatedText; if (el.tagName === "INPUT") { if (el.type === 'button' || el.type === 'submit') { k = 'value'; @@ -141,12 +143,21 @@ function translateElement(el, parentNode) { k = 'placeholder'; } - el.setAttribute('data-original-value', el[k]); + translatedText = GetSCLocalizationTranslateString(el[k]); + if (el[k] === translatedText) return; + + const originalValue = parentNode.getAttribute('data-original-value') || ""; + el.setAttribute('data-original-value', originalValue + el[k]); } else { k = 'data'; - parentNode.setAttribute('data-original-value', el[k]); + + translatedText = GetSCLocalizationTranslateString(el[k]); + if (el[k] === translatedText) return; + + const originalValue = parentNode.getAttribute('data-original-value') || ""; + parentNode.setAttribute('data-original-value', originalValue + el[k]); } - el[k] = GetSCLocalizationTranslateString(el[k]); + el[k] = translatedText; } function translateRelativeTimeEl(el) { diff --git a/dist/chrome/content_scripts/content-0.js b/dist/chrome/content_scripts/content-0.js index 352d182..46253e1 100644 --- a/dist/chrome/content_scripts/content-0.js +++ b/dist/chrome/content_scripts/content-0.js @@ -1 +1 @@ -(()=>{var s={756:function(){let s={},n=!1,e=!1;function a(n){n.sort(function(s,n){return n.word.length-s.word.length}).forEach(({word:n,replacement:e})=>{s[n]=e}),window.location.hostname.startsWith("issue-council.robertsspaceindustries.com")&&(s.save="保存"),t().then(s=>{})}async function t(){async function s(n,e){if(n.nodeType===Node.TEXT_NODE){let s=n.nodeValue,a=u(s);s!==a&&(e.setAttribute("data-original-value",s),n.nodeValue=a)}else for(let e=0;e{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"))}),{success:!0}}function i(s,n){let e;"INPUT"===s.tagName?(e="button"===s.type||"submit"===s.type?"value":"placeholder",s.setAttribute("data-original-value",s[e])):(e="data",n.setAttribute("data-original-value",s[e])),s[e]=u(s[e])}function u(e){let a=e.toLowerCase().replace(/\xa0/g," ").replace(/\s{2,}/g," ").trim(),t=e.replace(/\xa0/g," ").replace(/\s{2,}/g," ").trim(),r=a.replace("the ",""),i=a.replace("- ","");if(s[a])e=s[a];else if(n)if(t.includes(" - ")){let n=e,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 "),n="升级包 "+u(s[0])+" 到 "+u(s[1]);a.endsWith("warbond edition")?n+=" 战争债券版":n+=" 标准版",e=n}else r.forEach(function(e){n=s[e.toLowerCase()]?n.replace(e,s[e.toLowerCase()]):n.replace(e,u(e))}),e=n}else if(a.endsWith("starter pack")||a.endsWith("starter package")){let n=a.replace("starter package","").replace("starter pack","").trim();s[n.toLowerCase()]&&(n=s[n.toLowerCase()]),e=n+" 新手包"}else a.startsWith("the ")&&s[r]?e=s[r]:a.startsWith("- ")&&s[i]&&(e="- "+s[i]);return e}new(window.MutationObserver||window.WebKitMutationObserver)(function(s,n){for(let n of s)for(let s of n.addedNodes)!function s(n){if(e&&function(s){if(["IMG","svg","mat-icon"].includes(s.tagName)||s.id&&[].includes(s.id))return!1;if(s.classList){for(let n of["css-truncate"])if(s.classList.contains(n))return!1}return!0}(n))for(let e of n.childNodes){if(["RELATIVE-TIME","TIME-AGO"].includes(n.tagName))return void function(s){let n=navigator.language||navigator.userLanguage,e=$(s).attr("datetime");$(s).text(timeago.format(e,n.replace("-","_")))}(n);e.nodeType===Node.TEXT_NODE?i(e,n):e.nodeType===Node.ELEMENT_NODE&&("INPUT"===e.tagName?i(e,n):s(e,n))}}(s)}).observe(document.body,{subtree:!0,characterData:!0,childList:!0}),window.location.href.includes("robertsspaceindustries.com")&&(n=!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){a(s.result)}),chrome.runtime.onMessage.addListener((s,t,i)=>{if("_toggleTranslation"===s.action){if(e){e=!1,r();return}n=!0,a(s.data)}}),window.addEventListener("message",async s=>{if(s.source!==window||!s.data||"SC_TRANSLATE_REQUEST"!==s.data.type)return;let{action:e}=s.data;if("translate"===e)try{n=!0,chrome.runtime.sendMessage({action:"_loadLocalizationData",url:"manual"},function(s){a(s.result)})}catch(s){s.message}else if("undoTranslate"===e)try{await r()}catch(s){s.message}}),window.postMessage({type:"SC-BOX-TRANSLATE-API-AVAILABLE"},"*")},928:function(s,n){(function(s){"use strict";var n=["second","minute","hour","day","week","month","year"];function e(s,e){if(0===e)return["just now","right now"];var a=n[Math.floor(e/2)];return 1=u[t]&&t=u[e]&&e"1.3.8",e.ruid="bundler=rspack@1.3.8",e(756),e(928)})(); \ No newline at end of file +(()=>{var s={756: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){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"))}),{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.userLanguage,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,e))}}(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){a(s.result)}),chrome.runtime.onMessage.addListener((s,t,i)=>{if("_toggleTranslation"===s.action){if(n){n=!1,r();return}e=!0,a(s.data)}}),window.addEventListener("message",async s=>{if(s.source!==window||!s.data||"SC_TRANSLATE_REQUEST"!==s.data.type)return;let{action:n}=s.data;if("translate"===n)try{e=!0,chrome.runtime.sendMessage({action:"_loadLocalizationData",url:"manual"},function(s){a(s.result)})}catch(s){s.message}else if("undoTranslate"===n)try{await r()}catch(s){s.message}}),window.postMessage({type:"SC-BOX-TRANSLATE-API-AVAILABLE"},"*")},928: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(756),n(928)})(); \ No newline at end of file