use random eventname
This commit is contained in:
@@ -53,8 +53,6 @@ export const MSG_TRANS_GETRULE = "trans_getrule";
|
|||||||
export const MSG_TRANS_PUTRULE = "trans_putrule";
|
export const MSG_TRANS_PUTRULE = "trans_putrule";
|
||||||
export const MSG_TRANS_CURRULE = "trans_currule";
|
export const MSG_TRANS_CURRULE = "trans_currule";
|
||||||
|
|
||||||
export const EVENT_KISS = "kissEvent";
|
|
||||||
|
|
||||||
export const THEME_LIGHT = "light";
|
export const THEME_LIGHT = "light";
|
||||||
export const THEME_DARK = "dark";
|
export const THEME_DARK = "dark";
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,10 @@
|
|||||||
import { fetchGM } from "./fetch";
|
import { fetchGM } from "./fetch";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 之否支持unsafeWindow
|
||||||
|
*/
|
||||||
|
export const isGrantUnsafe = GM?.info?.script?.grant?.includes("unsafeWindow");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 注入页面的脚本,请求并接受GM接口信息
|
* 注入页面的脚本,请求并接受GM接口信息
|
||||||
* @param {*} param0
|
* @param {*} param0
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ import {
|
|||||||
APP_LCNAME,
|
APP_LCNAME,
|
||||||
TRANS_MIN_LENGTH,
|
TRANS_MIN_LENGTH,
|
||||||
TRANS_MAX_LENGTH,
|
TRANS_MAX_LENGTH,
|
||||||
EVENT_KISS,
|
|
||||||
MSG_TRANS_CURRULE,
|
MSG_TRANS_CURRULE,
|
||||||
OPT_STYLE_DASHLINE,
|
OPT_STYLE_DASHLINE,
|
||||||
OPT_STYLE_FUZZY,
|
OPT_STYLE_FUZZY,
|
||||||
@@ -11,7 +10,7 @@ import {
|
|||||||
} from "../config";
|
} from "../config";
|
||||||
import Content from "../views/Content";
|
import Content from "../views/Content";
|
||||||
import { updateFetchPool, clearFetchPool } from "./fetch";
|
import { updateFetchPool, clearFetchPool } from "./fetch";
|
||||||
import { debounce } from "./utils";
|
import { debounce, genEventName } from "./utils";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 翻译类
|
* 翻译类
|
||||||
@@ -37,6 +36,7 @@ export class Translator {
|
|||||||
"script",
|
"script",
|
||||||
"iframe",
|
"iframe",
|
||||||
];
|
];
|
||||||
|
_eventName = genEventName();
|
||||||
|
|
||||||
// 显示
|
// 显示
|
||||||
_interseObserver = new IntersectionObserver(
|
_interseObserver = new IntersectionObserver(
|
||||||
@@ -105,6 +105,10 @@ export class Translator {
|
|||||||
return this._setting;
|
return this._setting;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get eventName() {
|
||||||
|
return this._eventName;
|
||||||
|
}
|
||||||
|
|
||||||
get rule() {
|
get rule() {
|
||||||
// console.log("get rule", this._rule);
|
// console.log("get rule", this._rule);
|
||||||
return this._rule;
|
return this._rule;
|
||||||
@@ -115,8 +119,9 @@ export class Translator {
|
|||||||
this._rule = rule;
|
this._rule = rule;
|
||||||
|
|
||||||
// 广播消息
|
// 广播消息
|
||||||
|
const eventName = this._eventName;
|
||||||
window.dispatchEvent(
|
window.dispatchEvent(
|
||||||
new CustomEvent(EVENT_KISS, {
|
new CustomEvent(eventName, {
|
||||||
detail: {
|
detail: {
|
||||||
action: MSG_TRANS_CURRULE,
|
action: MSG_TRANS_CURRULE,
|
||||||
args: rule,
|
args: rule,
|
||||||
|
|||||||
@@ -116,3 +116,9 @@ export const sha256 = async (text, salt) => {
|
|||||||
.map((b) => b.toString(16).padStart(2, "0"))
|
.map((b) => b.toString(16).padStart(2, "0"))
|
||||||
.join("");
|
.join("");
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成随机事件名称
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
|
export const genEventName = () => btoa(Math.random()).slice(3, 11);
|
||||||
|
|||||||
@@ -12,8 +12,9 @@ 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 { MSG_TRANS_TOGGLE, MSG_TRANS_PUTRULE } from "./config";
|
||||||
import { isIframe } from "./libs/iframe";
|
import { isIframe } from "./libs/iframe";
|
||||||
import { handlePing, injectScript } from "./libs/gm";
|
import { isGrantUnsafe, handlePing, injectScript } from "./libs/gm";
|
||||||
import { matchRule } from "./libs/rules";
|
import { matchRule } from "./libs/rules";
|
||||||
|
import { genEventName } from "./libs/utils";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 入口函数
|
* 入口函数
|
||||||
@@ -25,11 +26,11 @@ const init = async () => {
|
|||||||
document.location.href.includes(process.env.REACT_APP_OPTIONSPAGE) ||
|
document.location.href.includes(process.env.REACT_APP_OPTIONSPAGE) ||
|
||||||
document.location.href.includes(process.env.REACT_APP_OPTIONSPAGE2)
|
document.location.href.includes(process.env.REACT_APP_OPTIONSPAGE2)
|
||||||
) {
|
) {
|
||||||
if (GM?.info?.script?.grant?.includes("unsafeWindow")) {
|
if (isGrantUnsafe) {
|
||||||
unsafeWindow.GM = GM;
|
unsafeWindow.GM = GM;
|
||||||
unsafeWindow.APP_NAME = process.env.REACT_APP_NAME;
|
unsafeWindow.APP_NAME = process.env.REACT_APP_NAME;
|
||||||
} else {
|
} else {
|
||||||
const ping = btoa(Math.random()).slice(3, 11);
|
const ping = genEventName();
|
||||||
window.addEventListener(ping, handlePing);
|
window.addEventListener(ping, handlePing);
|
||||||
// window.eval(`(${injectScript})("${ping}")`); // eslint-disable-line
|
// window.eval(`(${injectScript})("${ping}")`); // eslint-disable-line
|
||||||
const script = document.createElement("script");
|
const script = document.createElement("script");
|
||||||
|
|||||||
@@ -9,7 +9,6 @@ import {
|
|||||||
OPT_STYLE_HIGHLIGHT,
|
OPT_STYLE_HIGHLIGHT,
|
||||||
OPT_STYLE_DIY,
|
OPT_STYLE_DIY,
|
||||||
DEFAULT_COLOR,
|
DEFAULT_COLOR,
|
||||||
EVENT_KISS,
|
|
||||||
MSG_TRANS_CURRULE,
|
MSG_TRANS_CURRULE,
|
||||||
TRANS_NEWLINE_LENGTH,
|
TRANS_NEWLINE_LENGTH,
|
||||||
} from "../../config";
|
} from "../../config";
|
||||||
@@ -112,11 +111,11 @@ export default function Content({ q, translator }) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
window.addEventListener(EVENT_KISS, handleKissEvent);
|
window.addEventListener(translator.eventName, handleKissEvent);
|
||||||
return () => {
|
return () => {
|
||||||
window.removeEventListener(EVENT_KISS, handleKissEvent);
|
window.removeEventListener(translator.eventName, handleKissEvent);
|
||||||
};
|
};
|
||||||
}, []);
|
}, [translator.eventName]);
|
||||||
|
|
||||||
if (loading) {
|
if (loading) {
|
||||||
return (
|
return (
|
||||||
|
|||||||
Reference in New Issue
Block a user