P2P Usage Progressbar

Show unused bandwidth

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

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

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         P2P Usage Progressbar
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Show unused bandwidth
// @author       You
// @match        https://portal.ptpbroadband.com/datausage.php
// @grant        none
// ==/UserScript==

(function () {
  'use strict';

  setTimeout(function () {

    var xhr = new XMLHttpRequest(),
      formData = new FormData();

    formData.append("action", "getDailyUsage");
    formData.append("customerId", "8730600");
    xhr.open('POST', 'https://portal.ptpbroadband.com/webService.php');

// LINE ADDED
    xhr.setRequestHeader("Accept", "application/json, text/javascript, */*; q=0.01");
    xhr.setRequestHeader("Accept-Language", "en-US,en;q=0.5");
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
    xhr.setRequestHeader("Cache-Control", "no-cache");
    xhr.send('action=getDailyUsage&customerId=8730600');

    xhr.onload = function () {
      if (xhr.readyState === xhr.DONE) {
        if (xhr.status === 200) {
          console.log(xhr.response);
          update(JSON.parse(xhr.response).data);
        }
      }
    };

    function update(data) {
      console.log(data);
      let progressBarDiv = document.querySelector(".progress-bar");
      let parentDiv = document.querySelector(".progress-bar").parentElement;

      progressBarDiv.remove();

      let expectedUsage = new Date(Date.now()).getDate() / data.recentUsage.length;
      let totalGB = data.recentUsage.reduce(function (count, it) {return count + it.y}, 0);
      let currentUsage = totalGB / 400;

      if (currentUsage < expectedUsage) {
        parentDiv.appendChild(createProgressbar(currentUsage, "#47a447"));
        parentDiv.appendChild(createProgressbar(expectedUsage - currentUsage, "#80918c"));
      }
      else {
        parentDiv.appendChild(createProgressbar(expectedUsage, "#a49c20"));
        parentDiv.appendChild(createProgressbar(currentUsage - expectedUsage, "#a42911"));
      }
      
      parentDiv.insertAdjacentHTML("afterend","<p>Your daily bandwidth budget is <strong>" + (400 / data.recentUsage.length).toFixed(1) + "GB</strong></p>" );

      function createProgressbar(percentage, bg) {
        let percent100 = (percentage * 100).toFixed(3);
        let div = document.createElement("div");
        div.classList.add("progress-bar");
        div.setAttribute("role", "progressbar");
        div.setAttribute("aria-valuenow", percent100);
        div.setAttribute("aria-valuemin", "0");
        div.setAttribute("aria-valuemax", "100");
        div.style.width = percent100 + "%";
        div.style.borderRadius = "0";
        div.style.background = bg;

        return div;
      }
    }
  }, 1000);
})();