- Delete 7 dead-squat widgets (registered UI only for other packs' nodes: UniRig/SAM3D/Mocap/FBXPreview/BVH/pointcloud) + their 6 orphaned viewer HTMLs + the entire unused js/viewer tree (21 files, imported by nothing; only ui/styles.css survives) + unused utils barrel. - Rename namespace geompack -> geometrypack (= DisplayName, lowercased); all 17 surviving extensions now geometrypack.*. - Add event.source provenance guards to every message listener (2 inline, 2 factory handlers via new iframe param). - Stop touching document.body: upload file input stays detached (click() works detached in modern browsers); folder autocomplete dropdown becomes a manual popover inside the widget's own DOM (top layer escapes canvas transforms without shared-DOM appends). Passes comfy-test's javascript isolation lint with 0 errors.
76 lines
2.6 KiB
JavaScript
76 lines
2.6 KiB
JavaScript
/**
|
|
* Screenshot capture and upload utility for ComfyUI viewers
|
|
*/
|
|
|
|
/**
|
|
* Convert base64 data URL to Blob
|
|
* @param {string} dataUrl - Base64 data URL (e.g., "data:image/png;base64,...")
|
|
* @returns {Blob} PNG image blob
|
|
*/
|
|
export function dataUrlToBlob(dataUrl) {
|
|
const base64Data = dataUrl.split(',')[1];
|
|
const byteString = atob(base64Data);
|
|
const arrayBuffer = new ArrayBuffer(byteString.length);
|
|
const uint8Array = new Uint8Array(arrayBuffer);
|
|
|
|
for (let i = 0; i < byteString.length; i++) {
|
|
uint8Array[i] = byteString.charCodeAt(i);
|
|
}
|
|
|
|
return new Blob([uint8Array], { type: 'image/png' });
|
|
}
|
|
|
|
/**
|
|
* Upload screenshot to ComfyUI backend
|
|
* @param {string} dataUrl - Base64 data URL of the screenshot
|
|
* @param {string} prefix - Filename prefix (e.g., "vtk-screenshot")
|
|
* @returns {Promise<{name: string, subfolder: string, type: string}>} Upload result
|
|
*/
|
|
export async function uploadScreenshot(dataUrl, prefix = 'screenshot') {
|
|
const blob = dataUrlToBlob(dataUrl);
|
|
|
|
// Generate filename with timestamp
|
|
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
|
|
const filename = `${prefix}-${timestamp}.png`;
|
|
|
|
// Create FormData for upload
|
|
const formData = new FormData();
|
|
formData.append('image', blob, filename);
|
|
formData.append('type', 'output'); // Save to output directory
|
|
formData.append('subfolder', ''); // Root of output folder
|
|
|
|
// Upload to ComfyUI backend
|
|
const response = await fetch('/upload/image', {
|
|
method: 'POST',
|
|
body: formData
|
|
});
|
|
|
|
if (!response.ok) {
|
|
throw new Error(`Upload failed: ${response.status} ${response.statusText}`);
|
|
}
|
|
|
|
return response.json();
|
|
}
|
|
|
|
/**
|
|
* Create a message handler for screenshot events from iframe
|
|
* @param {string} prefix - Filename prefix for screenshots
|
|
* @param {HTMLIFrameElement} iframe - Only accept messages from this iframe
|
|
* @param {Function} onError - Error callback
|
|
* @returns {Function} Event handler function
|
|
*/
|
|
export function createScreenshotHandler(prefix = 'screenshot', iframe = null, onError = console.error) {
|
|
return async (event) => {
|
|
// Provenance guard: without it this fires for EVERY pack's iframe
|
|
// messages in the shared page, not just ours.
|
|
if (iframe && event.source !== iframe.contentWindow) return;
|
|
if (event.data.type === 'SCREENSHOT' && event.data.image) {
|
|
try {
|
|
await uploadScreenshot(event.data.image, prefix);
|
|
} catch (error) {
|
|
onError(`Error saving screenshot: ${error}`);
|
|
}
|
|
}
|
|
};
|
|
}
|