Cart Detection
Functions for extracting shopping cart data from web pages. Import from @snapfill/core.
detectCart()
function detectCart(): AutofillCartInfo | nullDetects cart data using all sources in priority order. Returns the first successful result or null.
Priority: JSON-LD → Microdata → Open Graph → DOM Heuristics
import { detectCart } from '@snapfill/core';
const cart = detectCart();
if (cart) {
console.log(`${cart.products.length} items, total: ${cart.total} cents`);
console.log('Currency:', cart.currency);
console.log('Source:', cart.source);
}Individual Extractors
extractFromJsonLd()
function extractFromJsonLd(): AutofillCartInfo | nullExtracts products from <script type="application/ld+json"> blocks. Handles Product, IndividualProduct, Order, and Invoice types. Supports @graph arrays.
extractFromMicrodata()
function extractFromMicrodata(): AutofillCartInfo | nullExtracts products from schema.org/Product microdata (itemscope/itemprop attributes).
extractFromOpenGraph()
function extractFromOpenGraph(): AutofillCartInfo | nullExtracts product data from Open Graph meta tags (og:type="product").
extractFromDomHeuristics()
function extractFromDomHeuristics(): AutofillCartInfo | nullExtracts cart data by finding cart containers (by class/ID patterns like cart, basket, order-summary) and parsing line items with price regex.
Utilities
priceToCents(priceStr)
function priceToCents(priceStr: string | number): numberParses a price string to an integer in cents. Handles multiple formats:
priceToCents('$29.99'); // → 2999
priceToCents('1,234.56'); // → 123456
priceToCents('29,99'); // → 2999 (EU format)
priceToCents('1.234,56'); // → 123456 (EU format)
priceToCents(89.99); // → 8999detectCurrency(priceStr, metaCurrency)
function detectCurrency(
priceStr: string | null,
metaCurrency: string | null
): string | nullDetects ISO 4217 currency code from a price string or metadata. Falls back to domain-based inference (.co.uk → GBP, .com.au → AUD).