diff --git a/src-tauri/src/client.rs b/src-tauri/src/client.rs index 5e72cee7..d73ee686 100644 --- a/src-tauri/src/client.rs +++ b/src-tauri/src/client.rs @@ -69,10 +69,33 @@ pub async fn create_mhy_client(handle: AppHandle, func: String, url: String) { "open_post" => { let window = handle.get_window("mhy_client").unwrap(); let execute_js = r#"javascript:(async function(){ - let url = window.location.href; - let arg = { - method: 'teyvat_open', - payload: url, + let url = new URL(window.location.href); + const whiteList = [ + "bbs.mihoyo.com", + "www.miyoushe.com", + "m.miyoushe.com", + ]; + if(!whiteList.includes(url.hostname)){ + alert(`当前页面不是米游社帖子页面\n${window.location.href}`); + return; + } + if(!url.pathname.includes("/article/") && !url.hash.includes("/article/")){ + alert(`当前页面不是米游社帖子页面\n${window.location.href}`); + return; + } + let postId; + if(url.pathname.includes("/article/")){ + postId = url.pathname.split("/").pop(); + }else{ + postId = url.hash.split("/").pop(); + } + if(typeof postId !== "string"){ + alert("帖子 ID 无效"); + return; + } + const arg = { + method: 'teyvat_open', + payload: postId, } await window.__TAURI__.event.emit('post_mhy_client',JSON.stringify(arg)); })()"#; diff --git a/src/utils/TGClient.ts b/src/utils/TGClient.ts index 8b10f67e..917bc85a 100644 --- a/src/utils/TGClient.ts +++ b/src/utils/TGClient.ts @@ -9,6 +9,7 @@ import type { Event } from "@tauri-apps/api/event"; import { appWindow, WebviewWindow } from "@tauri-apps/api/window"; import { parseLink } from "./linkParser"; +import { createPost } from "./TGWindow"; import { getDeviceInfo } from "./toolFunc"; import showSnackbar from "../components/func/snackbar"; import { useAppStore } from "../store/modules/app"; @@ -196,6 +197,25 @@ class TGClient { this.window = WebviewWindow.getByLabel("mhy_client"); await this.window?.show(); await this.window?.setFocus(); + await this.loadJSBridge(); + } + + /** + * @func handleCustomCallback + * @since Beta v0.3.8 + * @desc 处理自定义的 callback + * @param {Event} arg - 事件参数 + * @returns {any} - 返回值 + */ + async handleCustomCallback(arg: Event): Promise { + const { method, payload } = JSON.parse(arg.payload); + switch (method) { + case "teyvat_open": + createPost(payload); + break; + default: + console.warn(`[${arg.windowLabel}] ${arg.payload}`); + } } /** @@ -206,10 +226,14 @@ class TGClient { * @returns {any} - 返回值 */ async handleCallback(arg: Event): Promise { + const { method, payload, callback } = JSON.parse(arg.payload); + if (method.startsWith("teyvat")) { + await this.handleCustomCallback(arg); + return; + } console.log(`[${arg.windowLabel}] ${arg.payload}`); await this.hideSideBar(); await this.hideOverlay(); - const { method, payload, callback } = JSON.parse(arg.payload); switch (method) { case "closePage": await this.closePage();