Skip to content

Instantly share code, notes, and snippets.

@luk4s
Last active November 8, 2020 11:27
Show Gist options
  • Save luk4s/43282124c8666eed78b0084f861832b6 to your computer and use it in GitHub Desktop.
Save luk4s/43282124c8666eed78b0084f861832b6 to your computer and use it in GitHub Desktop.
Rohlik.cz - stažení rozbalené objednávky
/**
* @class OrderItem
* Item in order detail in user profile
*
* @see https://www.rohlik.cz/uzivatel/profil
*/
class OrderItem {
/** @param {Element} element */
constructor(element) {
this._name = element.querySelector("a.productName span:first-child").innerText;
this._quantity = Number.parseFloat(element.querySelector("a.productName span.boldText").innerText);
this._price = Number.parseFloat(element.querySelector(".productInfo > span:last-child").innerText.replace(",","."));
}
/** @return {String} */
get name() {
return this._name;
}
/** @return {Number} */
get quantity() {
return this._quantity;
}
/** @return {Number} */
get price() {
return this._price;
}
/** @return {Number} */
get unitPrice() {
return this.price / this.quantity;
}
/**
* @return {String}
*/
toCsv() {
const row = [this.csvValue(this.name), this.quantity, this.unitPrice, this.price];
return row.join(",");
}
csvValue(text){
return `"${text}"`
}
}
const orderRows = document.querySelectorAll(".collapseItemContent span.collapseInside");
const items = [];
orderRows.forEach((item, index) => {
items.push(new OrderItem(item))
})
/**
* @param {Array<OrderItem,CartItem>} items
*/
function downloadCSV(items) {
const csv = ["Nazev,Mnozstvi,Jed.cena,Clk.cena"];
items.forEach((item) => {
csv.push(item.toCsv());
})
const csvFile = new Blob([csv.join("\n")], {type:"text/csv"});
const downloadLink = document.createElement("a");
downloadLink.download = "rohlik.cz-objednavka.csv";
downloadLink.href = window.URL.createObjectURL(csvFile);
downloadLink.style.display = "none";
document.body.appendChild(downloadLink);
downloadLink.click();
}
downloadCSV(items);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment