mirror of
https://ghfast.top/https://github.com/StarCitizenToolBox/StarCitizenBoxBrowserEx.git
synced 2025-05-09 21:51:25 +08:00
refactor: move content script logic to core.js
This commit is contained in:
parent
9fb43078fe
commit
5410e398c3
@ -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' }, '*');
|
@ -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);
|
||||
});
|
||||
}
|
||||
|
||||
// 注入脚本到网页上下文
|
||||
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' }, '*');
|
@ -32,7 +32,6 @@
|
||||
"https://robertsspaceindustries.com/spectrum/*"
|
||||
],
|
||||
"js": [
|
||||
"content.js",
|
||||
"core.js",
|
||||
"thirdparty/timeago.full.min.js"
|
||||
]
|
||||
|
Loading…
x
Reference in New Issue
Block a user