From ccd457c99282e25ac69219b77119c4340afee766 Mon Sep 17 00:00:00 2001 From: Gabe Date: Thu, 30 Oct 2025 22:01:08 +0800 Subject: [PATCH] fix: iframe bugs --- src/config/i18n.js | 2 +- src/hooks/Alert.js | 6 +++++- src/libs/fabManager.js | 4 ++-- src/libs/translatorManager.js | 15 +++++++++------ src/views/Action/ContentFab.js | 7 ++----- src/views/Popup/PopupCont.js | 1 + 6 files changed, 20 insertions(+), 15 deletions(-) diff --git a/src/config/i18n.js b/src/config/i18n.js index 1bdc716..bbb94a7 100644 --- a/src/config/i18n.js +++ b/src/config/i18n.js @@ -1788,7 +1788,7 @@ export const I18N = { zh: `滚动加载提前触发 (0-10000px)`, en: `Early triggering of scroll loading (0-10000px)`, zh_TW: `滾動載入提前觸發 (0-10000px)`, - } + }, }; export const newI18n = (lang) => (key) => I18N[key]?.[lang] || ""; diff --git a/src/hooks/Alert.js b/src/hooks/Alert.js index f23e04d..2cabcb7 100644 --- a/src/hooks/Alert.js +++ b/src/hooks/Alert.js @@ -59,7 +59,11 @@ export function AlertProvider({ children }) { onClose={handleClose} anchorOrigin={{ vertical, horizontal }} > - + {message} diff --git a/src/libs/fabManager.js b/src/libs/fabManager.js index 25a44af..bd96671 100644 --- a/src/libs/fabManager.js +++ b/src/libs/fabManager.js @@ -3,12 +3,12 @@ import { APP_CONSTS } from "../config"; import ContentFab from "../views/Action/ContentFab"; export class FabManager extends ShadowDomManager { - constructor({ translator, processActions, fabConfig }) { + constructor({ processActions, fabConfig }) { super({ id: APP_CONSTS.fabID, className: "notranslate", reactComponent: ContentFab, - props: { translator, processActions, fabConfig }, + props: { processActions, fabConfig }, }); if (!fabConfig?.isHide) { diff --git a/src/libs/translatorManager.js b/src/libs/translatorManager.js index 5a3547b..87d7697 100644 --- a/src/libs/translatorManager.js +++ b/src/libs/translatorManager.js @@ -54,15 +54,15 @@ export default class TranslatorManager { isIframe, }); + this._transboxManager = new TransboxManager(setting); + if (!isIframe) { - this._transboxManager = new TransboxManager(setting); this._inputTranslator = new InputTranslator(setting); this._popupManager = new PopupManager({ translator: this._translator, processActions: this.#processActions.bind(this), }); this._fabManager = new FabManager({ - translator: this._translator, processActions: this.#processActions.bind(this), fabConfig, }); @@ -137,6 +137,9 @@ export default class TranslatorManager { window.addEventListener("message", this.#windowMessageHandler); } else { browser.runtime.onMessage.addListener(this.#browserMessageHandler); + if (this.#isIframe) { + window.addEventListener("message", this.#windowMessageHandler); + } } } @@ -184,7 +187,7 @@ export default class TranslatorManager { } #handleBrowserMessage(message, sender, sendResponse) { - const result = this.#processActions(message); + const result = this.#processActions(message, true); const response = result || { rule: this._translator.rule, setting: this._translator.setting, @@ -242,9 +245,9 @@ export default class TranslatorManager { ]; } - #processActions({ action, args } = {}) { - if (this.#isUserscript) { - sendIframeMsg(action); + #processActions({ action, args } = {}, fromExt = false) { + if (!fromExt) { + sendIframeMsg(action, args); } switch (action) { diff --git a/src/views/Action/ContentFab.js b/src/views/Action/ContentFab.js index 397fd38..955145b 100644 --- a/src/views/Action/ContentFab.js +++ b/src/views/Action/ContentFab.js @@ -5,11 +5,9 @@ import Draggable from "./Draggable"; import { useState, useMemo, useCallback } from "react"; import { SettingProvider } from "../../hooks/Setting"; import { MSG_TRANS_TOGGLE, MSG_POPUP_TOGGLE } from "../../config"; -import { sendIframeMsg } from "../../libs/iframe"; import useWindowSize from "../../hooks/WindowSize"; export default function ContentFab({ - translator, fabConfig: { x: fabX, y: fabY, fabClickAction = 0 } = {}, processActions, }) { @@ -28,13 +26,12 @@ export default function ContentFab({ const handleClick = useCallback(() => { if (!moved) { if (fabClickAction === 1) { - translator.toggle(); - sendIframeMsg(MSG_TRANS_TOGGLE); + processActions({ action: MSG_TRANS_TOGGLE }); } else { processActions({ action: MSG_POPUP_TOGGLE }); } } - }, [moved, translator, fabClickAction, processActions]); + }, [moved, fabClickAction, processActions]); const fabProps = useMemo( () => ({ diff --git a/src/views/Popup/PopupCont.js b/src/views/Popup/PopupCont.js index 299d9db..103a8aa 100644 --- a/src/views/Popup/PopupCont.js +++ b/src/views/Popup/PopupCont.js @@ -110,6 +110,7 @@ export default function PopupCont({ }; const handleChange = async (e) => { + console.log("handleChange", e); try { const { name, value } = e.target; setRule((pre) => ({ ...pre, [name]: value }));