2022-09-19 09:41:57 +08:00
|
|
|
// Monitors the gallery and sends a browser notification when the leading image is new.
|
|
|
|
|
|
|
|
let lastHeadImg = null;
|
|
|
|
|
2023-05-01 03:08:52 +08:00
|
|
|
let notificationButton = null;
|
2022-09-22 18:15:33 +08:00
|
|
|
|
2023-05-25 14:09:13 +08:00
|
|
|
onAfterUiUpdate(function() {
|
2023-05-17 20:46:58 +08:00
|
|
|
if (notificationButton == null) {
|
|
|
|
notificationButton = gradioApp().getElementById('request_notifications');
|
2022-09-22 18:15:33 +08:00
|
|
|
|
2023-05-17 20:46:58 +08:00
|
|
|
if (notificationButton != null) {
|
2023-05-01 03:12:24 +08:00
|
|
|
notificationButton.addEventListener('click', () => {
|
2023-05-01 03:14:51 +08:00
|
|
|
void Notification.requestPermission();
|
2023-05-17 20:46:58 +08:00
|
|
|
}, true);
|
2022-09-22 18:15:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-29 00:09:37 +08:00
|
|
|
const galleryPreviews = gradioApp().querySelectorAll('div[id^="tab_"] div[id$="_results"] .thumbnail-item > img');
|
2022-09-19 09:41:57 +08:00
|
|
|
|
|
|
|
if (galleryPreviews == null) return;
|
|
|
|
|
|
|
|
const headImg = galleryPreviews[0]?.src;
|
|
|
|
|
|
|
|
if (headImg == null || headImg == lastHeadImg) return;
|
|
|
|
|
|
|
|
lastHeadImg = headImg;
|
|
|
|
|
2022-09-27 04:57:31 +08:00
|
|
|
// play notification sound if available
|
|
|
|
gradioApp().querySelector('#audio_notification audio')?.play();
|
|
|
|
|
2022-09-19 09:41:57 +08: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 05:12:20 +08:00
|
|
|
body: `Generated ${imgs.size > 1 ? imgs.size - opts.return_grid : 1} image${imgs.size > 1 ? 's' : ''}`,
|
2022-09-19 09:41:57 +08:00
|
|
|
icon: headImg,
|
|
|
|
image: headImg,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2023-05-17 20:46:58 +08:00
|
|
|
notification.onclick = function(_) {
|
2022-09-19 09:41:57 +08:00
|
|
|
parent.focus();
|
|
|
|
this.close();
|
|
|
|
};
|
|
|
|
});
|