File size: 1,604 Bytes
1e3b872
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { app } from "../../../scripts/app.js";

const notificationSetup = () => {
	if (!("Notification" in window)) {
		console.log("This browser does not support notifications.");
		alert("This browser does not support notifications.");
		return;
	}
	if (Notification.permission === "denied") {
		console.log("Notifications are blocked. Please enable them in your browser settings.");
		alert("Notifications are blocked. Please enable them in your browser settings.");
		return;
	}
	if (Notification.permission !== "granted") {
		Notification.requestPermission();
	}
	return true;
};

app.registerExtension({
	name: "pysssss.SystemNotification",
	async beforeRegisterNodeDef(nodeType, nodeData, app) {
		if (nodeData.name === "SystemNotification|pysssss") {
			const onExecuted = nodeType.prototype.onExecuted;
			nodeType.prototype.onExecuted = async function () {
				onExecuted?.apply(this, arguments);
				const mode = this.widgets.find((w) => w.name === "mode");
				const message = this.widgets.find((w) => w.name === "message");

				if (mode.value === "on empty queue") {
					if (app.ui.lastQueueSize !== 0) {
						await new Promise((r) => setTimeout(r, 500));
					}
					if (app.ui.lastQueueSize !== 0) {
						return;
					}
				}
				if (!notificationSetup()) return;
				const notification = new Notification("ComfyUI", { body: message.value ?? "Your notification has triggered." });
			};

			const onNodeCreated = nodeType.prototype.onNodeCreated;
			nodeType.prototype.onNodeCreated = function () {
				onNodeCreated?.apply(this, arguments);
				notificationSetup();
			};
		}
	},
});