P2P Usage Progressbar

Show unused bandwidth

  1. // ==UserScript==
  2. // @name P2P Usage Progressbar
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1
  5. // @description Show unused bandwidth
  6. // @author You
  7. // @match https://portal.ptpbroadband.com/datausage.php
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function () {
  12. 'use strict';
  13.  
  14. setTimeout(function () {
  15.  
  16. var xhr = new XMLHttpRequest(),
  17. formData = new FormData();
  18.  
  19. formData.append("action", "getDailyUsage");
  20. formData.append("customerId", "8730600");
  21. xhr.open('POST', 'https://portal.ptpbroadband.com/webService.php');
  22.  
  23. // LINE ADDED
  24. xhr.setRequestHeader("Accept", "application/json, text/javascript, */*; q=0.01");
  25. xhr.setRequestHeader("Accept-Language", "en-US,en;q=0.5");
  26. xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
  27. xhr.setRequestHeader("Cache-Control", "no-cache");
  28. xhr.send('action=getDailyUsage&customerId=8730600');
  29.  
  30. xhr.onload = function () {
  31. if (xhr.readyState === xhr.DONE) {
  32. if (xhr.status === 200) {
  33. console.log(xhr.response);
  34. update(JSON.parse(xhr.response).data);
  35. }
  36. }
  37. };
  38.  
  39. function update(data) {
  40. console.log(data);
  41. let progressBarDiv = document.querySelector(".progress-bar");
  42. let parentDiv = document.querySelector(".progress-bar").parentElement;
  43.  
  44. progressBarDiv.remove();
  45.  
  46. let expectedUsage = new Date(Date.now()).getDate() / data.recentUsage.length;
  47. let totalGB = data.recentUsage.reduce(function (count, it) {return count + it.y}, 0);
  48. let currentUsage = totalGB / 400;
  49.  
  50. if (currentUsage < expectedUsage) {
  51. parentDiv.appendChild(createProgressbar(currentUsage, "#47a447"));
  52. parentDiv.appendChild(createProgressbar(expectedUsage - currentUsage, "#80918c"));
  53. }
  54. else {
  55. parentDiv.appendChild(createProgressbar(expectedUsage, "#a49c20"));
  56. parentDiv.appendChild(createProgressbar(currentUsage - expectedUsage, "#a42911"));
  57. }
  58. parentDiv.insertAdjacentHTML("afterend","<p>Your daily bandwidth budget is <strong>" + (400 / data.recentUsage.length).toFixed(1) + "GB</strong></p>" );
  59.  
  60. function createProgressbar(percentage, bg) {
  61. let percent100 = (percentage * 100).toFixed(3);
  62. let div = document.createElement("div");
  63. div.classList.add("progress-bar");
  64. div.setAttribute("role", "progressbar");
  65. div.setAttribute("aria-valuenow", percent100);
  66. div.setAttribute("aria-valuemin", "0");
  67. div.setAttribute("aria-valuemax", "100");
  68. div.style.width = percent100 + "%";
  69. div.style.borderRadius = "0";
  70. div.style.background = bg;
  71.  
  72. return div;
  73. }
  74. }
  75. }, 1000);
  76. })();