2022-09-19 04:41:57 +03:00
|
|
|
// Monitors the gallery and sends a browser notification when the leading image is new.
|
|
|
|
|
|
|
|
let lastHeadImg = null;
|
|
|
|
|
2022-09-22 13:15:33 +03:00
|
|
|
notificationButton = null
|
|
|
|
|
2022-09-19 04:41:57 +03:00
|
|
|
onUiUpdate(function(){
|
2022-09-22 13:15:33 +03:00
|
|
|
if(notificationButton == null){
|
|
|
|
notificationButton = gradioApp().getElementById('request_notifications')
|
|
|
|
|
|
|
|
if(notificationButton != null){
|
|
|
|
notificationButton.addEventListener('click', function (evt) {
|
|
|
|
Notification.requestPermission();
|
|
|
|
},true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-23 00:31:49 +03:00
|
|
|
const galleryPreviews = gradioApp().querySelectorAll('div[id^="tab_"][style*="display: block"] div[id$="_results"] img.h-full.w-full.overflow-hidden');
|
2022-09-19 04:41:57 +03:00
|
|
|
|
|
|
|
if (galleryPreviews == null) return;
|
|
|
|
|
|
|
|
const headImg = galleryPreviews[0]?.src;
|
|
|
|
|
|
|
|
if (headImg == null || headImg == lastHeadImg) return;
|
|
|
|
|
|
|
|
lastHeadImg = headImg;
|
|
|
|
|
2022-09-26 23:57:31 +03:00
|
|
|
// play notification sound if available
|
|
|
|
gradioApp().querySelector('#audio_notification audio')?.play();
|
|
|
|
|
2022-09-19 04:41:57 +03:00
|
|
|
if (document.hasFocus()) return;
|
|
|
|
|
|
|
|
// Multiple copies of the images are in the DOM when one is selected. Dedup with a Set to get the real number generated.
|
|
|
|
const imgs = new Set(Array.from(galleryPreviews).map(img => img.src));
|
|
|
|
|
|
|
|
const notification = new Notification(
|
|
|
|
'Stable Diffusion',
|
|
|
|
{
|
2022-10-13 00:12:20 +03:00
|
|
|
body: `Generated ${imgs.size > 1 ? imgs.size - opts.return_grid : 1} image${imgs.size > 1 ? 's' : ''}`,
|
2022-09-19 04:41:57 +03:00
|
|
|
icon: headImg,
|
|
|
|
image: headImg,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
notification.onclick = function(_){
|
|
|
|
parent.focus();
|
|
|
|
this.close();
|
|
|
|
};
|
|
|
|
});
|