Prevent empty MAX media placeholders

This commit is contained in:
sevenhill
2026-07-06 14:40:25 +03:00
parent 057d4346fe
commit df429d5c43
2 changed files with 179 additions and 55 deletions
+68 -45
View File
@@ -1302,10 +1302,10 @@ async function downloadDomStickerThroughScreenshot(p, request) {
const resolved = [];
for (const handle of handles) {
const visual = await visualHandleFor(handle);
if (visual !== handle) {
await handle.dispose().catch(() => {});
}
resolved.push(visual);
if (visual !== handle) {
resolved.push(handle);
}
}
return resolved;
};
@@ -1557,10 +1557,14 @@ async function downloadDomStickerThroughScreenshot(p, request) {
}
const sorted = (strict.length ? strict : candidates)
.sort((a, b) => b.meta.score - a.meta.score || b.meta.area - a.meta.area || b.meta.y - a.meta.y);
const target = sorted[Math.max(0, Math.min(requestedIndex, sorted.length - 1))];
if (!target) {
if (sorted.length === 0) {
return null;
}
const selectedIndex = Math.max(0, Math.min(requestedIndex, sorted.length - 1));
const orderedTargets = [
...sorted.slice(selectedIndex),
...sorted.slice(0, selectedIndex)
];
const screenshotClip = async (box) => {
if (!Number.isFinite(box?.x) ||
@@ -1590,47 +1594,57 @@ async function downloadDomStickerThroughScreenshot(p, request) {
});
};
const metaBox = {
x: target.meta.x,
y: target.meta.y,
width: target.meta.width,
height: target.meta.height
};
const currentBoxOrMeta = async () => {
const box = await target.handle.boundingBox().catch(() => null);
return box && box.width > 0 && box.height > 0 ? box : metaBox;
};
const captureErrors = [];
try {
await target.handle.scrollIntoViewIfNeeded({ timeout: 3000 }).catch(() => {});
const canvasCapture = await captureCanvasPng(target.handle);
if (canvasCapture.buffer) {
return await stickerCaptureResult(canvasCapture.buffer);
for (const target of orderedTargets) {
const metaBox = {
x: target.meta.x,
y: target.meta.y,
width: target.meta.width,
height: target.meta.height
};
const currentBoxOrMeta = async () => {
const box = await target.handle.boundingBox().catch(() => null);
return box && box.width > 0 && box.height > 0 ? box : metaBox;
};
try {
await target.handle.scrollIntoViewIfNeeded({ timeout: 3000 }).catch(() => {});
const canvasCapture = await captureCanvasPng(target.handle);
if (canvasCapture.buffer) {
return await stickerCaptureResult(canvasCapture.buffer);
}
let buffer;
if (request?.acceptSmall) {
buffer = await screenshotClip(await currentBoxOrMeta());
if (!buffer) {
throw new Error(`${kindLabel} clip was not visible.`);
}
} else {
try {
buffer = await target.handle.screenshot({ type: "png", timeout: 5000 });
} catch (error) {
buffer = await screenshotClip(await currentBoxOrMeta());
if (!buffer) {
throw error;
}
}
}
if (!buffer) {
buffer = await target.handle.screenshot({ type: "png", timeout: 5000 });
}
return await stickerCaptureResult(buffer);
} catch (error) {
captureErrors.push(error?.message || String(error));
}
}
let buffer;
if (request?.acceptSmall) {
buffer = await screenshotClip(await currentBoxOrMeta());
if (!buffer) {
throw new Error(`${kindLabel} clip was not visible.`);
}
} else {
try {
buffer = await target.handle.screenshot({ type: "png", timeout: 5000 });
} catch (error) {
buffer = await screenshotClip(await currentBoxOrMeta());
if (!buffer) {
throw error;
}
}
if (captureErrors.length > 0) {
throw new Error(`${kindLabel} was not captured: ${captureErrors.slice(0, 5).join("; ")}`);
}
if (!buffer) {
try {
buffer = await target.handle.screenshot({ type: "png", timeout: 5000 });
} catch (error) {
throw error;
}
}
return await stickerCaptureResult(buffer);
return null;
} finally {
for (const item of candidates) {
await item.handle.dispose().catch(() => {});
@@ -1639,17 +1653,26 @@ async function downloadDomStickerThroughScreenshot(p, request) {
};
const index = Number.isFinite(Number(request?.index)) ? Number(request.index) : 0;
let testIdCaptureError = null;
if (testId) {
const handles = await p.$$(selectorForTestId(testId));
const result = await screenshotCandidate(await resolveVisualHandles(handles), 0);
if (result) {
return result;
try {
const result = await screenshotCandidate(await resolveVisualHandles(handles), 0);
if (result) {
return result;
}
} catch (error) {
testIdCaptureError = error;
}
}
const handles = await p.$$(fallbackSelector);
const result = await screenshotCandidate(await resolveVisualHandles(handles), index);
if (!result) {
if (testIdCaptureError) {
throw testIdCaptureError;
}
throw new Error(`${kindLabel} was not found.`);
}