diff --git a/StarCitizenBoxBrowserEx/content.js b/StarCitizenBoxBrowserEx/content.js deleted file mode 100644 index e222f63..0000000 --- a/StarCitizenBoxBrowserEx/content.js +++ /dev/null @@ -1,48 +0,0 @@ -// 注入脚本到网页上下文 -const script = document.createElement('script'); -script.src = chrome.runtime.getURL('injected.js'); -script.onload = function() { - this.remove(); -}; -(document.head || document.documentElement).appendChild(script); - -// 监听来自网页的消息 -window.addEventListener('message', async (event) => { - if (event.source !== window || !event.data || event.data.type !== 'SC_TRANSLATE_REQUEST') return; - - console.log("event.data ==" + JSON.stringify(event.data)); - - const { action, payload, requestId } = event.data; - - let response = { success: false }; - - if (action === 'translate') { - try { - chrome.runtime.sendMessage({action: "_loadLocalizationData", url: "manual"}, function (response) { - WebLocalizationUpdateReplaceWords(response.result); - }); - } catch (error) { - response = { success: false, error: error.message }; - } - } else if (action === 'updateReplaceWords') { - try { - if (payload && payload.words && Array.isArray(payload.words)) { - WebLocalizationUpdateReplaceWords(payload.words); - response = { success: true }; - } else { - response = { success: false, error: 'Invalid words format' }; - } - } catch (error) { - response = { success: false, error: error.message }; - } - } - - // 发送响应回网页 - window.postMessage({ - type: 'SC_TRANSLATE_RESPONSE', - requestId, - response - }, '*'); -}); - -window.postMessage({ type: 'SC-BOX-TRANSLATE-API-AVAILABLE' }, '*'); \ No newline at end of file diff --git a/StarCitizenBoxBrowserEx/core.js b/StarCitizenBoxBrowserEx/core.js index ad31063..f0dcfa5 100644 --- a/StarCitizenBoxBrowserEx/core.js +++ b/StarCitizenBoxBrowserEx/core.js @@ -43,7 +43,7 @@ function WebLocalizationUpdateReplaceWords(w) { let replaceWords = w.sort(function (a, b) { return b.word.length - a.word.length; }); - replaceWords.forEach(({word, replacement}) => { + replaceWords.forEach(({ word, replacement }) => { SCLocalizationReplaceLocalesMap[word] = replacement; }); if (window.location.hostname.startsWith("issue-council.robertsspaceindustries.com")) { @@ -188,7 +188,57 @@ function GetSCLocalizationTranslateString(txtSrc) { InitWebLocalization(); function _loadLocalizationData() { - chrome.runtime.sendMessage({action: "_loadLocalizationData", url: window.location.href}, function (response) { + chrome.runtime.sendMessage({ action: "_loadLocalizationData", url: window.location.href }, function (response) { WebLocalizationUpdateReplaceWords(response.result); }); -} \ No newline at end of file +} + +// 注入脚本到网页上下文 +const script = document.createElement('script'); +script.src = chrome.runtime.getURL('injected.js'); +script.onload = function () { + this.remove(); +}; +(document.head || document.documentElement).appendChild(script); + +// 监听来自网页的消息 +window.addEventListener('message', async (event) => { + if (event.source !== window || !event.data || event.data.type !== 'SC_TRANSLATE_REQUEST') return; + + console.log("event.data ==" + JSON.stringify(event.data)); + + const { action, payload, requestId } = event.data; + + let response = { success: false }; + + if (action === 'translate') { + try { + SCLocalizationEnableSplitMode = true; + chrome.runtime.sendMessage({ action: "_loadLocalizationData", url: "manual" }, function (response) { + WebLocalizationUpdateReplaceWords(response.result); + }); + } catch (error) { + response = { success: false, error: error.message }; + } + } else if (action === 'updateReplaceWords') { + try { + if (payload && payload.words && Array.isArray(payload.words)) { + WebLocalizationUpdateReplaceWords(payload.words); + response = { success: true }; + } else { + response = { success: false, error: 'Invalid words format' }; + } + } catch (error) { + response = { success: false, error: error.message }; + } + } + + // 发送响应回网页 + window.postMessage({ + type: 'SC_TRANSLATE_RESPONSE', + requestId, + response + }, '*'); +}); + +window.postMessage({ type: 'SC-BOX-TRANSLATE-API-AVAILABLE' }, '*'); \ No newline at end of file diff --git a/StarCitizenBoxBrowserEx/manifest.json b/StarCitizenBoxBrowserEx/manifest.json index c8a7c70..f77d900 100644 --- a/StarCitizenBoxBrowserEx/manifest.json +++ b/StarCitizenBoxBrowserEx/manifest.json @@ -32,7 +32,6 @@ "https://robertsspaceindustries.com/spectrum/*" ], "js": [ - "content.js", "core.js", "thirdparty/timeago.full.min.js" ]