mirror of
https://ghfast.top/https://github.com/StarCitizenToolBox/StarCitizenBoxBrowserEx.git
synced 2025-05-09 21:51:25 +08:00
feat: 为chrome拓展添加手动调用翻译的功能
This commit is contained in:
parent
75565c0ef0
commit
22ab42d607
9
.vscode/settings.json
vendored
Normal file
9
.vscode/settings.json
vendored
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"cSpell.words": [
|
||||||
|
"clazz",
|
||||||
|
"erkul",
|
||||||
|
"robertsspaceindustries",
|
||||||
|
"timeago",
|
||||||
|
"WARBOND"
|
||||||
|
]
|
||||||
|
}
|
29
StarCitizenBoxBrowserEx/README.md
Normal file
29
StarCitizenBoxBrowserEx/README.md
Normal 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操作都在网页上下文中执行,不会泄露敏感权限
|
@ -31,7 +31,7 @@ async function _initLocalization(url) {
|
|||||||
// TODO check version
|
// TODO check version
|
||||||
let data = {};
|
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["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["concierge"] = await _getJsonData("concierge.json", {cacheKey: "concierge", version: v.concierge});
|
||||||
data["orgs"] = await _getJsonData("orgs.json", v.orgs);
|
data["orgs"] = await _getJsonData("orgs.json", v.orgs);
|
||||||
@ -49,7 +49,7 @@ async function _initLocalization(url) {
|
|||||||
replaceWords.push(...getLocalizationResource(data, key));
|
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 org = "https://robertsspaceindustries.com/orgs";
|
||||||
const citizens = "https://robertsspaceindustries.com/citizens";
|
const citizens = "https://robertsspaceindustries.com/citizens";
|
||||||
const organization = "https://robertsspaceindustries.com/account/organization";
|
const organization = "https://robertsspaceindustries.com/account/organization";
|
||||||
|
46
StarCitizenBoxBrowserEx/content.js
Normal file
46
StarCitizenBoxBrowserEx/content.js
Normal 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
|
||||||
|
}, '*');
|
||||||
|
});
|
22
StarCitizenBoxBrowserEx/injected.js
Normal file
22
StarCitizenBoxBrowserEx/injected.js
Normal 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
|
||||||
|
}, '*');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
@ -25,12 +25,14 @@
|
|||||||
"https://robertsspaceindustries.com/*",
|
"https://robertsspaceindustries.com/*",
|
||||||
"https://*.robertsspaceindustries.com/*",
|
"https://*.robertsspaceindustries.com/*",
|
||||||
"https://www.erkul.games/*",
|
"https://www.erkul.games/*",
|
||||||
"https://uexcorp.space/*"
|
"https://uexcorp.space/*",
|
||||||
|
"*://*/*"
|
||||||
],
|
],
|
||||||
"exclude_matches": [
|
"exclude_matches": [
|
||||||
"https://robertsspaceindustries.com/spectrum/*"
|
"https://robertsspaceindustries.com/spectrum/*"
|
||||||
],
|
],
|
||||||
"js": [
|
"js": [
|
||||||
|
"content.js",
|
||||||
"core.js",
|
"core.js",
|
||||||
"thirdparty/timeago.full.min.js"
|
"thirdparty/timeago.full.min.js"
|
||||||
]
|
]
|
||||||
@ -52,5 +54,11 @@
|
|||||||
],
|
],
|
||||||
"run_at": "document_idle"
|
"run_at": "document_idle"
|
||||||
}
|
}
|
||||||
|
],
|
||||||
|
"web_accessible_resources": [
|
||||||
|
{
|
||||||
|
"resources": ["injected.js"],
|
||||||
|
"matches": ["<all_urls>"]
|
||||||
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user