Advertisement
Guest User

Untitled

a guest
May 5th, 2024
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.62 KB | None | 0 0
  1. // ==UserScript==
  2. // @name KG_TotalGamesCount_And_TotalHours
  3. // @namespace klavogonki
  4. // @description Добавляет отображение общего количества заездов и общее количество часов, проведенных в заездах во всех режимах на страницу статистики
  5. // @author voidmain, ASplayer9119
  6. // @license MIT
  7. // @version 1.2.0
  8. // @include http*://klavogonki.ru/u/*
  9. // @grant none
  10. // @run-at document-end
  11. // ==/UserScript==
  12.  
  13. function exec(fn) {
  14. var script = document.createElement('script');
  15. script.setAttribute("type", "application/javascript");
  16. script.textContent = '(' + fn + ')();';
  17. document.body.appendChild(script);
  18. document.body.removeChild(script);
  19. }
  20.  
  21. function main() {
  22. const NUMBER_OF_SECONDS_IN_ONE_MINUTE = 60;
  23. const NUMBER_OF_MINUTES_IN_ONE_HOUR = 60;
  24. const NUMBER_OF_HOURS_IN_ONE_DAY = 24;
  25. const NUMBER_OF_SECONDS_IN_ONE_HOUR = NUMBER_OF_SECONDS_IN_ONE_MINUTE * NUMBER_OF_MINUTES_IN_ONE_HOUR;
  26. const NUMBER_OF_SECONDS_IN_ONE_DAY = NUMBER_OF_SECONDS_IN_ONE_HOUR * NUMBER_OF_HOURS_IN_ONE_DAY;
  27.  
  28. function calculateTotalSeconds(stats) {
  29. const totalSeconds = Object.values(stats).reduce((acc, gameStat) => {
  30. return acc + gameStat.info.haul;
  31. }, 0);
  32. return totalSeconds;
  33. }
  34.  
  35. function calculateTotalHours(seconds) {
  36. const totalHours = Math.round(seconds / NUMBER_OF_SECONDS_IN_ONE_HOUR);
  37. return totalHours;
  38. }
  39.  
  40. function splitTotalSeconds(seconds) {
  41. let secondsLeft;
  42.  
  43. const days = Math.floor(seconds / (NUMBER_OF_SECONDS_IN_ONE_DAY));
  44. secondsLeft = seconds - days * NUMBER_OF_SECONDS_IN_ONE_DAY;
  45. const hours = Math.floor(secondsLeft / (NUMBER_OF_SECONDS_IN_ONE_HOUR));
  46. secondsLeft = secondsLeft - hours * NUMBER_OF_SECONDS_IN_ONE_HOUR;
  47. const minutes = Math.floor(secondsLeft / NUMBER_OF_SECONDS_IN_ONE_MINUTE);
  48. secondsLeft = secondsLeft - minutes * NUMBER_OF_SECONDS_IN_ONE_MINUTE;
  49.  
  50. return `${days} дн. ${hours} ч. ${minutes} мин. ${secondsLeft} сек.`
  51. }
  52.  
  53. var totalGamesCountTemplate = '\n<style>\
  54. #totalGamesCounter { width: 100%; padding: 1px 0 5px 0; background-color: #f5f5f5; border-radius: 3px; margin-bottom: 20px; margin-right: 20px; }\
  55. #totalHours { width: 100%; padding: 1px 0 5px 0; background-color: #f5f5f5; border-radius: 3px; margin-bottom: 20px; }\
  56. #totalGamesCounter .title { padding: 5px; margin-left: 5px; font-size: 12px; font-weight: bold; color: #aaa; text-transform: uppercase; text-align: center; }\
  57. #totalHours .title { padding: 5px; margin-left: 5px; font-size: 12px; font-weight: bold; color: #aaa; text-transform: uppercase; text-align: center; }\
  58. #totalGamesCounter .value { position: relative; padding: 3px 0 3px 10px; margin: 0 5px; font-size: 18px; font-weight: bold; color: #666; background-color: #f9f9f9; border-top: 1px solid #ccc; border-bottom: 1px solid white; border-radius: 3px; }\
  59. #totalHours .value { position: relative; padding: 3px 0 3px 10px; margin: 0 5px; font-size: 18px; font-weight: bold; color: #666; background-color: #f9f9f9; border-top: 1px solid #ccc; border-bottom: 1px solid white; border-radius: 3px; }\
  60. #totalGamesCounter .value .icon-icomoon { margin-right: 2px; font-size: 16px; vertical-align: middle; }\
  61. #totalHours .value .icon-icomoon { margin-right: 2px; font-size: 16px; vertical-align: middle; }\
  62. #totalGamesCounter .value span { vertical-align: middle; }\
  63. #totalHours .value span { vertical-align: middle; }\
  64. #totalContainer { display: flex; flex-direction: row; justify-content: center; align-items: center }\
  65. </style>\n\
  66. <div id="totalContainer">\n\
  67. <div id="totalGamesCounter">\n\
  68. <div class="title">Общий пробег</div>\n\
  69. <div class="value">\n\
  70. <div class="icon-icomoon icon-road"></div>\n\
  71. <span>{{total_num_races}}</span>\n\
  72. </div>\n\
  73. </div>\n\
  74. <div id="totalHours">\n\
  75. <div class="title">Суммарное время чистого набора</div>\n\
  76. <div class="value">\n\
  77. <div class="icon-icomoon"><svg xmlns="http://www.w3.org/2000/svg" width="1em" height="1em" viewBox="0 0 16 16"><path fill="currentColor" d="M10.293 11.707L7 8.414V4h2v3.586l2.707 2.707zM8 0a8 8 0 1 0 0 16A8 8 0 0 0 8 0m0 14A6 6 0 1 1 8 2a6 6 0 0 1 0 12"/></svg></div>\n\
  78. <span>{{total_hours}}</span>\n\
  79. </div>\n\
  80. </div>\n\
  81. </div>\n'
  82. ;
  83.  
  84. angular.element('body').scope().$on('routeSegmentChange', function(e, obj) {
  85. if(!obj.segment || obj.segment.name != "overview") {
  86. return;
  87. }
  88.  
  89. var profile = angular.element('body').injector().get('Profile');
  90.  
  91. profile.getStatsOverview(obj.segment.locals.data.summary.user.id).then(function(stats) {
  92. const totalSeconds = calculateTotalSeconds(stats.gametypes);
  93. const totalHours = calculateTotalHours(totalSeconds);
  94. const splittedTimeStr = splitTotalSeconds(totalSeconds);
  95. e.targetScope.total_hours = `${totalHours} ч. (${splittedTimeStr})`;
  96. });
  97.  
  98. profile.getIndexData(obj.segment.locals.data.summary.user.id).then(function(index) {
  99. e.targetScope.total_num_races = index.stats.total_num_races;
  100. });
  101.  
  102. var template = obj.segment.locals.$template;
  103. var index = template.indexOf("<div class='recent'>\n<h4>Недавние режимы</h4>");
  104. template = template.substring(0, index) + totalGamesCountTemplate + template.substring(index, template.length);
  105. obj.segment.locals.$template = template;
  106. });
  107. }
  108.  
  109. window.addEventListener("load", function() {
  110. exec(main);
  111. }, false);
  112.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement