stable-diffusion-webui
53 строки · 1.6 Кб
1// Monitors the gallery and sends a browser notification when the leading image is new.
2
3let lastHeadImg = null;4
5let notificationButton = null;6
7onAfterUiUpdate(function() {8if (notificationButton == null) {9notificationButton = gradioApp().getElementById('request_notifications');10
11if (notificationButton != null) {12notificationButton.addEventListener('click', () => {13void Notification.requestPermission();14}, true);15}16}17
18const galleryPreviews = gradioApp().querySelectorAll('div[id^="tab_"] div[id$="_results"] .thumbnail-item > img');19
20if (galleryPreviews == null) return;21
22const headImg = galleryPreviews[0]?.src;23
24if (headImg == null || headImg == lastHeadImg) return;25
26lastHeadImg = headImg;27
28// play notification sound if available29const notificationAudio = gradioApp().querySelector('#audio_notification audio');30if (notificationAudio) {31notificationAudio.volume = opts.notification_volume / 100.0 || 1.0;32notificationAudio.play();33}34
35if (document.hasFocus()) return;36
37// Multiple copies of the images are in the DOM when one is selected. Dedup with a Set to get the real number generated.38const imgs = new Set(Array.from(galleryPreviews).map(img => img.src));39
40const notification = new Notification(41'Stable Diffusion',42{43body: `Generated ${imgs.size > 1 ? imgs.size - opts.return_grid : 1} image${imgs.size > 1 ? 's' : ''}`,44icon: headImg,45image: headImg,46}47);48
49notification.onclick = function(_) {50parent.focus();51this.close();52};53});54