Warehouse mobile total usage

Calculate total usage in Warehouse mobile website.

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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`;
    }

})();