diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..7b7fd16 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,9 @@ +{ + "cSpell.words": [ + "clazz", + "erkul", + "robertsspaceindustries", + "timeago", + "WARBOND" + ] +} \ No newline at end of file diff --git a/StarCitizenBoxBrowserEx/README.md b/StarCitizenBoxBrowserEx/README.md new file mode 100644 index 0000000..652fdb3 --- /dev/null +++ b/StarCitizenBoxBrowserEx/README.md @@ -0,0 +1,29 @@ +# 星际公民盒子浏览器拓展 + +为星际公民网站及工具站提供汉化功能的浏览器扩展。 + +## 网页API使用说明 + +本扩展现在提供了网页API,允许网页JavaScript直接调用翻译功能。 + +### 手动触发翻译 + +```javascript +// 检查API是否可用 +if (window.SCTranslateApi && window.SCTranslateApi.translate) { + // 触发翻译 + window.SCTranslateApi.translate() + .then(response => { + if (response.success) { + console.log('翻译成功'); + } else { + console.error('翻译失败:', response.error); + } + }); +} +``` + +## 安全说明 + +- API仅在扩展支持的网站上可用 +- 所有API操作都在网页上下文中执行,不会泄露敏感权限 \ No newline at end of file diff --git a/StarCitizenBoxBrowserEx/background.js b/StarCitizenBoxBrowserEx/background.js index e065f51..b9d094b 100644 --- a/StarCitizenBoxBrowserEx/background.js +++ b/StarCitizenBoxBrowserEx/background.js @@ -31,7 +31,7 @@ async function _initLocalization(url) { // TODO check version let data = {}; - if (url.includes("robertsspaceindustries.com")) { + if (url.includes("robertsspaceindustries.com") || 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", v.orgs); @@ -49,7 +49,7 @@ async function _initLocalization(url) { replaceWords.push(...getLocalizationResource(data, key)); } - if (url.includes("robertsspaceindustries.com")) { + if (url.includes("robertsspaceindustries.com") || url.includes("manual")) { const org = "https://robertsspaceindustries.com/orgs"; const citizens = "https://robertsspaceindustries.com/citizens"; const organization = "https://robertsspaceindustries.com/account/organization"; diff --git a/StarCitizenBoxBrowserEx/content.js b/StarCitizenBoxBrowserEx/content.js new file mode 100644 index 0000000..21ae5b4 --- /dev/null +++ b/StarCitizenBoxBrowserEx/content.js @@ -0,0 +1,46 @@ +// 注入脚本到网页上下文 +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 + }, '*'); +}); \ No newline at end of file diff --git a/StarCitizenBoxBrowserEx/injected.js b/StarCitizenBoxBrowserEx/injected.js new file mode 100644 index 0000000..a4a14da --- /dev/null +++ b/StarCitizenBoxBrowserEx/injected.js @@ -0,0 +1,22 @@ +// 在网页上下文中定义一个API +window.SCTranslateApi = { + // 手动触发页面翻译 + translate: async () => { + const requestId = Math.random().toString(36).substr(2); + return new Promise((resolve) => { + const handler = (event) => { + if (event.data.type === 'SC_TRANSLATE_RESPONSE' && event.data.requestId === requestId) { + window.removeEventListener('message', handler); + resolve(event.data.response); + } + }; + window.addEventListener('message', handler); + + window.postMessage({ + type: 'SC_TRANSLATE_REQUEST', + action: 'translate', + requestId + }, '*'); + }); + } +}; \ No newline at end of file diff --git a/StarCitizenBoxBrowserEx/manifest.json b/StarCitizenBoxBrowserEx/manifest.json index 07140ee..c8a7c70 100644 --- a/StarCitizenBoxBrowserEx/manifest.json +++ b/StarCitizenBoxBrowserEx/manifest.json @@ -25,12 +25,14 @@ "https://robertsspaceindustries.com/*", "https://*.robertsspaceindustries.com/*", "https://www.erkul.games/*", - "https://uexcorp.space/*" + "https://uexcorp.space/*", + "*://*/*" ], "exclude_matches": [ "https://robertsspaceindustries.com/spectrum/*" ], "js": [ + "content.js", "core.js", "thirdparty/timeago.full.min.js" ] @@ -52,5 +54,11 @@ ], "run_at": "document_idle" } + ], + "web_accessible_resources": [ + { + "resources": ["injected.js"], + "matches": [""] + } ] } \ No newline at end of file