common.js 4.5 KB

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