Files
kiss-translator/src/libs/gm.js

103 lines
2.7 KiB
JavaScript
Raw Normal View History

2023-08-29 00:06:50 +08:00
import { fetchGM } from "./fetch";
2023-09-02 15:54:51 +08:00
import { genEventName } from "./utils";
2023-08-29 00:06:50 +08:00
2023-09-02 15:54:51 +08:00
const MSG_GM_xmlHttpRequest = "xmlHttpRequest";
const MSG_GM_setValue = "setValue";
const MSG_GM_getValue = "getValue";
const MSG_GM_deleteValue = "deleteValue";
const MSG_GM_info = "info";
2023-09-02 14:14:27 +08:00
2023-08-29 00:06:50 +08:00
/**
* 注入页面的脚本请求并接受GM接口信息
* @param {*} param0
*/
export const injectScript = (ping) => {
2023-09-02 15:54:51 +08:00
window.APP_INFO = {
name: process.env.REACT_APP_NAME,
version: process.env.REACT_APP_VERSION,
eventName: ping,
};
};
2023-08-29 00:06:50 +08:00
2023-09-02 15:54:51 +08:00
/**
* 适配GM脚本
*/
export const adaptScript = (ping) => {
2023-08-29 00:06:50 +08:00
const promiseGM = (action, args, timeout = 5000) =>
2023-08-28 17:59:51 +08:00
new Promise((resolve, reject) => {
2023-09-02 15:54:51 +08:00
const pong = genEventName();
2023-08-28 17:59:51 +08:00
const handleEvent = (e) => {
2023-08-29 00:06:50 +08:00
window.removeEventListener(pong, handleEvent);
2023-08-28 17:59:51 +08:00
const { data, error } = e.detail;
if (error) {
reject(new Error(error));
} else {
resolve(data);
}
};
2023-08-29 00:06:50 +08:00
window.addEventListener(pong, handleEvent);
2023-08-28 17:59:51 +08:00
window.dispatchEvent(
2023-08-29 00:06:50 +08:00
new CustomEvent(ping, { detail: { action, args, pong } })
2023-08-28 17:59:51 +08:00
);
setTimeout(() => {
2023-08-29 00:06:50 +08:00
window.removeEventListener(pong, handleEvent);
reject(new Error("timeout"));
2023-08-28 17:59:51 +08:00
}, timeout);
});
2023-08-29 00:06:50 +08:00
window.KISS_GM = {
fetch: (input, init) => promiseGM(MSG_GM_xmlHttpRequest, { input, init }),
setValue: (key, val) => promiseGM(MSG_GM_setValue, { key, val }),
getValue: (key) => promiseGM(MSG_GM_getValue, { key }),
deleteValue: (key) => promiseGM(MSG_GM_deleteValue, { key }),
2023-09-02 15:54:51 +08:00
getInfo: async () => {
if (!window.GM_info) {
window.GM_info = await promiseGM(MSG_GM_info);
2023-08-29 00:06:50 +08:00
}
2023-09-02 15:54:51 +08:00
return window.GM_info;
2023-08-29 00:06:50 +08:00
},
};
};
/**
* 监听并回应页面对GM接口的请求
* @param {*} param0
*/
export const handlePing = async (e) => {
const { action, args, pong } = e.detail;
let res;
try {
switch (action) {
case MSG_GM_xmlHttpRequest:
const { input, init } = args;
res = await fetchGM(input, init);
break;
case MSG_GM_setValue:
const { key, val } = args;
await GM.setValue(key, val);
res = val;
break;
case MSG_GM_getValue:
res = await GM.getValue(args.key);
break;
case MSG_GM_deleteValue:
await GM.deleteValue(args.key);
res = "ok";
break;
case MSG_GM_info:
res = GM.info;
break;
default:
throw new Error(`message action is unavailable: ${action}`);
}
window.dispatchEvent(new CustomEvent(pong, { detail: { data: res } }));
} catch (err) {
window.dispatchEvent(
new CustomEvent(pong, { detail: { error: err.message } })
);
}
2023-08-28 17:59:51 +08:00
};