← back to Eur Recrawl
probe-productcode.mjs
35 lines
// probe-productcode.mjs [CODE] — the trade portal product-code lookup: data + images?
import { chromium } from 'playwright';
import path from 'node:path';
const code = process.argv[2] || 'W7216';
const ctx = await chromium.launchPersistentContext(path.join(process.cwd(), '.auth', 'osbtrade'), {
headless: false, channel: 'chrome', viewport: { width: 1440, height: 900 },
});
const page = ctx.pages()[0] || await ctx.newPage();
await page.goto('https://tradenew.osborneandlittle.com/user/stock-enquiry/productcode', { waitUntil: 'domcontentloaded', timeout: 60000 }).catch((e) => console.log('nav', e.message));
await page.waitForTimeout(3000);
console.log('page:', (await page.title().catch(() => '')).slice(0, 40), '@', page.url());
const inputs = await page.evaluate(() => [...document.querySelectorAll('input,select,button')].map((e) => ({ t: e.tagName, type: e.type || '', name: e.name || '', id: e.id || '', ph: e.placeholder || '', txt: (e.innerText || e.value || '').slice(0, 15) })).filter((e) => e.type !== 'hidden').slice(0, 15));
console.log('fields:', JSON.stringify(inputs));
const box = await page.$('input[type=text]:not([name*=user i]), input[name*=code i], input[name*=product i], input[name*=search i], input[id*=code i]');
if (box) {
await box.fill(code).catch(() => {});
const go = await page.$('button:has-text("Search"), button:has-text("Enquiry"), button[type=submit], input[type=submit]');
if (go) await go.click().catch(() => {}); else await page.keyboard.press('Enter').catch(() => {});
await page.waitForTimeout(4500);
const info = await page.evaluate(() => {
const t = document.body ? document.body.innerText : '';
const imgs = [...document.querySelectorAll('img')].map((i) => i.src).filter((s) => /http/.test(s) && !/logo|icon|sprite|flag/i.test(s)).slice(0, 10);
const prices = [...t.matchAll(/(?:£|\$|€)?\s?\d{1,4}\.\d{2}\b/g)].map((m) => m[0].trim()).slice(0, 12);
return { url: location.href, imgs, prices, body: t.replace(/\s+/g, ' ').slice(0, 260) };
});
console.log(`\nsearch "${code}" @ ${info.url}`);
console.log('images:', info.imgs.join('\n ') || '(none)');
console.log('prices:', info.prices.join(' | ') || '(none)');
console.log('body:', info.body);
} else console.log('no search box found');
await page.screenshot({ path: 'probe-productcode.png', fullPage: true }).catch(() => {});
await page.waitForTimeout(1500);
await ctx.close();