Coding Repos Size

Coding仓库大小统计

  1. // ==UserScript==
  2. // @name Coding Repos Size
  3. // @version 1.0.0
  4. // @description Coding仓库大小统计
  5. // @author Jack.Chan (971546@qq.com)
  6. // @namespace http://fulicat.com
  7. // @url https://greasyfork.org/zh-CN/scripts/475875-coding-repos-size
  8. // @match https://*.coding.net/api/repos*
  9. // @grant none
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13. (function(){
  14.  
  15. function formatSize(size) {
  16. if (size === 0) return '0 B';
  17. // var k = 1000; // or 1024
  18. var k = 1024; // or 1024
  19. size = size * k;
  20. var sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
  21. var i = Math.floor(Math.log(size) / Math.log(k));
  22. return (size / Math.pow(k, i)).toFixed(2) + ' ' + sizes[i];
  23. }
  24.  
  25. var $style = document.createElement('style');
  26. $style.setAttribute('type', 'text/css');
  27. $style.innerText = `
  28. .loading{
  29. text-align: center;
  30. padding: 50px 20px;
  31. }
  32.  
  33. table{
  34. border-collapse: collapse;
  35. width: 100%;
  36. }
  37.  
  38. table th{
  39. background: #f5f5f5;
  40. }
  41.  
  42. table,
  43. table th,
  44. table td{
  45. border: 1px solid #e5e5e5;
  46. }
  47.  
  48. table th,
  49. table td{
  50. padding: 2px 5px;
  51. }
  52. `;
  53.  
  54. var profile = location.host.split('.')[0];
  55. console.log('profile:', profile);
  56. if (!profile) {
  57. return console.error('出现异常了哇!');
  58. }
  59.  
  60. document.title = 'Coding Repos Size - by Jack.Chan';
  61. var $body = document.body;
  62. var $result = document.createElement('div');
  63. var $loading = document.createElement('p');
  64.  
  65. $loading.className = 'loading';
  66. $loading.innerText = '加载中...';
  67. $result.appendChild($loading);
  68.  
  69. $body.innerHTML = '';
  70. $body.appendChild($style);
  71. $body.appendChild($result);
  72.  
  73. var baseURL = `https://${ profile }.coding.net`;
  74.  
  75. fetch(`${ baseURL }/api/user/shebaochina/depots?type=ALL`, { method: 'GET' }).then((res) => res.json()).then((res) => {
  76. $loading.innerText = '计算中...';
  77.  
  78. console.log('res:', res);
  79.  
  80. res.data.sort((a, b)=>{ return b.size - a.size; });
  81.  
  82. // res.data = res.data.sort((a, b) => {
  83. // return a.size - b.size
  84. // });
  85.  
  86. var totalSize = 0;
  87.  
  88. var html = [];
  89. html.push(`<colgroup>`);
  90. html.push(`<col style="min-width: 180px;max-width: 260px;">`);
  91. html.push(`<col style="min-width: 180px;max-width: 260px;">`);
  92. html.push(`<col style="min-width: 100px;max-width: 160px;">`);
  93. html.push(`<col style="width: auto;">`);
  94. html.push(`<col style="width: auto;">`);
  95. html.push(`<col style="width: auto;">`);
  96. html.push(`<\/colgroup>`);
  97. html.push(`<thead>`);
  98. html.push(`<tr>`);
  99. html.push(`<th>名称<\/th>`);
  100. html.push(`<th>描述<\/th>`);
  101. html.push(`<th>大小<\/th>`);
  102. html.push(`<th>HTTP<\/th>`);
  103. html.push(`<th>SSH<\/th>`);
  104. html.push(`<th>URL<\/th>`);
  105. html.push(`<\/tr>`);
  106. html.push(`<\/thead>`);
  107. html.push(`<tbody>`);
  108. res.data.forEach((item) => {
  109. totalSize = totalSize + parseInt(item.size);
  110. html.push(`<tr>
  111. <td>${ item.name }<\/td>
  112. <td>${ item.description }<\/td>
  113. <td style="text-align: right;" title="${ item.size }">${ formatSize(item.size) }<\/td>
  114. <td>${ item.gitHttpsUrl }<\/td>
  115. <td>${ item.gitSshUrl }<\/td>
  116. <td><a href="${ baseURL + item.depotPath }">https://shebaochina.coding.net${ item.depotPath }</a><\/td>
  117. <\/tr>`);
  118. });
  119. html.push(`<\/tbody>`);
  120.  
  121. var $table = document.createElement('table');
  122. $table.innerHTML = html.join('');
  123. $table.tBodies[0].innerHTML = `
  124. <tr>
  125. <td colspan="2" style="font-weight: bold;">共: ${ res.data.length } 个仓库<\/td>
  126. <td style="text-align: right;font-weight: bold;" title="${ totalSize }">${ formatSize(totalSize) }<\/td>
  127. <td colspan="3">&nbsp;<\/td>
  128. <\/tr> ${ $table.tBodies[0].innerHTML }
  129. `;
  130. $result.innerHTML = '';
  131. $result.appendChild($table);
  132. }).catch((err) => {
  133. $result.innerHTML = '<p style="text-align: center;padding: 50px 20px;color: red;">接口出错了哇!</p>';
  134. console.error(err);
  135. });
  136.  
  137. })();