Skip to content

Instantly share code, notes, and snippets.

@koreapyj
Created November 12, 2022 07:57
Show Gist options
  • Save koreapyj/55c901eafd44a3ad893b38f9fc5aef56 to your computer and use it in GitHub Desktop.
Save koreapyj/55c901eafd44a3ad893b38f9fc5aef56 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name AliExpress KRW helper
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author koreapyj
// @match https://www.aliexpress.com/p/trade/confirm.html*
// @icon https://www.google.com/s2/favicons?sz=64&domain=aliexpress.com
// @grant none
// ==/UserScript==
(async function() {
'use strict';
const SELECTOR_ORDER_TOTAL = '[class^="pl-order-toal"][class$="content"]'
const SELECTOR_CARD_BIN = 'img[class^="credit-card"]+span,img[class^="chosen-channel--chosen-channel-icon--"]+span'
const sleep = ms => new Promise(r => setTimeout(r,ms))
const do_job = async () => {
try {
document.querySelectorAll('[data-alistopdcc]').forEach(elem => elem.remove())
const order = {...(()=>{
const str = document.querySelector(SELECTOR_ORDER_TOTAL).textContent
const match_idx = str.match(/[\d.,]+$/).index
return {
currency: str.substr(0, match_idx),
total: +str.substr(match_idx)
}
})()}
if(order.currency != 'US $') throw new EvalError
const card = {
bin: document.querySelector(SELECTOR_CARD_BIN).textContent.replace(/\D/g,'').substr(0,6)
}
switch(card.bin) {
/* 신한 JCB */
case '356900':
card.brandfee = 0
card.servicefee = 0.0018
card.bankname='shinhan'
break
default:
/* 신한 URS */
if(card.bin.match(/^356(?:297|90(?:7|8))$/)) {
card.brandfee = 0.01
card.servicefee = 0.0018
card.bankname='shinhan'
break
}
/* 신한 AMEX */
if(card.bin.match(/^377(?:178|98(?:3|8))$/)) {
card.brandfee = 0.014
card.servicefee = 0.0018
card.bankname='shinhan'
break
}
/* 신한 VISA */
if(card.bin.match(/^4(?:0(?:0(?:278|772)|3965|4678|5753)|2(?:2155|9612)|3(?:1236|4976|6420|8676)|4(?:5090|9914)|5(?:18(?:42|45))|6(?:0561|1954|5887)|7(?:2(?:175|246|429)|8292)|8(?:6678|7033|9023))$/)) {
card.brandfee = 0.01
card.servicefee = 0.0018
card.bankname='shinhan'
break
}
/* 신한 master */
if(card.bin.match(/^5(?:1(?:0737|1187|5594|7134)|2(?:1189|8638)|3(?:5180|7943|8413)|4(?:2879|9840)|59410)$/)) {
card.brandfee = 0.01
card.servicefee = 0.0018
card.bankname='shinhan'
break
}
/* 신한 UnionPay */
if(card.bin.match(/^62(?:1078|4331|4348|5178)$/)) {
card.brandfee = 0
card.servicefee = 0.0018
card.bankname='shinhan'
break
}
throw new EvalError
}
card.currency = +(await (await fetch(`https://api.dcmys.net/exchange-rate/krw-usd/tt-sell/${card.bankname}?now=${(new Date).toISOString().replace(/T.*$/,'')}`)).json())
order.total_in_krw = (order.total * (1 + card.brandfee)) * card.currency + order.total * card.servicefee
console.log('AliStopDCC:', card, order)
Array.from(document.querySelectorAll('[class^="pl-order-toal"][class$="content"]')).forEach((cont) => {
const parent = cont.parentNode
const new_parent = parent.cloneNode(true)
new_parent.setAttribute('data-alistopdcc','')
const elem_title = new_parent.querySelector('[class$="title-wrap"]')
elem_title.removeAttribute('style')
elem_title.textContent = ''
const elem_content = new_parent.querySelector('[class$="content"]')
elem_content.removeAttribute('style')
elem_content.textContent = `(카드청구금액 ${(~~order.total_in_krw).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")}원)`
elem_content.setAttribute('title', `국제브랜드수수료 ${(~~(card.brandfee*10000))/100}%\n해외서비스수수료 ${(~~(card.servicefee*10000))/100}%\n해외환율고정서비스 적용시`)
parent.after(new_parent)
})
} catch(ex) {
if(!(ex instanceof EvalError)) {
console.error('AliStopDCC:', ex)
}
}
}
while(1) {
const elem_ot = document.querySelector(SELECTOR_ORDER_TOTAL)
const elem_cb = document.querySelector(SELECTOR_CARD_BIN)
if(!elem_ot || !elem_cb) {
await sleep(100)
continue
}
break
}
do_job()
const timers = {}
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if(timers.mutation) {
clearTimeout(timers.mutation)
}
timers.mutation = setTimeout(()=>{
timers.mutation = null
do_job()
}, 1000)
})
})
const observer_conf = {
attributes: true,
characterData: true,
childList: true,
}
observer.observe(document.body, observer_conf)
console.log('observer installed')
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment