extractFlipkart() { try { const url = window.location.href; console.log('Flipkart: Starting extraction from:', url); // Extract Product ID let productId = null; // Method 1: From pid parameter const pidMatch = url.match(/[?&]pid=([A-Z0-9]+)/i); if (pidMatch) { productId = pidMatch[1]; } // Method 2: From /p/ path if (!productId) { const pathMatch = url.match(/\/p\/([A-Z0-9]+)/i); if (pathMatch) { productId = pathMatch[1]; } } if (!productId) { console.error('Flipkart: Could not extract product ID'); return null; } console.log('✓ Flipkart: Product ID:', productId); // Get title let title = document.title.split(':')[0].split('|')[0].split('-')[0].trim(); if (!title || title.length < 10) { title = 'Flipkart Product ' + productId; } // SCRAPE PREVIEW DATA (for popup display) let previewPrice = null; const priceSelectors = [ 'div._30jeq3._16Jk6d', 'div._30jeq3', 'div._16Jk6d', 'div[class*="price"]' ]; for (const selector of priceSelectors) { const priceEl = document.querySelector(selector); if (priceEl) { const priceText = priceEl.textContent; const match = priceText.match(/₹([\d,]+)/); if (match) { previewPrice = parseFloat(match[1].replace(/,/g, '')); console.log('Flipkart: Preview price found:', previewPrice); break; } } } // Get preview image const previewImages = []; const imgSelectors = [ 'img._396cs4._2amPTt', 'img[class*="product"]', 'div._1AtVbE img' ]; for (const selector of imgSelectors) { const imgs = document.querySelectorAll(selector); imgs.forEach(img => { const src = img.src || img.getAttribute('data-src'); if (src && src.includes('flipkart.com') && !previewImages.includes(src)) { const hqSrc = src.replace(/\/\d+\/\d+\//, '/800/800/'); previewImages.push(hqSrc); } }); if (previewImages.length > 0) break; } console.log('Flipkart: Preview images found:', previewImages.length); return { title: title, price: previewPrice, // For preview images: previewImages, // For preview flipkart_product_id: productId, // For WordPress API use_api: true, // Signal to use API button_text: 'Buy on Flipkart', }; } catch (error) { console.error('Flipkart extraction error:', error); return null; } }
