Path: blob/master/javascript/notification.js
3055 views
// Monitors the gallery and sends a browser notification when the leading image is new.12let lastHeadImg = null;34let notificationButton = null;56onAfterUiUpdate(function() {7if (notificationButton == null) {8notificationButton = gradioApp().getElementById('request_notifications');910if (notificationButton != null) {11notificationButton.addEventListener('click', () => {12void Notification.requestPermission();13}, true);14}15}1617const galleryPreviews = gradioApp().querySelectorAll('div[id^="tab_"] div[id$="_results"] .thumbnail-item > img');1819if (galleryPreviews == null) return;2021const headImg = galleryPreviews[0]?.src;2223if (headImg == null || headImg == lastHeadImg) return;2425lastHeadImg = headImg;2627// play notification sound if available28const notificationAudio = gradioApp().querySelector('#audio_notification audio');29if (notificationAudio) {30notificationAudio.volume = opts.notification_volume / 100.0 || 1.0;31notificationAudio.play();32}3334if (document.hasFocus()) return;3536// Multiple copies of the images are in the DOM when one is selected. Dedup with a Set to get the real number generated.37const imgs = new Set(Array.from(galleryPreviews).map(img => img.src));3839const notification = new Notification(40'Stable Diffusion',41{42body: `Generated ${imgs.size > 1 ? imgs.size - opts.return_grid : 1} image${imgs.size > 1 ? 's' : ''}`,43icon: headImg,44image: headImg,45}46);4748notification.onclick = function(_) {49parent.focus();50this.close();51};52});535455