AliExpress Total Price

Display total price (including shipping) on AliExpress item pages

// ==UserScript==
// @name        AliExpress Total Price
// @namespace   forked_bytes
// @match       https://*.aliexpress.com/item/*
// @match       https://*.aliexpress.us/item/*
// @grant       none
// @version     1.6
// @author      forked_bytes
// @license     0BSD
// @description Display total price (including shipping) on AliExpress item pages
// ==/UserScript==

const total = document.createElement('h2');

setInterval(function() {
  const priceElement = document.querySelector('.product-price-current, .dcss-price-first');
  const shippingElement = document.querySelector('.dynamic-shipping-line, div[ae_button_type="sku_shipmethod_click"]');
  const quantityElement = document.querySelector('div[class*="quantity--picker"] input');

  const [price, prefix, suffix] = parse(priceElement?.textContent);
  const [shipping] = shippingElement?.textContent.includes(':') ? parse(shippingElement?.textContent) : [0];
  const quantity = parseInt(quantityElement?.value) || 1;

  if (price) total.textContent = `Σ ${prefix}${(price * quantity + shipping).toFixed(2)}${suffix}`;
  if ((shipping || quantity > 1) && !total.isConnected) priceElement.parentElement.appendChild(total);
}, 500);

function parse(text) {
  const match = text?.match(/([^\d\s-]*)(\d[\d\s.,]*\d|\d)([^\d\s-]*)/);
  if (!match) return [0, '', ''];

  let [, prefix, price, suffix] = match;
  price = price.replace(/\s/g, '').split(/\D+/);
  if (!prefix && !suffix && price.length <= 1) return [0, '', ''];

  price = price.length > 1 ? price.slice(0, -1).join('') + '.' + price[price.length - 1] : price[0];
  return [parseFloat(price), prefix, suffix];
}