Get Total Purchase Value

Get total purchase value from API and display it

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Get Total Purchase Value
// @namespace    http://tampermonkey.net/
// @version      0.4
// @description  Get total purchase value from API and display it
// @author       You
// @match        https://*.battle.net/*
// @grant        GM_xmlhttpRequest
// ==/UserScript==

(function() {
    'use strict';

    // Create button
    var button = document.createElement('button');
    button.textContent = 'Get Total Purchase Value';
    document.body.appendChild(button);

    // Create text element to display total value
    var totalValueText = document.createElement('p');
    document.body.appendChild(totalValueText);

    // Add click event listener to button
    button.addEventListener('click', function() {
        GM_xmlhttpRequest({
            method: 'GET',
            url: 'https://account.battle.net/api/transactions?regionId=2',
            onload: function(response) {
                var data = JSON.parse(response.responseText);
                var purchases = data['purchases'];
                var totalValue = 0;

                // Loop through purchases and add up total value
                for (var i = 0; i < purchases.length; i++) {
                    totalValue += purchases[i]['total'].value;
                    console.log(purchases[i]['total'].value);
                }

                // Display total value
                totalValueText.textContent = 'Total Purchase Value: ' + totalValue;
            }
        });
    });
})();