This commit is contained in:
+12
-6
@@ -76,12 +76,18 @@ export async function checkStockStatus(url: string): Promise<ScrapeResult> {
|
||||
const result = await page.evaluate(() => {
|
||||
let status: 'in_stock' | 'sold_out' | 'unknown' = 'unknown';
|
||||
|
||||
// Check all spans for the known button text
|
||||
const spans = document.querySelectorAll('span');
|
||||
for (const span of spans) {
|
||||
const text = span.textContent?.trim();
|
||||
if (text === 'Add to Cart') { status = 'in_stock'; break; }
|
||||
if (text === 'Sold Out') { status = 'sold_out'; break; }
|
||||
// In stock: primary button carries label="Add to Cart" as an attribute.
|
||||
// Why: styled-components class hashes rebuild every deploy, and "Add to Cart"
|
||||
// text alone can appear in related-product cards on the page.
|
||||
if (document.querySelector('button[label="Add to Cart"]')) {
|
||||
status = 'in_stock';
|
||||
} else {
|
||||
const soldOutPhrases = ['Notify me when available', 'Sold Out', 'Out of Stock'];
|
||||
const buttons = document.querySelectorAll('button');
|
||||
for (const btn of buttons) {
|
||||
const text = btn.textContent?.trim();
|
||||
if (text && soldOutPhrases.includes(text)) { status = 'sold_out'; break; }
|
||||
}
|
||||
}
|
||||
|
||||
// Product name: og:title is most reliable for single-product pages
|
||||
|
||||
Reference in New Issue
Block a user