fix iframe bug
This commit is contained in:
@@ -7,7 +7,7 @@ import {
|
|||||||
} from "./config";
|
} from "./config";
|
||||||
import { getSettingWithDefault, getRulesWithDefault } from "./libs/storage";
|
import { getSettingWithDefault, getRulesWithDefault } from "./libs/storage";
|
||||||
import { Translator } from "./libs/translator";
|
import { Translator } from "./libs/translator";
|
||||||
import { isIframe } from "./libs/iframe";
|
import { isIframe, sendIframeMsg, sendPrentMsg } from "./libs/iframe";
|
||||||
import { matchRule } from "./libs/rules";
|
import { matchRule } from "./libs/rules";
|
||||||
import { webfix } from "./libs/webfix";
|
import { webfix } from "./libs/webfix";
|
||||||
|
|
||||||
@@ -15,8 +15,34 @@ import { webfix } from "./libs/webfix";
|
|||||||
* 入口函数
|
* 入口函数
|
||||||
*/
|
*/
|
||||||
const init = async () => {
|
const init = async () => {
|
||||||
const href = isIframe ? document.referrer : document.location.href;
|
|
||||||
const setting = await getSettingWithDefault();
|
const setting = await getSettingWithDefault();
|
||||||
|
|
||||||
|
if (isIframe) {
|
||||||
|
let translator;
|
||||||
|
window.addEventListener("message", (e) => {
|
||||||
|
const { action, args } = e.data || {};
|
||||||
|
switch (action) {
|
||||||
|
case MSG_TRANS_TOGGLE:
|
||||||
|
translator?.toggle();
|
||||||
|
break;
|
||||||
|
case MSG_TRANS_TOGGLE_STYLE:
|
||||||
|
translator?.toggleStyle();
|
||||||
|
break;
|
||||||
|
case MSG_TRANS_PUTRULE:
|
||||||
|
if (!translator) {
|
||||||
|
translator = new Translator(args, setting);
|
||||||
|
} else {
|
||||||
|
translator.updateRule(args || {});
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
});
|
||||||
|
sendPrentMsg(MSG_TRANS_GETRULE);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const href = document.location.href;
|
||||||
const rules = await getRulesWithDefault();
|
const rules = await getRulesWithDefault();
|
||||||
const rule = await matchRule(rules, href, setting);
|
const rule = await matchRule(rules, href, setting);
|
||||||
const translator = new Translator(rule, setting);
|
const translator = new Translator(rule, setting);
|
||||||
@@ -27,20 +53,32 @@ const init = async () => {
|
|||||||
switch (action) {
|
switch (action) {
|
||||||
case MSG_TRANS_TOGGLE:
|
case MSG_TRANS_TOGGLE:
|
||||||
translator.toggle();
|
translator.toggle();
|
||||||
|
sendIframeMsg(MSG_TRANS_TOGGLE);
|
||||||
break;
|
break;
|
||||||
case MSG_TRANS_TOGGLE_STYLE:
|
case MSG_TRANS_TOGGLE_STYLE:
|
||||||
translator.toggleStyle();
|
translator.toggleStyle();
|
||||||
|
sendIframeMsg(MSG_TRANS_TOGGLE_STYLE);
|
||||||
break;
|
break;
|
||||||
case MSG_TRANS_GETRULE:
|
case MSG_TRANS_GETRULE:
|
||||||
break;
|
break;
|
||||||
case MSG_TRANS_PUTRULE:
|
case MSG_TRANS_PUTRULE:
|
||||||
translator.updateRule(args);
|
translator.updateRule(args);
|
||||||
|
sendIframeMsg(MSG_TRANS_PUTRULE, args);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
return { error: `message action is unavailable: ${action}` };
|
return { error: `message action is unavailable: ${action}` };
|
||||||
}
|
}
|
||||||
return { data: translator.rule };
|
return { data: translator.rule };
|
||||||
});
|
});
|
||||||
|
window.addEventListener("message", (e) => {
|
||||||
|
const { action } = e.data || {};
|
||||||
|
switch (action) {
|
||||||
|
case MSG_TRANS_GETRULE:
|
||||||
|
sendIframeMsg(MSG_TRANS_PUTRULE, rule);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
(async () => {
|
(async () => {
|
||||||
|
|||||||
@@ -5,3 +5,7 @@ export const sendIframeMsg = (action, args) => {
|
|||||||
iframe.contentWindow.postMessage({ action, args }, "*");
|
iframe.contentWindow.postMessage({ action, args }, "*");
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const sendPrentMsg = (action, args) => {
|
||||||
|
window.parent.postMessage({ action, args }, "*");
|
||||||
|
};
|
||||||
|
|||||||
@@ -10,8 +10,13 @@ import {
|
|||||||
} from "./libs/storage";
|
} from "./libs/storage";
|
||||||
import { Translator } from "./libs/translator";
|
import { Translator } from "./libs/translator";
|
||||||
import { trySyncAllSubRules } from "./libs/subRules";
|
import { trySyncAllSubRules } from "./libs/subRules";
|
||||||
import { MSG_TRANS_TOGGLE, MSG_TRANS_PUTRULE } from "./config";
|
import {
|
||||||
import { isIframe } from "./libs/iframe";
|
MSG_TRANS_TOGGLE,
|
||||||
|
MSG_TRANS_TOGGLE_STYLE,
|
||||||
|
MSG_TRANS_GETRULE,
|
||||||
|
MSG_TRANS_PUTRULE,
|
||||||
|
} from "./config";
|
||||||
|
import { isIframe, sendIframeMsg, sendPrentMsg } from "./libs/iframe";
|
||||||
import { handlePing, injectScript } from "./libs/gm";
|
import { handlePing, injectScript } from "./libs/gm";
|
||||||
import { matchRule } from "./libs/rules";
|
import { matchRule } from "./libs/rules";
|
||||||
import { genEventName } from "./libs/utils";
|
import { genEventName } from "./libs/utils";
|
||||||
@@ -46,29 +51,49 @@ const init = async () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 翻译页面
|
// 翻译页面
|
||||||
const href = isIframe ? document.referrer : document.location.href;
|
|
||||||
const setting = await getSettingWithDefault();
|
const setting = await getSettingWithDefault();
|
||||||
|
|
||||||
|
if (isIframe) {
|
||||||
|
let translator;
|
||||||
|
window.addEventListener("message", (e) => {
|
||||||
|
const { action, args } = e.data || {};
|
||||||
|
switch (action) {
|
||||||
|
case MSG_TRANS_TOGGLE:
|
||||||
|
translator?.toggle();
|
||||||
|
break;
|
||||||
|
case MSG_TRANS_TOGGLE_STYLE:
|
||||||
|
translator?.toggleStyle();
|
||||||
|
break;
|
||||||
|
case MSG_TRANS_PUTRULE:
|
||||||
|
if (!translator) {
|
||||||
|
translator = new Translator(args, setting);
|
||||||
|
} else {
|
||||||
|
translator.updateRule(args || {});
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
});
|
||||||
|
sendPrentMsg(MSG_TRANS_GETRULE);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const href = isIframe ? document.referrer : document.location.href;
|
||||||
const rules = await getRulesWithDefault();
|
const rules = await getRulesWithDefault();
|
||||||
const rule = await matchRule(rules, href, setting);
|
const rule = await matchRule(rules, href, setting);
|
||||||
const translator = new Translator(rule, setting);
|
const translator = new Translator(rule, setting);
|
||||||
webfix(href, setting);
|
webfix(href, setting);
|
||||||
|
|
||||||
if (isIframe) {
|
// 监听消息
|
||||||
// iframe
|
window.addEventListener("message", (e) => {
|
||||||
window.addEventListener("message", (e) => {
|
const { action } = e.data || {};
|
||||||
const action = e?.data?.action;
|
switch (action) {
|
||||||
switch (action) {
|
case MSG_TRANS_GETRULE:
|
||||||
case MSG_TRANS_TOGGLE:
|
sendIframeMsg(MSG_TRANS_PUTRULE, rule);
|
||||||
translator.toggle();
|
break;
|
||||||
break;
|
default:
|
||||||
case MSG_TRANS_PUTRULE:
|
}
|
||||||
translator.updateRule(e.data.args || {});
|
});
|
||||||
break;
|
|
||||||
default:
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 浮球按钮
|
// 浮球按钮
|
||||||
const fab = await getFabWithDefault();
|
const fab = await getFabWithDefault();
|
||||||
|
|||||||
@@ -16,8 +16,11 @@ import {
|
|||||||
OPT_SHORTCUT_STYLE,
|
OPT_SHORTCUT_STYLE,
|
||||||
OPT_SHORTCUT_POPUP,
|
OPT_SHORTCUT_POPUP,
|
||||||
OPT_SHORTCUT_SETTING,
|
OPT_SHORTCUT_SETTING,
|
||||||
|
MSG_TRANS_TOGGLE,
|
||||||
|
MSG_TRANS_TOGGLE_STYLE,
|
||||||
} from "../../config";
|
} from "../../config";
|
||||||
import { shortcutRegister } from "../../libs/shortcut";
|
import { shortcutRegister } from "../../libs/shortcut";
|
||||||
|
import { sendIframeMsg } from "../../libs/iframe";
|
||||||
|
|
||||||
export default function Action({ translator, fab }) {
|
export default function Action({ translator, fab }) {
|
||||||
const fabWidth = 40;
|
const fabWidth = 40;
|
||||||
@@ -57,10 +60,12 @@ export default function Action({ translator, fab }) {
|
|||||||
const clearShortcuts = [
|
const clearShortcuts = [
|
||||||
shortcutRegister(shortcuts[OPT_SHORTCUT_TRANSLATE], () => {
|
shortcutRegister(shortcuts[OPT_SHORTCUT_TRANSLATE], () => {
|
||||||
translator.toggle();
|
translator.toggle();
|
||||||
|
sendIframeMsg(MSG_TRANS_TOGGLE);
|
||||||
setShowPopup(false);
|
setShowPopup(false);
|
||||||
}),
|
}),
|
||||||
shortcutRegister(shortcuts[OPT_SHORTCUT_STYLE], () => {
|
shortcutRegister(shortcuts[OPT_SHORTCUT_STYLE], () => {
|
||||||
translator.toggleStyle();
|
translator.toggleStyle();
|
||||||
|
sendIframeMsg(MSG_TRANS_TOGGLE_STYLE);
|
||||||
setShowPopup(false);
|
setShowPopup(false);
|
||||||
}),
|
}),
|
||||||
shortcutRegister(shortcuts[OPT_SHORTCUT_POPUP], () => {
|
shortcutRegister(shortcuts[OPT_SHORTCUT_POPUP], () => {
|
||||||
@@ -91,6 +96,7 @@ export default function Action({ translator, fab }) {
|
|||||||
"Toggle Translate (Alt+q)",
|
"Toggle Translate (Alt+q)",
|
||||||
(event) => {
|
(event) => {
|
||||||
translator.toggle();
|
translator.toggle();
|
||||||
|
sendIframeMsg(MSG_TRANS_TOGGLE);
|
||||||
setShowPopup(false);
|
setShowPopup(false);
|
||||||
},
|
},
|
||||||
"Q"
|
"Q"
|
||||||
@@ -99,6 +105,7 @@ export default function Action({ translator, fab }) {
|
|||||||
"Toggle Style (Alt+c)",
|
"Toggle Style (Alt+c)",
|
||||||
(event) => {
|
(event) => {
|
||||||
translator.toggleStyle();
|
translator.toggleStyle();
|
||||||
|
sendIframeMsg(MSG_TRANS_TOGGLE_STYLE);
|
||||||
setShowPopup(false);
|
setShowPopup(false);
|
||||||
},
|
},
|
||||||
"C"
|
"C"
|
||||||
|
|||||||
Reference in New Issue
Block a user