✨ Support website message notification display
This commit is contained in:
@@ -18,6 +18,13 @@ pub struct BinaryDownloadParams {
|
||||
binary: Vec<u8>,
|
||||
}
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
pub struct NotificationParams {
|
||||
title: String,
|
||||
body: String,
|
||||
icon: String,
|
||||
}
|
||||
|
||||
#[command]
|
||||
pub async fn download_file(app: AppHandle, params: DownloadFileParams) -> Result<(), String> {
|
||||
let window: WebviewWindow = app.get_webview_window("pake").unwrap();
|
||||
@@ -71,3 +78,16 @@ pub async fn download_file_by_binary(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[command]
|
||||
pub fn send_notification(app: AppHandle, params: NotificationParams) -> Result<(), String> {
|
||||
use tauri_plugin_notification::NotificationExt;
|
||||
app.notification()
|
||||
.builder()
|
||||
.title(¶ms.title)
|
||||
.body(¶ms.body)
|
||||
.icon(¶ms.icon)
|
||||
.show()
|
||||
.unwrap();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
33
src-tauri/src/inject/event.js
vendored
33
src-tauri/src/inject/event.js
vendored
@@ -187,7 +187,6 @@ document.addEventListener('DOMContentLoaded', () => {
|
||||
};
|
||||
|
||||
const detectAnchorElementClick = e => {
|
||||
|
||||
const anchorElement = e.target.closest('a');
|
||||
|
||||
if (anchorElement && anchorElement.href) {
|
||||
@@ -256,6 +255,38 @@ document.addEventListener('DOMContentLoaded', () => {
|
||||
);
|
||||
});
|
||||
|
||||
document.addEventListener('DOMContentLoaded', function () {
|
||||
let permVal = 'granted';
|
||||
window.Notification = function (title, options) {
|
||||
const { invoke } = window.__TAURI__.core;
|
||||
const body = options?.body || '';
|
||||
let icon = options?.icon || '';
|
||||
|
||||
// If the icon is a relative path, convert to full path using URI
|
||||
if (icon.startsWith('/')) {
|
||||
icon = window.location.origin + icon;
|
||||
}
|
||||
|
||||
invoke('send_notification', {
|
||||
params: {
|
||||
title,
|
||||
body,
|
||||
icon,
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
window.Notification.requestPermission = async () => 'granted';
|
||||
|
||||
Object.defineProperty(window.Notification, 'permission', {
|
||||
enumerable: true,
|
||||
get: () => permVal,
|
||||
set: v => {
|
||||
permVal = v;
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
function setDefaultZoom() {
|
||||
const htmlZoom = window.localStorage.getItem('htmlZoom');
|
||||
if (htmlZoom) {
|
||||
|
||||
@@ -3,7 +3,7 @@ mod app;
|
||||
mod util;
|
||||
|
||||
use app::{invoke, menu::set_system_tray, window};
|
||||
use invoke::{download_file, download_file_by_binary};
|
||||
use invoke::{download_file, download_file_by_binary, send_notification};
|
||||
use std::str::FromStr;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::time::{Duration, Instant};
|
||||
@@ -38,10 +38,12 @@ pub fn run_app() {
|
||||
.plugin(tauri_plugin_oauth::init())
|
||||
.plugin(tauri_plugin_http::init())
|
||||
.plugin(tauri_plugin_shell::init())
|
||||
.plugin(tauri_plugin_notification::init())
|
||||
.plugin(tauri_plugin_single_instance::init(|_, _, _| ()))
|
||||
.invoke_handler(tauri::generate_handler![
|
||||
download_file,
|
||||
download_file_by_binary
|
||||
download_file_by_binary,
|
||||
send_notification,
|
||||
])
|
||||
.setup(move |app| {
|
||||
let data_dir = get_data_dir(app.app_handle(), tauri_config.clone());
|
||||
|
||||
Reference in New Issue
Block a user