Skip to content

Commit 3931495

Browse files
committed
Add Amazon Price conversion script
0 parents  commit 3931495

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

amazon-convert.user.js

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
// ==UserScript==
2+
// @name Amazon Currency Conversion
3+
// @namespace carltonf.github.io
4+
// @description Automatically converts different currencies into your home currency, more convenient for overseas shopping.
5+
// @include http://www.amazon.com/*
6+
// @include http://www.amazon.co.jp/*
7+
// @version 0.3
8+
// @grant none
9+
// ==/UserScript==
10+
11+
'use strict';
12+
13+
// * Extra info
14+
var CurSymMap = {
15+
'CNY': '¥',
16+
// amazon.co.jp uses a Yen symbol in JP locale (it seems)
17+
'JPY': '¥',
18+
'USD': '$',
19+
};
20+
21+
// As 2015-10-31
22+
var CurRates = {
23+
'JPY-CNY': 0.05237,
24+
'USD-CNY': 6.3185,
25+
}
26+
27+
// * Setup
28+
// Get the
29+
var origCur = null;
30+
switch (document.location.hostname){
31+
case "www.amazon.com":
32+
origCur = 'USD';
33+
break;
34+
case "www.amazon.co.jp":
35+
origCur = 'JPY';
36+
break;
37+
default:
38+
throw new Error(`${document.location.hostname} is NOT supported!`);
39+
}
40+
41+
// * Main converter for price
42+
// for now only 'CNY' as target, so targetCur is pre-fixed.
43+
function PriceConverter (origCur, targetCur){
44+
var self = this;
45+
46+
47+
this._origCur = origCur;
48+
this._targetCur = targetCur || 'CNY';
49+
50+
// ** Price string conversion
51+
// accept a string in format like "$ 130" and outputs a string in the target
52+
// currency.
53+
this.convertPriceStr = convertPriceStr
54+
function convertPriceStr(str){
55+
var origSym = CurSymMap[self._origCur],
56+
targetSym = CurSymMap[self._targetCur],
57+
origNum = -1,
58+
targetNum = -1;
59+
60+
// *** Extract the number part
61+
str = str.substring( str.indexOf(origSym) + 1 ).trim().replace(',', '');
62+
origNum = Number(str);
63+
64+
// *** Convert currency
65+
targetNum = origNum * CurRates[`${self._origCur}-${self._targetCur}`];
66+
67+
// *** output in the currency
68+
// TODO the locale is fixed to Chinese
69+
var numberFormat = new Intl.NumberFormat("zh-Hans-CN",
70+
{ style: 'currency',
71+
currency: this._targetCur,
72+
maximumFractionDigits: 1,
73+
});
74+
75+
// also separates the symbol and number with a space
76+
// with right locale, no need to add currency symbol manually
77+
return numberFormat.format(targetNum);
78+
};
79+
}
80+
81+
var priceConverter = new PriceConverter(origCur);
82+
83+
// * Mainpulate the page price tags
84+
// Return an array of elements
85+
function getPriceTagAll(){
86+
function isPriceTag (elm){
87+
return elm.textContent.includes(CurSymMap[origCur]);
88+
}
89+
90+
var priceTags = [].filter.call(document.querySelectorAll('span'),
91+
isPriceTag);
92+
93+
return priceTags;
94+
}
95+
96+
97+
function changePriceTag(priceTag){
98+
priceTag.innerText = priceConverter.convertPriceStr(priceTag.textContent);
99+
}
100+
101+
getPriceTagAll().forEach(changePriceTag);

0 commit comments

Comments
 (0)