Size
6.0 KB
Version
1.0.1
Created
Jan 21, 2026
Updated
13 days ago
1// ==UserScript==
2// @name Lidl Product Exporter to CSV
3// @description Export promotional products from Lidl to CSV file
4// @version 1.0.1
5// @match https://*.lidl.pl/*
6// @icon https://www.lidl.pl/cdn/assets/logos/1.0.1/lidl-logo-shop-cdn.svg
7// ==/UserScript==
8(function() {
9 'use strict';
10
11 function init() {
12 console.log('Lidl Product Exporter initialized');
13 addExportButton();
14 }
15
16 function addExportButton() {
17 // Wait for the page to load
18 const checkInterval = setInterval(() => {
19 const productGrid = document.querySelector('.product-grid');
20 if (productGrid) {
21 clearInterval(checkInterval);
22 createButton();
23 }
24 }, 1000);
25 }
26
27 function createButton() {
28 // Create export button
29 const button = document.createElement('button');
30 button.textContent = '📥 Eksportuj do CSV';
31 button.style.cssText = `
32 position: fixed;
33 top: 20px;
34 right: 20px;
35 z-index: 10000;
36 background: #0050AA;
37 color: white;
38 border: none;
39 padding: 12px 24px;
40 font-size: 16px;
41 font-weight: bold;
42 border-radius: 8px;
43 cursor: pointer;
44 box-shadow: 0 4px 6px rgba(0, 0, 0, 0.2);
45 transition: background 0.3s;
46 `;
47
48 button.addEventListener('mouseenter', () => {
49 button.style.background = '#003d80';
50 });
51
52 button.addEventListener('mouseleave', () => {
53 button.style.background = '#0050AA';
54 });
55
56 button.addEventListener('click', exportToCSV);
57
58 document.body.appendChild(button);
59 console.log('Export button added to page');
60 }
61
62 async function exportToCSV() {
63 console.log('Starting CSV export...');
64
65 // Find all product boxes
66 const productBoxes = document.querySelectorAll('.product-grid-box');
67 console.log(`Found ${productBoxes.length} products`);
68
69 if (productBoxes.length === 0) {
70 alert('Nie znaleziono produktów do eksportu');
71 return;
72 }
73
74 const products = [];
75
76 productBoxes.forEach((box, index) => {
77 try {
78 // Extract product name
79 const titleElement = box.querySelector('.product-grid-box__title');
80 const name = titleElement ? titleElement.textContent.trim().replace(/\s+/g, ' ') : 'Brak nazwy';
81
82 // Extract current price
83 const priceElement = box.querySelector('.ods-price__value');
84 const currentPrice = priceElement ? priceElement.textContent.trim().replace(/\s+/g, '') : 'Brak ceny';
85
86 // Extract old price
87 const oldPriceElement = box.querySelector('.ods-price__stroke-price');
88 const oldPrice = oldPriceElement ? oldPriceElement.textContent.trim().replace(/\s+/g, ' ').replace('*', '').trim() : 'Brak';
89
90 // Extract discount percentage
91 const discountElement = box.querySelector('.ods-price__box-content-text-el');
92 const discount = discountElement ? discountElement.textContent.trim() : 'Brak';
93
94 // Extract product link
95 const linkElement = box.querySelector('a[href*="/p/"]');
96 const link = linkElement ? 'https://www.lidl.pl' + linkElement.getAttribute('href') : 'Brak linku';
97
98 // Extract label (e.g., "Produkt dnia")
99 const labelElement = box.querySelector('.odsc-tile__label');
100 const label = labelElement ? labelElement.textContent.trim() : 'Brak etykiety';
101
102 products.push({
103 name,
104 currentPrice,
105 oldPrice,
106 discount,
107 label,
108 link
109 });
110
111 console.log(`Product ${index + 1}:`, { name, currentPrice, oldPrice, discount });
112 } catch (error) {
113 console.error(`Error extracting product ${index + 1}:`, error);
114 }
115 });
116
117 // Create CSV content
118 const csvContent = generateCSV(products);
119
120 // Download CSV file
121 downloadCSV(csvContent, 'lidl_produkty_promocyjne.csv');
122
123 console.log('CSV export completed');
124 alert(`Wyeksportowano ${products.length} produktów do pliku CSV`);
125 }
126
127 function generateCSV(products) {
128 // CSV header
129 const header = 'Nazwa produktu,Cena aktualna,Cena poprzednia,Zniżka,Etykieta,Link\n';
130
131 // CSV rows
132 const rows = products.map(product => {
133 return [
134 escapeCSV(product.name),
135 escapeCSV(product.currentPrice),
136 escapeCSV(product.oldPrice),
137 escapeCSV(product.discount),
138 escapeCSV(product.label),
139 escapeCSV(product.link)
140 ].join(',');
141 }).join('\n');
142
143 return header + rows;
144 }
145
146 function escapeCSV(value) {
147 // Escape quotes and wrap in quotes if contains comma, quote, or newline
148 if (value.includes(',') || value.includes('"') || value.includes('\n')) {
149 return '"' + value.replace(/"/g, '""') + '"';
150 }
151 return value;
152 }
153
154 function downloadCSV(content, filename) {
155 // Add BOM for proper UTF-8 encoding in Excel
156 const BOM = '\uFEFF';
157 const blob = new Blob([BOM + content], { type: 'text/csv;charset=utf-8;' });
158 const link = document.createElement('a');
159 const url = URL.createObjectURL(blob);
160
161 link.setAttribute('href', url);
162 link.setAttribute('download', filename);
163 link.style.visibility = 'hidden';
164
165 document.body.appendChild(link);
166 link.click();
167 document.body.removeChild(link);
168
169 console.log('CSV file downloaded:', filename);
170 }
171
172 // Initialize when page loads
173 if (document.readyState === 'loading') {
174 document.addEventListener('DOMContentLoaded', init);
175 } else {
176 init();
177 }
178})();