Skip to content

Instantly share code, notes, and snippets.

@divinity76
Last active March 24, 2022 17:18
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 divinity76/57b7bf085c796c3fe4ce083178219821 to your computer and use it in GitHub Desktop.
Save divinity76/57b7bf085c796c3fe4ce083178219821 to your computer and use it in GitHub Desktop.
aliexpress total cost userscript
// ==UserScript==
// @name New script - aliexpress.com
// @namespace Violentmonkey Scripts
// @match https://www.aliexpress.com/item/\d*.html
// @grant none
// @version 1.0
// @author -
// @description 3/24/2022, 5:28:33 PM
// ==/UserScript==
(function() {
let $ = document.querySelector.bind(document);
let updateTotal = function() {
let totalEle = document.querySelector("#total_cost_ele");
if (!totalEle) {
let pp = document.querySelectorAll(".product-price")[0];
totalEle = document.createElement("span");
totalEle.id = "total_cost_ele";
pp.insertBefore(document.createElement("br"), pp.firstChild);
pp.insertBefore(totalEle, pp.firstChild);
let totalTextEle = document.createElement("span");
totalTextEle.textContent = "Total: ";
totalEle.style = totalTextEle.style = "font-size: large; font-weight: bold;";
pp.insertBefore(totalTextEle, pp.firstChild);
}
// now to calculate total..
let totalCost = null;
try {
let baseCost = Number($(".product-price-value").textContent.match(/\d+(?:(?:\.|\,)\d+)?/)[0].replace(",", "."));
let shippingCost = $(".dynamic-shipping-line").textContent.match(/\d+(?:(?:\.|\,)\d+)?/);
if(shippingCost && shippingCost[0]){
shippingCost=Number(shippingCost[0].replace(",", "."));
}else{
shippingCost=0;
}
totalCost = baseCost + shippingCost;
} catch (ex) {
totalCost = "error calculating totalCost: " + ex.toString() + " - this script has crashed";
crash = ex;
totalEle.textContent = totalCost;
throw ex;
}
totalEle.textContent = totalCost.toFixed(2);
setTimeout(updateTotal, 500);
};
updateTotal();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment