feat: add context menu for translating pages

This commit is contained in:
EduarteXD
2025-05-04 12:32:14 +08:00
parent 5410e398c3
commit 1f1c501124
3 changed files with 24 additions and 5 deletions

View File

@ -151,3 +151,16 @@ function setLocalData(key, data) {
}); });
}); });
} }
chrome.contextMenus.create({
id: "translate",
title: "翻译本页面",
contexts: ["page"]
});
chrome.contextMenus.onClicked.addListener((info, tab) => {
console.log("contextMenus", info, tab);
_initLocalization("manual").then(data => {
chrome.tabs.sendMessage(tab.id, {action: "_initTranslation", data});
});
});

View File

@ -193,6 +193,13 @@ function _loadLocalizationData() {
}); });
} }
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "_initTranslation") {
SCLocalizationEnableSplitMode = true;
WebLocalizationUpdateReplaceWords(request.data);
}
});
// 注入脚本到网页上下文 // 注入脚本到网页上下文
const script = document.createElement('script'); const script = document.createElement('script');
script.src = chrome.runtime.getURL('injected.js'); script.src = chrome.runtime.getURL('injected.js');

View File

@ -5,13 +5,16 @@
"description": "为星际公民网站及工具站提供汉化", "description": "为星际公民网站及工具站提供汉化",
"author": "xkeyC", "author": "xkeyC",
"icons": { "icons": {
"16": "icon.png",
"48": "icon.png",
"192": "icon.png" "192": "icon.png"
}, },
"host_permissions": [ "host_permissions": [
"https://git.scbox.xkeyc.cn/*" "https://git.scbox.xkeyc.cn/*"
], ],
"permissions": [ "permissions": [
"storage" "storage",
"contextMenus"
], ],
"action": { "action": {
"default_popup": "popup.html" "default_popup": "popup.html"
@ -22,10 +25,6 @@
"content_scripts": [ "content_scripts": [
{ {
"matches": [ "matches": [
"https://robertsspaceindustries.com/*",
"https://*.robertsspaceindustries.com/*",
"https://www.erkul.games/*",
"https://uexcorp.space/*",
"*://*/*" "*://*/*"
], ],
"exclude_matches": [ "exclude_matches": [