Skip to content

Instantly share code, notes, and snippets.

@azu
Last active August 8, 2021 04:13
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 azu/4407c5424f3941d340ee9b1a2ebde69f to your computer and use it in GitHub Desktop.
Save azu/4407c5424f3941d340ee9b1a2ebde69f to your computer and use it in GitHub Desktop.
furusato-tax.jpの表示金額に2割の表示を追加するGreasemonkeyスクリプト
// ==UserScript==
// @name furusato-tax.jp: price * 0.2
// @namespace furusato-tax.jpの表示金額に2割の表示を追加する
// @match https://www.furusato-tax.jp/*
// @grant none
// @version 1.0
// @author -
// @description 2021/8/8 12:08:48
// @run-at document-end
// ==/UserScript==
// 表示する金額の割合
const PRICE_RATE = 0.2;
/**
* Parse a localized number to a float.
* @param {string} stringNumber - the localized number
* @param {string} locale - [optional] the locale that the number is represented in. Omit this parameter to use the current locale.
*/
function parseLocaleNumber(stringNumber, locale) {
var thousandSeparator = Intl.NumberFormat(locale).format(11111).replace(/\p{Number}/gu, '');
var decimalSeparator = Intl.NumberFormat(locale).format(1.1).replace(/\p{Number}/gu, '');
return parseFloat(stringNumber
.replace(new RegExp('\\' + thousandSeparator, 'g'), '')
.replace(new RegExp('\\' + decimalSeparator), '.')
);
}
const update = () => {
const prices = document.querySelectorAll(".card-product__price, .goods--slide__price, .goods-col_price");
for (priceNode of prices) {
if(priceNode.dataset.gmModified){
continue;
}
const originalValue = parseLocaleNumber(priceNode.textContent.trim(), "ja-JP");
if(originalValue){
const value2wari = originalValue * PRICE_RATE;
const value2Format = Intl.NumberFormat("ja-Jp").format(value2wari);
priceNode.textContent += ` (${value2Format}円)`;
priceNode.dataset.gmModified = 1;
}
}
// content
const mainPrice = document.querySelector(".basicinfo_price");
if(mainPrice && !mainPrice.dataset.gmModified) {
const orignalValueMatch = mainPrice.textContent.match(/[\d,]+\s{0,5}円/);
const originalValue = parseLocaleNumber(orignalValueMatch[0].replace(/\s/g, ""), "ja-JP");
if(originalValue){
const value2wari = originalValue * PRICE_RATE;
const value2Format = Intl.NumberFormat("ja-Jp").format(value2wari);
mainPrice.append(document.createTextNode(`(${value2Format}円)`));
mainPrice.dataset.gmModified = 1;
}
}
}
update();
const observer = new MutationObserver((mutations) => {
update();
});
observer.observe(document.body, { childList: true, subtree:true });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment