fix: 兼容网页主动调用

This commit is contained in:
xkeyC 2025-05-08 22:49:39 +08:00
parent 23f7df8400
commit 72185370c9
2 changed files with 6 additions and 2 deletions

View File

@ -254,6 +254,7 @@ function _loadLocalizationData() {
if (response.result.length > 0) { if (response.result.length > 0) {
SCLocalizationTranslating = true; SCLocalizationTranslating = true;
WebLocalizationUpdateReplaceWords(response.result); WebLocalizationUpdateReplaceWords(response.result);
window.postMessage({ type: 'TOGGLED-SC-BOX-TRANSLATE', action: 'on' }, '*');
} }
}); });
} }
@ -283,8 +284,10 @@ window.addEventListener('message', async (event) => {
if (action === 'translate') { if (action === 'translate') {
try { try {
_saveLocalizationSwitchStater(true)
await new Promise(resolve => setTimeout(resolve, 100));
SCLocalizationEnableSplitMode = true; SCLocalizationEnableSplitMode = true;
chrome.runtime.sendMessage({ action: "_loadLocalizationData", url: "manual" }, function (response) { chrome.runtime.sendMessage({ action: "_loadLocalizationData", url: window.location.href }, function (response) {
SCLocalizationTranslating = true; SCLocalizationTranslating = true;
window.postMessage({ type: 'TOGGLED-SC-BOX-TRANSLATE', action: 'on' }, '*'); window.postMessage({ type: 'TOGGLED-SC-BOX-TRANSLATE', action: 'on' }, '*');
WebLocalizationUpdateReplaceWords(response.result); WebLocalizationUpdateReplaceWords(response.result);
@ -295,6 +298,7 @@ window.addEventListener('message', async (event) => {
} }
} else if (action === 'undoTranslate') { } else if (action === 'undoTranslate') {
try { try {
_saveLocalizationSwitchStater(false)
response = await undoTranslate(); response = await undoTranslate();
} catch (error: any) { } catch (error: any) {
response = { success: false, error: error.message }; response = { success: false, error: error.message };

File diff suppressed because one or more lines are too long