Last active
June 17, 2020 20:01
-
-
Save vogler/41f0b6a5bead9e4670dc9c9b30454a0e to your computer and use it in GitHub Desktop.
AliExpress: cart: 'real shipping' = shipping - 'shipping saved'
This file contains hidden or 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 AliExpress: cart: 'real shipping' = shipping - 'shipping saved' | |
| // @namespace https://gist.github.com/vogler | |
| // @downloadURL https://gist.github.com/vogler/41f0b6a5bead9e4670dc9c9b30454a0e/raw/saved-shipping.aliexpress.tamper.js | |
| // @version 0.2 | |
| // @description Show the real shipping cost, i.e. minus the shared/saved shipping. Without this it's hard to optimize shipping since there also might be other savings. | |
| // @author Ralf Vogler | |
| // @match https://shoppingcart.aliexpress.com/shopcart/* | |
| // @grant none | |
| // ==/UserScript== | |
| (function() { | |
| 'use strict'; | |
| const num = el => parseFloat(el.innerText.match(/\d+\.\d+/)); | |
| const f = t => { | |
| // console.log('target', t); | |
| if (t.className != 'next-loading-masker') return; | |
| const el = document.querySelector('#shoppingcart-price'); | |
| const shipping = num(el.querySelectorAll('.charges-totle dd')[1]); | |
| const savings = el.querySelectorAll('.charges-detail dl'); | |
| let real = shipping; | |
| for (const x of savings) { | |
| const [dt, dd] = x.children; | |
| if (dt.innerText.match(/Shipping +saved/)) { | |
| const saved = num(dd); | |
| real = (shipping - saved).toFixed(2); | |
| console.log(shipping, saved, real); | |
| } | |
| } | |
| // beware infinite mutation-observer-loop! need to only set text if it's different. | |
| const shipping_el = el.querySelectorAll('.charges-totle dt')[1]; | |
| const check_set = s => { if (shipping_el.innerText != s) shipping_el.innerText = s }; | |
| check_set(`Shipping (eff. ${real})`); | |
| } | |
| // handle dynamically loaded/changed items | |
| // the #shoppingcart-price we want to observe is not there initially, so we try until it exists | |
| // const tryUntil = (f, a, c, t=200, n=5) => { | |
| // const r = f.apply(this, [a]); | |
| // if (r) c.apply(this, [r]); | |
| // else if (n) window.setTimeout(() => tryUntil(f,a,c,t,n-1), t) | |
| // } | |
| const observe = (n) => { | |
| const id = '#price-overview'; | |
| const target = document.querySelector(id); | |
| if (target) { | |
| console.log(`${id} found!`); | |
| (new MutationObserver((ms, ob) => | |
| ms.forEach(m => { | |
| // console.log(m); | |
| m.removedNodes.forEach(f); | |
| }) | |
| )).observe(target, { childList: true, subtree: true }); | |
| } else if (n) { | |
| console.log(`${id} not yet found`); | |
| window.setTimeout(() => observe(n-1), 100); | |
| } | |
| }; | |
| observe(10); | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment