♻️ 姑且能跑 dev,尚需调试功能

This commit is contained in:
目棃
2024-07-02 23:05:33 +08:00
parent 1214501691
commit 367307029b
66 changed files with 12626 additions and 2887 deletions

View File

@@ -1,13 +1,13 @@
/**
* @file utils/TGClient.ts
* @desc 负责米游社客户端的 callback 处理
* @since Beta v0.4.4
* @since Beta v0.5.0
*/
import { event, invoke } from "@tauri-apps/api";
import { event, core } from "@tauri-apps/api";
import type { Event } from "@tauri-apps/api/event";
import type { UnlistenFn } from "@tauri-apps/api/helpers/event";
import { appWindow, WebviewWindow } from "@tauri-apps/api/window";
import type { UnlistenFn } from "@tauri-apps/api/event";
import { Window } from "@tauri-apps/api/window";
import showSnackbar from "../components/func/snackbar.js";
import TGSqlite from "../plugins/Sqlite/index.js";
@@ -44,11 +44,11 @@ class TGClient {
/**
* @private 窗口实例
* @since Beta v0.3.4
* @type {WebviewWindow}
* @since Beta v0.5.0
* @type {Window}
* @memberof TGClient
*/
private window: WebviewWindow | null;
private window: Window | null;
/**
* @private 模拟路由
@@ -66,7 +66,7 @@ class TGClient {
*/
constructor() {
try {
this.window = WebviewWindow.getByLabel("mhy_client");
this.window = Window.getByLabel("mhy_client");
} catch (error) {
this.window = null;
}
@@ -94,7 +94,7 @@ class TGClient {
/**
* @func callback
* @since Beta v0.4.2
* @since Beta v0.5.0
* @desc 回调函数
* @param {string} callback - 回调函数名
* @param {object} data - 回调数据
@@ -108,7 +108,7 @@ class TGClient {
};
const js = `javascript:mhyWebBridge("${callback}", ${JSON.stringify(response)});`;
console.info(`[callback] ${js}`);
await invoke("execute_js", { label: "mhy_client", js });
await core.invoke("execute_js", { label: "mhy_client", js });
}
/**
@@ -270,13 +270,13 @@ class TGClient {
await this.nullCallback(<TGApp.Plugins.JSBridge.NullArg>argParse);
break;
default:
console.warn(`[${arg.windowLabel}] ${JSON.stringify(argParse)}`);
console.warn(`[${arg.event}] ${JSON.stringify(argParse)}`);
}
}
/**
* @func handleCustomCallback
* @since Beta v0.4.3
* @since Beta v0.5.0
* @desc 处理自定义的 callback
* @param {TGApp.Plugins.JSBridge.Arg<any>} arg - 事件参数
* @returns {Promise<void>} - 返回值
@@ -294,7 +294,7 @@ class TGClient {
const executeJS = `javascript:(function(){
window.location.reload();
})();`;
await invoke("execute_js", { label: "mhy_client", js: executeJS });
await core.invoke("execute_js", { label: "mhy_client", js: executeJS });
await this.loadJSBridge();
break;
}
@@ -380,7 +380,7 @@ class TGClient {
};
document.addEventListener("mousedown", mouseDownListener);
})()`;
await invoke("execute_js", { label: "mhy_client", js: executeJS });
await core.invoke("execute_js", { label: "mhy_client", js: executeJS });
break;
}
default:
@@ -390,7 +390,7 @@ class TGClient {
/**
* @func hideOverlay
* @since Beta v0.3.7
* @since Beta v0.5.0
* @desc 隐藏遮罩
* @returns {Promise<void>}
*/
@@ -401,12 +401,12 @@ class TGClient {
box.remove();
}
})();`;
await invoke("execute_js", { label: "mhy_client", js: executeJS });
await core.invoke("execute_js", { label: "mhy_client", js: executeJS });
}
/**
* @func hideSideBar
* @since Beta v0.3.5
* @since Beta v0.5.0
* @desc 隐藏侧边栏
* @returns {void} - 无返回值
*/
@@ -421,12 +421,12 @@ class TGClient {
document.querySelector('body').appendChild(style);
}
})();`;
await invoke("execute_js", { label: "mhy_client", js: executeJS });
await core.invoke("execute_js", { label: "mhy_client", js: executeJS });
}
/**
* @func loadJSBridge
* @since Beta v0.3.8
* @since Beta v0.5.0
* @desc 加载 JSBridge
* @returns {void} - 无返回值
*/
@@ -438,12 +438,12 @@ class TGClient {
closePage: function() { this.postMessage('{"method":"closePage"}') },
};
})();`;
await invoke("execute_js", { label: "mhy_client", js: executeJS });
await core.invoke("execute_js", { label: "mhy_client", js: executeJS });
}
/**
* @func loadSignIn
* @since Beta v0.4.4
* @since Beta v0.5.0
* @desc 自动检测登录ck
* @returns {Promise<void>}
*/
@@ -465,7 +465,7 @@ class TGClient {
}
}
})();`;
await invoke("execute_js", { label: "mhy_client", js: executeJS });
await core.invoke("execute_js", { label: "mhy_client", js: executeJS });
}
/**
@@ -481,7 +481,7 @@ class TGClient {
/**
* @func open
* @since Beta v0.4.4
* @since Beta v0.5.0
* @desc 打开米游社客户端
* @param {string} func - 方法名
* @param {string} url - url
@@ -491,8 +491,8 @@ class TGClient {
if (url === undefined) url = this.getUrl(func);
this.route = [url];
await TGLogger.Info(`[TGClient][open][${func}] ${url}`);
await invoke<InvokeArg>("create_mhy_client", { func, url });
this.window = WebviewWindow.getByLabel("mhy_client");
await core.invoke<InvokeArg>("create_mhy_client", { func, url });
this.window = Window.getByLabel("mhy_client");
await this.window?.show();
await this.window?.setFocus();
await this.loadJSBridge();
@@ -504,7 +504,7 @@ class TGClient {
/* JSBridge 回调处理 */
/**
* @func closePage
* @since Beta v0.3.9
* @since Beta v0.5.0
* @desc 关闭米游社客户端的页面
* @param {TGApp.Plugins.JSBridge.NullArg} arg - 请求参数
* @returns {void} - 无返回值
@@ -520,7 +520,7 @@ class TGClient {
const executeJS = `javascript:(function(){
window.location.href = '${url}';
})();`;
await invoke("execute_js", { label: "mhy_client", js: executeJS });
await core.invoke("execute_js", { label: "mhy_client", js: executeJS });
await this.loadJSBridge();
}
@@ -618,7 +618,7 @@ class TGClient {
/**
* @func getCookieToken
* @since Beta v0.4.0
* @since Beta v0.5.0
* @desc 获取米游社客户端的 cookie_token
* @param {TGApp.Plugins.JSBridge.Arg<TGApp.Plugins.JSBridge.GetCookieTokenPayload>} arg - 请求参数
* @returns {void} - 无返回值
@@ -648,7 +648,7 @@ class TGClient {
document.cookie = "ltmid_v2=${user.cookie.mid};domain=.mihoyo.com;path=/;expires=Fri, 31 Dec 9999 23:59:59 GMT;";
})();`;
console.info(`[getCookieToken] ${executeJS}`);
await invoke("execute_js", { label: "mhy_client", js: executeJS });
await core.invoke("execute_js", { label: "mhy_client", js: executeJS });
const data = {
cookie_token: user.cookie.cookie_token,
};
@@ -744,7 +744,7 @@ class TGClient {
/**
* @func onClickImg
* @since Beta v0.3.9
* @since Beta v0.5.0
* @desc 点击图片,下载到本地
* @param {TGApp.Plugins.JSBridge.Arg<TGApp.Plugins.JSBridge.OnClickImgPayload>} arg - 方法参数
* @returns {void} - 无返回值
@@ -755,12 +755,12 @@ class TGClient {
const { image_list } = arg.payload;
const image = image_list[0];
const executeJS = this.getSaveImgJS(image.url, image.format);
await invoke("execute_js", { label: "mhy_client", js: executeJS });
await core.invoke("execute_js", { label: "mhy_client", js: executeJS });
}
/**
* @func openApplication
* @since Beta v0.3.9
* @since Beta v0.5.0
* @desc 打开应用
* @param {TGApp.Plugins.JSBridge.Arg<TGApp.Plugins.JSBridge.OpenApplicationPayload>} arg - 方法参数
* @returns {void} - 无返回值
@@ -769,7 +769,8 @@ class TGClient {
arg: TGApp.Plugins.JSBridge.Arg<TGApp.Plugins.JSBridge.OpenApplicationPayload>,
): Promise<void> {
console.log(`[openApplication] ${JSON.stringify(arg.payload)}`);
await appWindow.setFocus();
const appWindow = Window.getByLabel("TeyvatGuide");
await appWindow?.setFocus();
showSnackbar({
text: `不支持的操作OpenApplication(${JSON.stringify(arg.payload)})`,
color: "error",
@@ -784,7 +785,7 @@ class TGClient {
/**
* @func pushPage
* @since Beta v0.3.9
* @since Beta v0.5.0
* @desc 打开米游社客户端的页面
* @param {TGApp.Plugins.JSBridge.Arg<TGApp.Plugins.JSBridge.PushPagePayload>} arg - 方法参数
* @returns {Promise<void>} - 无返回值
@@ -794,7 +795,8 @@ class TGClient {
): Promise<void> {
const res = await parseLink(arg.payload.page, true);
if (!res) {
await appWindow.setFocus();
const appWindow = Window.getByLabel("TeyvatGuide");
await appWindow?.setFocus();
showSnackbar({
text: `未知链接:${arg.payload.page}`,
color: "error",
@@ -814,7 +816,7 @@ class TGClient {
const executeJS = `javascript:(function(){
window.location.href = '${res}';
})();`;
await invoke("execute_js", { label: "mhy_client", js: executeJS });
await core.invoke("execute_js", { label: "mhy_client", js: executeJS });
await this.loadJSBridge();
await this.hideSideBar();
await this.hideOverlay();
@@ -838,7 +840,7 @@ class TGClient {
/**
* @func share
* @since Beta v0.4.4
* @since Beta v0.5.0
* @desc 分享
* @param {TGApp.Plugins.JSBridge.Arg<TGApp.Plugins.JSBridge.SharePayload>} arg - 方法参数
* @returns {Promise<void>} - 无返回值
@@ -849,7 +851,7 @@ class TGClient {
const image = arg.payload.content.image_url;
const format = image.split(".").pop();
const executeJS = this.getSaveImgJS(image, format ?? "png");
await invoke("execute_js", { label: "mhy_client", js: executeJS });
await core.invoke("execute_js", { label: "mhy_client", js: executeJS });
await this.callback(arg.callback, {});
return;
}
@@ -924,7 +926,7 @@ class TGClient {
}
mhyWebBridge("${arg.callback}", {});
})();`;
await invoke("execute_js", { label: "mhy_client", js: executeJS });
await core.invoke("execute_js", { label: "mhy_client", js: executeJS });
return;
}
if (arg.payload.type === "image") {
@@ -952,7 +954,7 @@ class TGClient {
}
mhyWebBridge('${arg.callback}', {});
})();`;
await invoke("execute_js", { label: "mhy_client", js: executeJS });
await core.invoke("execute_js", { label: "mhy_client", js: executeJS });
return;
}
}