支持打开酒馆内的帖子

This commit is contained in:
BTMuli
2023-12-16 17:41:56 +08:00
parent 1f5daab03f
commit 497c126263
2 changed files with 52 additions and 5 deletions

View File

@@ -69,10 +69,33 @@ pub async fn create_mhy_client(handle: AppHandle, func: String, url: String) {
"open_post" => { "open_post" => {
let window = handle.get_window("mhy_client").unwrap(); let window = handle.get_window("mhy_client").unwrap();
let execute_js = r#"javascript:(async function(){ let execute_js = r#"javascript:(async function(){
let url = window.location.href; let url = new URL(window.location.href);
let arg = { const whiteList = [
method: 'teyvat_open', "bbs.mihoyo.com",
payload: url, "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)); await window.__TAURI__.event.emit('post_mhy_client',JSON.stringify(arg));
})()"#; })()"#;

View File

@@ -9,6 +9,7 @@ import type { Event } from "@tauri-apps/api/event";
import { appWindow, WebviewWindow } from "@tauri-apps/api/window"; import { appWindow, WebviewWindow } from "@tauri-apps/api/window";
import { parseLink } from "./linkParser"; import { parseLink } from "./linkParser";
import { createPost } from "./TGWindow";
import { getDeviceInfo } from "./toolFunc"; import { getDeviceInfo } from "./toolFunc";
import showSnackbar from "../components/func/snackbar"; import showSnackbar from "../components/func/snackbar";
import { useAppStore } from "../store/modules/app"; import { useAppStore } from "../store/modules/app";
@@ -196,6 +197,25 @@ class TGClient {
this.window = WebviewWindow.getByLabel("mhy_client"); this.window = WebviewWindow.getByLabel("mhy_client");
await this.window?.show(); await this.window?.show();
await this.window?.setFocus(); await this.window?.setFocus();
await this.loadJSBridge();
}
/**
* @func handleCustomCallback
* @since Beta v0.3.8
* @desc 处理自定义的 callback
* @param {Event<string>} arg - 事件参数
* @returns {any} - 返回值
*/
async handleCustomCallback(arg: Event<string>): Promise<any> {
const { method, payload } = <NormalArg>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} - 返回值 * @returns {any} - 返回值
*/ */
async handleCallback(arg: Event<string>): Promise<any> { async handleCallback(arg: Event<string>): Promise<any> {
const { method, payload, callback } = <NormalArg>JSON.parse(arg.payload);
if (method.startsWith("teyvat")) {
await this.handleCustomCallback(arg);
return;
}
console.log(`[${arg.windowLabel}] ${arg.payload}`); console.log(`[${arg.windowLabel}] ${arg.payload}`);
await this.hideSideBar(); await this.hideSideBar();
await this.hideOverlay(); await this.hideOverlay();
const { method, payload, callback } = <NormalArg>JSON.parse(arg.payload);
switch (method) { switch (method) {
case "closePage": case "closePage":
await this.closePage(); await this.closePage();