Warehouse mobile total usage

Calculate total usage in Warehouse mobile website.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Warehouse mobile total usage
// @namespace    http://tampermonkey.net/
// @version      1
// @description  Calculate total usage in Warehouse mobile website.
// @author       dont-be-evil
// @match        https://myaccount.warehousemobile.co.nz/usage
// @icon         https://www.google.com/s2/favicons?sz=64&domain=warehousemobile.co.nz
// @grant        none
// @run-at       context-menu
// @license      GPLv3
// ==/UserScript==

function extract_data_usage(x) {
    // Split the input string into value and unit
    let parts = x.split(" ");
    let value = parseFloat(parts[0]); // Convert the numeric part to a floating-point number
    let unit = parts[1].toUpperCase(); // Get the unit and convert to uppercase for consistency

    // Check if the unit is MB or KB and convert accordingly
    if (unit === "MB") {
        return value * 1024; // Convert MB to KB
    } else if (unit === "KB") {
        return value; // No conversion needed for KB
    }
}

(function() {
    'use strict';
    let usage_type = document.querySelectorAll("span.multiselect__single")[1].textContent;
    const container = document.querySelectorAll('div.container')[2];
    let usage_p = container.querySelector('p');
    if (!usage_p) {
        let new_p = document.createElement('p');
        container.appendChild(new_p);
        usage_p = new_p;
    }
    if (["Calls & Texts", "Calls Only", "Texts Only"].includes(usage_type)) {
        let usage = 0;
        for (let row of document.querySelector("div.usage-table").querySelectorAll("div.usage-table__row:not(.usage-table__header)")) {
            usage = usage + parseFloat(row.lastChild.textContent.replace(/^\$/, ''));
        }
        usage_p.textContent = `Total charge: $${usage}`;
    } else if (["Data"].includes(usage_type)) {
        let usage_kb = 0;
        for (let row of document.querySelector("div.usage-table").querySelectorAll("div.usage-table__row:not(.usage-table__header)")) {
            usage_kb = usage_kb + parseFloat(extract_data_usage(row.children[1].textContent));
        }
        let usage_mb = Math.round(usage_kb / 1024);
        usage_p.textContent = `Total data usage: ${usage_mb} MB`;
    }

})();