Last active
September 17, 2023 19:48
-
-
Save polarblau/8f96785b82335dc6ee36f47e648de5a1 to your computer and use it in GitHub Desktop.
Download IKEA shopping cart as CSV
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var productList = document.querySelector( | |
'[data-testid="product_list_product_group_items"]' | |
); | |
var products = productList.querySelectorAll("[class^='product_product']"); | |
const sizeRegex = /(\d+)x(\d+) cm/; | |
const delimiter = ";"; | |
const headers = [ | |
"Article Nr.", | |
"Name", | |
"Description", | |
"Width", | |
"Height", | |
"Quantity", | |
"Price", | |
"Total Price", | |
"Link", | |
]; | |
var csv = headers.join(delimiter) + "\n"; | |
for (var i = 0; i < products.length; ++i) { | |
try { | |
let productName = products[i].querySelector("[itemprop='name']"); | |
let productDescription = products[i].querySelector( | |
"[itemprop='description']" | |
); | |
let productArticleNr = products[i].querySelector( | |
".cart-ingka-product-identifier__value" | |
); | |
let productInfo = products[i].querySelectorAll("[class^='product_informationContainer'] li"); | |
let productLink = products[i].querySelector("a").href; | |
let productPrice = products[i].querySelector( | |
"[class^='pricePerItem_container']" | |
); | |
let productPriceTotal = products[i].querySelector("[class^='price_total']"); | |
let quantity = products[i].querySelector( | |
"[class^='cart-ingka-quantity-stepper__input']" | |
); | |
let quantityVal = quantity ? +quantity.value : 1; | |
let productPriceVal = | |
productPrice.innerText.length > 0 ? +productPrice.innerText : undefined; | |
let productPriceTotalVal = +productPriceTotal.innerText; | |
let priceVal = productPriceVal | |
? productPriceVal | |
: +(productPriceTotalVal / quantityVal); | |
let found = false; | |
let w = "", h = ""; | |
for (const item of productInfo) { | |
const text = item.textContent; | |
const match = text.match(sizeRegex); | |
if (match) { | |
found = true; | |
w = parseInt(match[1], 10); | |
h = parseInt(match[2], 10); | |
break; | |
} | |
} | |
csv += | |
productArticleNr.innerText + | |
delimiter + | |
productName.innerText + | |
delimiter + | |
productDescription.innerText + | |
delimiter + | |
w + | |
delimiter + | |
h + | |
delimiter + | |
quantityVal + | |
delimiter + | |
priceVal.toFixed(2) + | |
delimiter + | |
productPriceTotalVal.toFixed(2) + | |
delimiter + | |
productLink + | |
"\n"; | |
} catch (e) { | |
console.error(e, products[i]); | |
} | |
} | |
const blob = new Blob([csv], { type: "text/csv" }); | |
const url = window.URL.createObjectURL(blob); | |
const a = document.createElement("a"); | |
a.setAttribute("href", url); | |
a.setAttribute("download", "ikea_cart.csv"); | |
a.click(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Extended and updated existing script by @GenieTim.