Files
wehub-resource-sync 9b395f5cc3
E2E Headed Chrome / e2e-headed (macos-15) (push) Has been cancelled
E2E Headed Chrome / e2e-headed (ubuntu-latest) (push) Has been cancelled
E2E Headed Chrome / e2e-headed (windows-latest) (push) Has been cancelled
CI / build (macos-latest) (push) Has been cancelled
CI / build (ubuntu-latest) (push) Has been cancelled
CI / build (windows-latest) (push) Has been cancelled
CI / unit-test (push) Has been cancelled
CI / bun-test (push) Has been cancelled
CI / adapter-test (push) Has been cancelled
CI / smoke-test (macos-latest) (push) Has been cancelled
CI / smoke-test (ubuntu-latest) (push) Has been cancelled
Security Audit / audit (push) Has been cancelled
Build Chrome Extension / build (push) Has been cancelled
Trigger Website Rebuild (Docs Updated) / dispatch (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:39:48 +08:00

74 lines
2.3 KiB
JavaScript

import { cli, Strategy } from '@jackwener/opencli/registry';
import {
getPostDetails,
getRawCode,
locatePreviewElement,
getDefaultOutputPath,
saveBase64File,
} from './_shared.js';
cli({
site: 'uiverse',
name: 'preview',
access: 'read',
description: 'Capture a screenshot of the Uiverse preview element',
domain: 'uiverse.io',
strategy: Strategy.PUBLIC,
browser: true,
navigateBefore: 'https://uiverse.io',
args: [
{ name: 'input', type: 'str', required: true, positional: true, help: 'Uiverse URL or author/slug identifier' },
{ name: 'output', type: 'str', required: false, help: 'Output image path (defaults to a temp file)' },
{ name: 'padding', type: 'int', required: false, default: 8, help: 'Extra padding around the captured preview in pixels' },
],
columns: ['username', 'slug', 'width', 'height', 'output'],
func: async (page, kwargs) => {
const detail = await getPostDetails(page, kwargs.input);
const payload = await getRawCode(page, detail.post.id);
const located = await locatePreviewElement(page, payload.html);
const rect = located.best.rect;
const padding = Math.max(0, Number(kwargs.padding ?? 8));
const clip = {
x: Math.max(0, rect.x - padding),
y: Math.max(0, rect.y - padding),
width: Math.max(1, rect.width + padding * 2),
height: Math.max(1, rect.height + padding * 2),
scale: 1,
};
const shot = await page.cdp('Page.captureScreenshot', {
format: 'png',
clip,
captureBeyondViewport: false,
});
const base64 = typeof shot === 'string' ? shot : shot?.data;
if (!base64) {
throw new Error('CDP screenshot failed: no image data was returned.');
}
const outputPath = kwargs.output || getDefaultOutputPath({
username: detail.username,
slug: detail.slug,
suffix: 'preview',
extension: 'png',
});
const savedPath = await saveBase64File(base64, outputPath);
return {
username: detail.username,
slug: detail.slug,
url: detail.url,
output: savedPath,
width: Math.round(clip.width),
height: Math.round(clip.height),
x: Math.round(clip.x),
y: Math.round(clip.y),
selectorSource: located.best.source,
matchedTag: located.best.tag,
matchedClassName: located.best.className,
postId: detail.post.id,
};
},
});