From 2ac2f999cf6277525ef08f6e167b919fcee23319 Mon Sep 17 00:00:00 2001 From: taoshihan <630892807@qq.com> Date: Wed, 4 Jan 2023 14:50:27 +0800 Subject: [PATCH] To add browser notifications --- static/js/functions.js | 48 +++++++++++++++++++++++++++++++----------- 1 file changed, 36 insertions(+), 12 deletions(-) diff --git a/static/js/functions.js b/static/js/functions.js index ea4ae87..5347d0d 100644 --- a/static/js/functions.js +++ b/static/js/functions.js @@ -18,28 +18,52 @@ function getWsBaseUrl() { } return url; } +//除去html标签 +function replaceHtml(str){ + return str.replace(/<[^>]*>/g, ''); +} +//浏览器桌面通知 function notify(title, options, callback) { + // 先检查浏览器是否支持 if (!window.Notification) { + console.log("浏览器不支持notify"); return; } - var notification; + options.body=replaceHtml(options.body); + console.log("浏览器notify权限:", Notification.permission); // 检查用户曾经是否同意接受通知 if (Notification.permission === 'granted') { - notification = new Notification(title, options); // 显示通知 - + var notification = new Notification(title, options); // 显示通知 + if (notification && callback) { + notification.onclick = function(event) { + callback(notification, event); + } + setTimeout(function () { + notification.close(); + },3000); + } } else { - var promise = Notification.requestPermission(); + Notification.requestPermission().then( (permission) =>function(){ + console.log("请求浏览器notify权限:", permission); + if (permission === 'granted') { + notification = new Notification(title, options); // 显示通知 + if (notification && callback) { + notification.onclick = function (event) { + callback(notification, event); + } + setTimeout(function () { + notification.close(); + }, 3000); + } + } else if (permission === 'default') { + console.log('用户关闭授权 可以再次请求授权'); + } else { + console.log('用户拒绝授权 不能显示通知'); + } + }); } - if (notification && callback) { - notification.onclick = function(event) { - callback(notification, event); - } - setTimeout(function () { - notification.close(); - },3000); - } } var titleTimer=0; var titleNum=0;