ui_2 - chrisgoringe/Comfy-Custom-Node-How-To GitHub Wiki

More on UI extension

I'm indebted to the author of ttN for writing really clear code that I learned a lot from...

Adding to the node menu

In the init() of your extension (code summarized from ttN)

	LGraphCanvas.prototype.myFunction = function(node) {  // add a function to the LGraphCanvas prototype so all nodes get it
		// do something to the node
	}

	const getNodeMenuOptions = LGraphCanvas.prototype.getNodeMenuOptions;   // store the existing method
	LGraphCanvas.prototype.getNodeMenuOptions = function (node) {           // replace it
		const options = getNodeMenuOptions.apply(this, arguments);      // start by calling the stored one
		node.setDirtyCanvas(true, true);                                // force a redraw of (foreground, background)
		options.splice(options.length - 1, 0,                           // splice a new option in at the end 
			{
				content: "My Menu Option",                                      // with a name
				callback: () => { LGraphCanvas.prototype.myFunction(node) ; }   // and the callback 
			},
                	null                                                    // a divider
		);
		return options;                                                 // and return the options
	};