Files
PozzettiAndrea 7947ff3f23 Vendor viewer JS, drop comfy-3d-viewers
Vendor the viewer JavaScript into javascript/ and drop the
comfy-3d-viewers dependency. The viewers were previously copied into web/ at
every startup by prestartup_script.py, which made the served frontend depend on
a second package resolving at runtime.

Bundles are vendored as .mjs so ComfyUI's **/*.js auto-import glob skips them:
they are iframe-only and must never enter the shared main browser realm. Only
the widget entry points stay .js.

Also replace comfy_env.copy_files with a local helper. Its two extra features
(frame-derived source dir, silent no-op on a missing source) were unused here,
and the asset seeding now resolves the input directory through
folder_paths.get_input_directory() so it follows ComfyUI Desktop and
--input-directory instead of writing into the code tree.

comfy-test.toml moves to the current schema: [test.workflows] cuda (there is no
"gpu" key -- the accelerator is named by backend, and the old spelling parsed to
an empty list, so CUDA runners silently ran the full unfiltered set), an
explicit [test.platforms] platforms allowlist (per-platform booleans are no
longer supported), and the javascript level.

DisplayName becomes "DepthAnythingV3": the namespace enforced on
registerExtension names is derived from it, and the space in "Depth Anything
V3" made that prefix invalid.

Delete the 21 loose js/viewer/*.js modules -- they are already inside
viewer-bundle-vtk.mjs, so shipping them loose leaked the whole viewer into the
shared realm.
2026-08-22 19:44:03 +02:00

75 lines
2.5 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 {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}`);
}
}
};
}