userscript...

This commit is contained in:
Gabe Yuan
2023-08-06 21:12:01 +08:00
parent 063d7c9ff0
commit fef4d50977
14 changed files with 456 additions and 102 deletions

View File

@@ -2,8 +2,9 @@ import { useEffect } from "react";
import { useState } from "react";
import { transPool } from "../libs/pool";
import { browser } from "../libs/browser";
import { MSG_TRANS_PUTRULE } from "../config";
import { MSG_TRANS_PUTRULE, EVENT_KISS } from "../config";
import { detectLang } from "../libs";
import { isExt } from "../libs/browser";
/**
* 翻译hook
@@ -25,10 +26,31 @@ export function useTranslate(q, initRule) {
return true;
};
const handleKissEvent = (e) => {
const action = e?.detail?.action;
const args = e?.detail?.args || {};
switch (action) {
case MSG_TRANS_PUTRULE:
setRule((pre) => ({ ...pre, ...args }));
break;
default:
// console.log(`[popup] kissEvent action skip: ${action}`);
}
};
useEffect(() => {
browser?.runtime.onMessage.addListener(handleMessage);
if (isExt) {
browser?.runtime.onMessage.addListener(handleMessage);
} else {
window.addEventListener(EVENT_KISS, handleKissEvent);
}
return () => {
browser?.runtime.onMessage.removeListener(handleMessage);
if (isExt) {
browser?.runtime.onMessage.removeListener(handleMessage);
} else {
window.removeEventListener(EVENT_KISS, handleKissEvent);
}
};
}, []);