Greasy Fork

Mandarake JPY to EUR

Converts Mandarake pricing to EUR

目前为 2017-12-01 提交的版本。查看 最新版本

// ==UserScript==
// @name         Mandarake JPY to EUR
// @namespace    http://order.mandarake.co.jp/
// @version      0.2
// @description  Converts Mandarake pricing to EUR
// @author       EIREXE
// @match        https://order.mandarake.co.jp/*
// @grant        none
// ==/UserScript==

// Stackoverflow format unicorn
String.prototype.formatUnicorn = String.prototype.formatUnicorn ||
function () {
    "use strict";
    var str = this.toString();
    if (arguments.length) {
        var t = typeof arguments[0];
        var key;
        var args = ("string" === t || "number" === t) ?
            Array.prototype.slice.call(arguments)
            : arguments[0];

        for (key in args) {
            str = str.replace(new RegExp("\\{" + key + "\\}", "gi"), args[key]);
        }
    }

    return str;
};

(function() {
    'use strict';
    var JPY_data = "https://api.fixer.io/latest?base=JPY";

    $.get(JPY_data, function(data){
        console.log(data.rates.EUR);
        var exchange_rate = data.rates.EUR;
        $('p:contains(" yen")').filter(function() {
            var price_jpy_str = $(this).text().split(" ")[0];
            var price_jpy = parseFloat(price_jpy_str.replace(/,/g, ""));
            var price_converted = (price_jpy*exchange_rate);
            var price_converted_format = price_converted.toLocaleString(undefined, {maximumFractionDigits: 2});
            $(this).text("");
            $(this).append("<strong>{price} €</strong>".formatUnicorn({"price": price_converted_format}));
            $(this).append(" ({price_jpy} ¥)".formatUnicorn({"price_jpy": price_jpy.toLocaleString()}));
            return;
        });
    });
})();