Created
November 9, 2022 23:14
-
-
Save mehdichaouch/0983b9f8bacf743a042ad33d6a4b0cde to your computer and use it in GitHub Desktop.
π§ WIP π· Add price per unit on CrowdFarming website
This file contains hidden or 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
// Script for https://www.crowdfarming.com/ | |
document.querySelectorAll(".project-card-container").forEach(function (element) { | |
var priceRaw = element.querySelector(".ds-oh-card-footer-price-quantity").textContent; | |
var price = priceRaw.replace(",", ".").match(/[+-]?\d+(\.\d+)?/g)[0]; | |
var quantity = ""; | |
if (element.querySelector(".ds-adoption-card-footer-price-weight") !== null) { | |
quantity = element.querySelector(".ds-adoption-card-footer-price-weight").textContent; | |
} else if (element.querySelector(".ds-select-box-selected-option span") !== null) { | |
quantity = element.querySelector(".ds-select-box-selected-option").textContent; | |
} | |
var unit = quantity; | |
quantity = quantity.match(/[+-]?\d+(\.\d+)?/g)[0]; | |
unit = unit.replace(quantity, "").split("/")[0].trim(); | |
var pricePerUnit = price / quantity; | |
element.querySelector(".ds-oh-card-footer-price-quantity").textContent = `${priceRaw} (${pricePerUnit.toFixed(2)}/${unit})`; | |
console.log(price, quantity, `${pricePerUnit.toFixed(2)}/${unit}`); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment