From 03a4f2012496c168e0b9fd751f284759c09b2369 Mon Sep 17 00:00:00 2001 From: Dicken Date: Thu, 9 Jul 2026 12:31:27 +0200 Subject: [PATCH] =?UTF-8?q?fix:=20K=C3=B6Pi=20-=20Screenshot=20mit=20Bound?= =?UTF-8?q?ingBox=20clip=20f=C3=BCr=20korrekte=20Bildgr=C3=B6=C3=9Fe?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/src/tools/koepi/routes.js | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/backend/src/tools/koepi/routes.js b/backend/src/tools/koepi/routes.js index a31bb3e..7a0f618 100644 --- a/backend/src/tools/koepi/routes.js +++ b/backend/src/tools/koepi/routes.js @@ -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; } }