Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

experience for Tauri and Bilibili Api #4

Open
pkptzx opened this issue Jan 28, 2023 · 0 comments
Open

experience for Tauri and Bilibili Api #4

pkptzx opened this issue Jan 28, 2023 · 0 comments

Comments

@pkptzx
Copy link

pkptzx commented Jan 28, 2023

只在发布的版本禁止右键
  if (import.meta.env.PROD) {
    document.addEventListener('contextmenu', (event) => event.preventDefault());
  }
屏蔽一些浏览器默认的快捷键

比如:ctrl+p,ctrl+u,ctrl+f,ctrl+g

  document.onkeydown = function(event) {
    if(event.ctrlKey == true && event.code == 'KeyP') {
        console.log('Ctrl + p');
        event.preventDefault();
    }
    if(event.ctrlKey == true && event.code == 'KeyU') {
        console.log('Ctrl + u');
        event.preventDefault();
    }
    if(event.ctrlKey == true && event.code == 'KeyG') {
        console.log('Ctrl + g');
        event.preventDefault();
    }
}
显示bilibili头像

由于bilibili的头像防盗链是验证了referrer

<meta name="referrer" content="no-referrer">
显示bilibili emoji表情

emoji在info[0][15].extra中,解完替换弹幕文本,以下为示例:

msg.body.contentHtml = msg.body.content
//解emoji表情的地址
const extra = JSON.parse(msg.raw.info[0][15].extra)
if(extra.emots){
  // 替换emoji表情
  console.log(extra.emots)
  for(let key in extra.emots){
    let reg = new RegExp(key.replace('[','\\[').replace(']','\\]'),'gi')
    msg.body.contentHtml = msg.body.contentHtml.replaceAll(reg, `<img style="width: 20px; height: 20px;" src="${extra.emots[key].url}" />`)
  }
}
发送bilibili 图片表情弹幕

通过判断当前弹幕信息是否只是图片表情,如果是则替换为表情ID,示例:

// dynamic_emojis的来源为https://api.live.bilibili.com/xlive/web-ucenter/v2/emoticon/GetEmoticons?platform=pc&room_id=8094023
// 然后整理成格式如:
// const dynamic_emojis=[
//  {
//      "emoji": "赞",
//      "emoticon_unique": "official_147"
//  },
//  {
//      "emoji": "妙啊",
//      "emoticon_unique": "official_109"
//  },
const dynamic_emoji = dynamic_emojis.find(emj=>emj.emoji==msg);
const dm_type = dynamic_emoji ? '1':'0';
msg = dynamic_emoji ? dynamic_emoji.emoticon_unique : msg;
body:{
...
'dm_type': dm_type,//是否为通用图片表情,2333,点赞,颠个勺等等的这种
'msg':msg,
...
}
//发送时,输入妙啊、赞等这种,会自动转换为图片表情
Rust侧实现鼠标穿透与取消穿透(Windows)

通常用于桌面字幕或歌词
首先添加windows-rs依赖

[dependencies.windows]
version = "0.44.0"
features = [
    "Data_Xml_Dom",
    "Win32_Foundation",
    "Win32_Security",
    "Win32_System_Threading",
    "Win32_UI_WindowsAndMessaging",
    "Win32_Security_Cryptography",
    "Foundation",
    "Storage_Streams",
    "Security_Cryptography_DataProtection",
    "Win32_System_WinRT",
    "Win32_System_Services",
    "Win32_System_Memory_NonVolatile",
]

在Rust代码中实现鼠标对窗体的穿透与取消

use tauri::Manager;
use windows::Win32::{UI::WindowsAndMessaging::{GWL_EXSTYLE, WS_EX_TRANSPARENT, WS_EX_LAYERED}};
#[tauri::command]
pub async fn window_mouse_penetration(app_handle: tauri::AppHandle,window: tauri::Window,label: Option<String>)-> tauri::Result<i32>{
    match label {
        Some(lbl) => {
            let win = app_handle.get_window(&lbl);
            if let Some(w) = win {
                Ok(mouse_penetration(w.hwnd().unwrap().0))
            }else {        
                Err(tauri::Error::FailedToExecuteApi(tauri::api::Error::Command(format!("Window {} not found -9999",lbl))))
            }
        },
        None => {
            Ok(mouse_penetration(window.hwnd().unwrap().0))
        },
    }
}

#[tauri::command]
pub async fn window_cancel_mouse_penetration(app_handle: tauri::AppHandle,window: tauri::Window,label: Option<String>)-> tauri::Result<i32>{
    match label {
        Some(lbl) => {
            let win = app_handle.get_window(&lbl);
            if let Some(w) = win {
                Ok(cancel_mouse_penetration(w.hwnd().unwrap().0))
            }else {        
                Err(tauri::Error::FailedToExecuteApi(tauri::api::Error::Command(format!("Window {} not found -9999",lbl))))
            }
        },
        None => {
            Ok(cancel_mouse_penetration(window.hwnd().unwrap().0))
        },
    }
}

fn mouse_penetration(hwnd: isize)->i32{
    unsafe{
        let extended_style = windows::Win32::UI::WindowsAndMessaging::GetWindowLongW(windows::Win32::Foundation::HWND(hwnd), GWL_EXSTYLE);
        windows::Win32::UI::WindowsAndMessaging::SetWindowLongW(windows::Win32::Foundation::HWND(hwnd), GWL_EXSTYLE, extended_style  | WS_EX_TRANSPARENT.0  as i32 | WS_EX_LAYERED.0  as i32)
    }
}
fn cancel_mouse_penetration(hwnd: isize)->i32{
    unsafe{
        let extended_style = windows::Win32::UI::WindowsAndMessaging::GetWindowLongW(windows::Win32::Foundation::HWND(hwnd), GWL_EXSTYLE);
        windows::Win32::UI::WindowsAndMessaging::SetWindowLongW(windows::Win32::Foundation::HWND(hwnd), GWL_EXSTYLE, extended_style  & !WS_EX_TRANSPARENT.0  as i32 & !WS_EX_LAYERED.0  as i32)
    }
}

//最后记得在Tauri中注册命令
...
.invoke_handler(tauri::generate_handler![commands::window_mouse_penetration,commands::window_cancel_mouse_penetration])
...

前端调用:

invoke('window_mouse_penetration',{'label' : 'subtitles'})
invoke('window_cancel_mouse_penetration',{'label' : 'subtitles'})
// label省略表示当前窗体
invoke("window_mouse_penetration").then(r=>console.log(`鼠标穿透设置结果:${r}`))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant