Skip to content

Instantly share code, notes, and snippets.

@mehdichaouch
Created November 9, 2022 23:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mehdichaouch/0983b9f8bacf743a042ad33d6a4b0cde to your computer and use it in GitHub Desktop.
Save mehdichaouch/0983b9f8bacf743a042ad33d6a4b0cde to your computer and use it in GitHub Desktop.
🚧 WIP πŸ‘· Add price per unit on CrowdFarming website
// 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