fix: KöPi - Screenshot mit BoundingBox clip für korrekte Bildgröße
This commit is contained in:
@@ -95,18 +95,33 @@ async function scrapeMarktguru() {
|
||||
});
|
||||
});
|
||||
|
||||
// Bilder via Puppeteer screenshot der img-Elemente
|
||||
// Bilder via Puppeteer: URL direkt aus img src holen und über page.goto laden
|
||||
const imgElements = await page.$$('img.offer-list-item-img');
|
||||
for (let i = 0; i < offers.length; i++) {
|
||||
if (!offers[i].image || !imgElements[i]) continue;
|
||||
try {
|
||||
// Warte bis Bild geladen
|
||||
// Viewport auf Bildgröße setzen damit kein Zoom-Effekt entsteht
|
||||
await page.evaluate(el => new Promise(res => {
|
||||
if (el.complete) return res();
|
||||
if (el.complete && el.naturalWidth > 0) return res();
|
||||
el.onload = res; el.onerror = res;
|
||||
setTimeout(res, 3000);
|
||||
setTimeout(res, 5000);
|
||||
}), imgElements[i]);
|
||||
const b64 = await imgElements[i].screenshot({ encoding: 'base64' });
|
||||
|
||||
// Bounding Box prüfen
|
||||
const box = await imgElements[i].boundingBox();
|
||||
if (!box || box.width < 5 || box.height < 5) {
|
||||
offers[i].image = null;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Screenshot mit clip auf vernünftige Größe
|
||||
const clip = {
|
||||
x: box.x,
|
||||
y: box.y,
|
||||
width: Math.min(box.width, 300),
|
||||
height: Math.min(box.height, 300),
|
||||
};
|
||||
const b64 = await page.screenshot({ encoding: 'base64', clip });
|
||||
offers[i].image = b64 ? `data:image/png;base64,${b64}` : null;
|
||||
} catch { offers[i].image = null; }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user