ai cooking
1// Extract product name
2let product_name = $('[data-isproduct-tracking-disabled]').text_sane();
3
4// Extract brand from product name (first word if it contains "Organic" or similar)
5let brand = null;
6if (product_name) {
7 let words = product_name.split(' ');
8 if (words[0] && (words[0].toLowerCase() === 'organic' || /^[A-Z]/.test(words[0]))) {
9 brand = words[0];
10 }
11}
12
13// Extract currency symbol from price text
14let price_element = $('.product-comp-v1__price__text span').eq(1);
15let price_text = price_element.text_sane();
16let currency_symbol = null;
17let currency_code = 'USD'; // default
18
19if (price_text) {
20 // Extract currency symbol (any non-digit, non-decimal character at start)
21 let symbol_match = price_text.match(/^([^\d.,]+)/);
22 if (symbol_match) {
23 currency_symbol = symbol_match[1].trim();
24 // Map common symbols to currency codes
25 const currency_map = {
26 '$': 'USD',
27 '€': 'EUR',
28 '£': 'GBP',
29 '¥': 'JPY',
30 '₹': 'INR',
31 'C$': 'CAD',
32 'A$': 'AUD'
33 };
34 currency_code = currency_map[currency_symbol] || 'USD';
35 }
36}
37
38// Extract price - remove currency symbol and parse
39let price = null;
40if (price_text) {
41 let price_value = parseFloat(price_text.replace(/[^\d.]/g, ''));
42 if (!isNaN(price_value)) {
43 price = new Money(price_value, currency_code);
44 }
45}
46
47// Extract price per unit
48let price_per_unit_element = $('.color-neutral-80.body-text-xxs span').eq(1);
49let price_per_unit_text = price_per_unit_element.text_sane();
50let price_per_unit = null;
51if (price_per_unit_text) {
52 // Extract numeric value from text like "($7.99 / Lb)"
53 let match = price_per_unit_text.match(/([\d.]+)/);
54 if (match) {
55 let price_per_unit_value = parseFloat(match[1]);
56 if (!isNaN(price_per_unit_value)) {
57 price_per_unit = new Money(price_per_unit_value, currency_code);
58 }
59 }
60}
61
62// Extract quantity from product name
63let quantity = null;
64if (product_name) {
65 let match = product_name.match(/(\d+\s*(?:Lb|Oz|oz|lb|kg|g|ml|L))/i);
66 if (match) {
67 quantity = match[1];
68 }
69}
70
71// Extract delivery availability - use eq(0) to get only the first matching element
72let availability_delivery = $('.product-details__product-shopping-options table tr[aria-label*="Delivery"] .product-details__product-shopping-options__availability').eq(0).text_sane();
73
74// Extract pickup availability - use eq(0) to get only the first matching element
75let availability_pickup = $('.product-details__product-shopping-options table tr[aria-label*="Pickup"] .product-details__product-shopping-options__availability').eq(0).text_sane();
76
77return {
78 product_name,
79 brand,
80 price,
81 price_per_unit,
82 quantity,
83 availability_delivery,
84 availability_pickup
85};