Files
liusida-ComfyUI-Debug/js/string_output.js
T
2024-04-06 19:51:22 +08:00

44 lines
1.5 KiB
JavaScript

import { api } from '../../scripts/api.js';
import { app } from '../../scripts/app.js'; // Assuming `app` exports the current application instance
// Function to update STRING widget's content based on server response
function updateStringWidgetContent(nodeId, widgetName, newContent) {
const node = app.graph._nodes.find(n => n.id === nodeId);
if (!node) {
console.error('Node not found:', nodeId);
return;
}
const widget = node.widgets.find(w => w.name === widgetName && w.type === 'customtext');
if (!widget) {
console.error('STRING widget not found:', widgetName);
return;
}
widget.value = newContent;
// Optionally, if there's a callback function defined for the widget, call it
if (widget.callback) {
widget.callback(newContent);
}
}
// Listen for a specific event from the server indicating a script execution is completed
api.addEventListener('executed', event => {
const detail = event.detail;
// Check if `output` exists and has a `output`
if (detail.output && Array.isArray(detail.output.output)) {
// Join the array elements into a string
const content = detail.output.output.join('');
// Example node ID and widget name, replace with actual ones
const nodeId = parseInt(app.runningNodeId); // Replace with actual node ID
const widgetName = 'output'; // Replace with actual widget name
updateStringWidgetContent(nodeId, widgetName, content);
} else {
console.error('Unexpected detail format:', detail);
}
});