Last active
March 24, 2022 17:18
-
-
Save divinity76/57b7bf085c796c3fe4ce083178219821 to your computer and use it in GitHub Desktop.
aliexpress total cost userscript
This file contains 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
// ==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