feat: 为chrome拓展添加手动调用翻译的功能

This commit is contained in:
EduarteXD 2025-05-03 00:32:49 +08:00
parent 75565c0ef0
commit 22ab42d607
6 changed files with 117 additions and 3 deletions

9
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,9 @@
{
"cSpell.words": [
"clazz",
"erkul",
"robertsspaceindustries",
"timeago",
"WARBOND"
]
}

View File

@ -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操作都在网页上下文中执行不会泄露敏感权限

View File

@ -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";

View File

@ -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
}, '*');
});

View File

@ -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
}, '*');
});
}
};

View File

@ -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": ["<all_urls>"]
}
]
}