Skip to content
chrisgoringe edited this page Sep 25, 2023 · 5 revisions

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)

	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: () => { myFunction(node); }           // and a callback
			},
                	null                                                    // (don't think this is needed for splice?)
		);
		return options;                                                 // and return the options
	};  

Clone this wiki locally