diff --git a/src/decorators/CorrectFinalPrice.ts b/src/decorators/CorrectFinalPrice.ts index a35b1ec..1a46d9a 100644 --- a/src/decorators/CorrectFinalPrice.ts +++ b/src/decorators/CorrectFinalPrice.ts @@ -3,60 +3,39 @@ import { State } from "../types/State"; export class CorrectFinalPrice { private state: State; - private btsPrice: number | undefined = undefined; constructor(state: State) { this.state = state; - this.btsPrice = this.fetchBTSPrice(); } - private fetchBTSPrice() { - const priceElement = document.querySelector(".price"); - return priceElement ? this.priceElementToNumber(priceElement) : undefined; + public async start() { + this.finalPriceFixer(); + this.shippingCostFixer(); } - private priceElementToNumber(element: Element) { - let priceValue = ""; - - const leftPart = element.querySelector("span.comma"); - if (!leftPart?.previousSibling) { - return undefined; - } - - const integerPart = leftPart.previousSibling.textContent; - priceValue = `${priceValue}${integerPart}`; + private finalPriceFixer() { + const finalPriceLabel = document.querySelector("label.toggle-switch-label"); - const rightPart = element.querySelector("span.comma + span"); - if (!rightPart) { - return undefined; + if (finalPriceLabel) { + finalPriceLabel.textContent += this.state.language === Language.ENGLISH ? " (with cash on delivery)" : " (με αντικαταβολή)"; } - - const decimalPart = rightPart.textContent; - priceValue = `${priceValue}.${decimalPart}`; - - return parseFloat(priceValue); } - public async start() { - const BTS_FREE_SHIPPING_THRESHOLD = 20; - - if(this.btsPrice && this.btsPrice < BTS_FREE_SHIPPING_THRESHOLD) { - const priceDiv = document.querySelector("div.price"); - - if (priceDiv) { - const deliverCostDiv = document.createElement("div"); - - deliverCostDiv.classList.add("deliver-cost"); - deliverCostDiv.textContent = this.state.language === Language.ENGLISH ? " (+3,00€ delivery cost)" : " (+3,00€ μεταφορικά)"; - - priceDiv.appendChild(deliverCostDiv); - } + private shippingCostFixer() { + const priceElement = document.querySelector(".price"); + if(!priceElement) { + return; } - const finalPriceLabel = document.querySelector("label.toggle-switch-label"); + const articleEm = document.querySelector("article.offering-card"); + const shippingCostEm = articleEm?.querySelector("em"); + if(!shippingCostEm) { + return; + } - if (finalPriceLabel) { - finalPriceLabel.textContent += this.state.language === Language.ENGLISH ? " (with cash on delivery)" : " (με αντικαταβολή)"; + if (!shippingCostEm.parentElement) { + return; } + priceElement.appendChild(shippingCostEm.parentElement.cloneNode(true)); } } diff --git a/src/decorators/PriceCheckerIndicator.ts b/src/decorators/PriceCheckerIndicator.ts index d2da0e3..06d7c02 100644 --- a/src/decorators/PriceCheckerIndicator.ts +++ b/src/decorators/PriceCheckerIndicator.ts @@ -13,7 +13,7 @@ interface LowestPriceData { export class PriceCheckerIndicator { private state: State; private btsPrice: number | undefined = undefined; - private btsDeliveryCost: number | undefined = undefined; + private btsShippingCost: number | undefined = undefined; private lowestPriceData: LowestPriceData | undefined = undefined; constructor(state: State) { @@ -23,14 +23,18 @@ export class PriceCheckerIndicator { public async start() { const offeringCard = document.querySelector("article.offering-card"); - if (offeringCard) { - this.lowestPriceData = await marketDataReceiver(); - if (this.lowestPriceData) { - this.btsPrice = buyThroughSkroutzRetriever(); - this.btsDeliveryCost = buyThroughSkroutzDeliveryCostRetriever(); - this.insertPriceIndication(offeringCard); - } + if (!offeringCard) { + return; + } + + this.lowestPriceData = await marketDataReceiver(); + if (!this.lowestPriceData) { + return; } + + this.btsPrice = buyThroughSkroutzRetriever(); + this.btsShippingCost = buyThroughSkroutzDeliveryCostRetriever(); + this.insertPriceIndication(offeringCard); } private insertPriceIndication(element: Element): void { @@ -42,7 +46,7 @@ export class PriceCheckerIndicator { const priceIndication = document.createElement("div"); const colFlex = document.createElement("div"); - const deliveryCost = this.btsDeliveryCost ?? 0; + const deliveryCost = this.btsShippingCost ?? 0; let isLowestPrice = false; if (!!this.btsPrice && !!this.lowestPriceData) { isLowestPrice = this.btsPrice + deliveryCost <= this.lowestPriceData.unformatted;