common.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. const ONE_KB = 1024;
  2. const ONE_MB = ONE_KB * 1024;
  3. const ONE_GB = ONE_MB * 1024;
  4. const ONE_TB = ONE_GB * 1024;
  5. const ONE_PB = ONE_TB * 1024;
  6. function sizeFormat(size) {
  7. if (size <= 0) return "0 B";
  8. if (size < ONE_KB) {
  9. return size.toFixed(0) + " B";
  10. } else if (size < ONE_MB) {
  11. return (size / ONE_KB).toFixed(2) + " KB";
  12. } else if (size < ONE_GB) {
  13. return (size / ONE_MB).toFixed(2) + " MB";
  14. } else if (size < ONE_TB) {
  15. return (size / ONE_GB).toFixed(2) + " GB";
  16. } else if (size < ONE_PB) {
  17. return (size / ONE_TB).toFixed(2) + " TB";
  18. } else {
  19. return (size / ONE_PB).toFixed(2) + " PB";
  20. }
  21. }
  22. function cpuSpeedFormat(speed) {
  23. if (speed > 1000) {
  24. const GHz = speed / 1000;
  25. return GHz.toFixed(2) + " GHz";
  26. } else {
  27. return speed.toFixed(2) + " MHz";
  28. }
  29. }
  30. function cpuCoreFormat(cores) {
  31. if (cores === 1) {
  32. return "1 Core";
  33. } else {
  34. return cores + " Cores";
  35. }
  36. }
  37. function formatSecond(second) {
  38. if (second < 60) {
  39. return second.toFixed(0) + 's';
  40. } else if (second < 3600) {
  41. return (second / 60).toFixed(0) + 'm';
  42. } else if (second < 3600 * 24) {
  43. return (second / 3600).toFixed(0) + 'h';
  44. } else {
  45. day = Math.floor(second / 3600 / 24);
  46. remain = ((second / 3600) - (day * 24)).toFixed(0);
  47. return day + 'd' + (remain > 0 ? ' ' + remain + 'h' : '');
  48. }
  49. }
  50. function addZero(num) {
  51. if (num < 10) {
  52. return "0" + num;
  53. } else {
  54. return num;
  55. }
  56. }
  57. function toFixed(num, n) {
  58. n = Math.pow(10, n);
  59. return Math.floor(num * n) / n;
  60. }
  61. function debounce(fn, delay) {
  62. var timeoutID = null;
  63. return function () {
  64. clearTimeout(timeoutID);
  65. var args = arguments;
  66. var that = this;
  67. timeoutID = setTimeout(function () {
  68. fn.apply(that, args);
  69. }, delay);
  70. };
  71. }
  72. function getCookie(cname) {
  73. let name = cname + '=';
  74. let ca = document.cookie.split(';');
  75. for (let i = 0; i < ca.length; i++) {
  76. let c = ca[i];
  77. while (c.charAt(0) == ' ') {
  78. c = c.substring(1);
  79. }
  80. if (c.indexOf(name) == 0) {
  81. // decode cookie value only
  82. return decodeURIComponent(c.substring(name.length, c.length));
  83. }
  84. }
  85. return '';
  86. }
  87. function setCookie(cname, cvalue, exdays) {
  88. const d = new Date();
  89. d.setTime(d.getTime() + exdays * 24 * 60 * 60 * 1000);
  90. let expires = 'expires=' + d.toUTCString();
  91. // encode cookie value
  92. document.cookie = cname + '=' + encodeURIComponent(cvalue) + ';' + expires + ';path=/';
  93. }
  94. function usageColor(data, threshold, total) {
  95. switch (true) {
  96. case data === null:
  97. return "purple";
  98. case total < 0:
  99. return "green";
  100. case total == 0:
  101. return "purple";
  102. case data < total - threshold:
  103. return "green";
  104. case data < total:
  105. return "orange";
  106. default:
  107. return "red";
  108. }
  109. }
  110. function clientUsageColor(clientStats, trafficDiff) {
  111. switch (true) {
  112. case !clientStats || clientStats.total == 0:
  113. return "#7a316f"; // purple
  114. case clientStats.up + clientStats.down < clientStats.total - trafficDiff:
  115. return "#008771"; // Green
  116. case clientStats.up + clientStats.down < clientStats.total:
  117. return "#f37b24"; // Orange
  118. default:
  119. return "#cf3c3c"; // Red
  120. }
  121. }
  122. function userExpiryColor(threshold, client, isDark = false) {
  123. if (!client.enable) {
  124. return isDark ? '#2c3950' : '#bcbcbc';
  125. }
  126. now = new Date().getTime(),
  127. expiry = client.expiryTime;
  128. switch (true) {
  129. case expiry === null:
  130. return "#7a316f"; // purple
  131. case expiry < 0:
  132. return "#008771"; // Green
  133. case expiry == 0:
  134. return "#7a316f"; // purple
  135. case now < expiry - threshold:
  136. return "#008771"; // Green
  137. case now < expiry:
  138. return "#f37b24"; // Orange
  139. default:
  140. return "#cf3c3c"; // Red
  141. }
  142. }
  143. function doAllItemsExist(array1, array2) {
  144. for (let i = 0; i < array1.length; i++) {
  145. if (!array2.includes(array1[i])) {
  146. return false;
  147. }
  148. }
  149. return true;
  150. }
  151. function buildURL({ host, port, isTLS, base, path }) {
  152. if (!host || host.length === 0) host = window.location.hostname;
  153. if (!port || port.length === 0) port = window.location.port;
  154. if (isTLS === undefined) isTLS = window.location.protocol === "https:";
  155. const protocol = isTLS ? "https:" : "http:";
  156. port = String(port);
  157. if (port === "" || (isTLS && port === "443") || (!isTLS && port === "80")) {
  158. port = "";
  159. } else {
  160. port = `:${port}`;
  161. }
  162. return `${protocol}//${host}${port}${base}${path}`;
  163. }