Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name KG_TotalGamesCount_And_TotalHours
- // @namespace klavogonki
- // @description Добавляет отображение общего количества заездов и общее количество часов, проведенных в заездах во всех режимах на страницу статистики
- // @author voidmain, ASplayer9119
- // @license MIT
- // @version 1.2.0
- // @include http*://klavogonki.ru/u/*
- // @grant none
- // @run-at document-end
- // ==/UserScript==
- function exec(fn) {
- var script = document.createElement('script');
- script.setAttribute("type", "application/javascript");
- script.textContent = '(' + fn + ')();';
- document.body.appendChild(script);
- document.body.removeChild(script);
- }
- function main() {
- const NUMBER_OF_SECONDS_IN_ONE_MINUTE = 60;
- const NUMBER_OF_MINUTES_IN_ONE_HOUR = 60;
- const NUMBER_OF_HOURS_IN_ONE_DAY = 24;
- const NUMBER_OF_SECONDS_IN_ONE_HOUR = NUMBER_OF_SECONDS_IN_ONE_MINUTE * NUMBER_OF_MINUTES_IN_ONE_HOUR;
- const NUMBER_OF_SECONDS_IN_ONE_DAY = NUMBER_OF_SECONDS_IN_ONE_HOUR * NUMBER_OF_HOURS_IN_ONE_DAY;
- function calculateTotalSeconds(stats) {
- const totalSeconds = Object.values(stats).reduce((acc, gameStat) => {
- return acc + gameStat.info.haul;
- }, 0);
- return totalSeconds;
- }
- function calculateTotalHours(seconds) {
- const totalHours = Math.round(seconds / NUMBER_OF_SECONDS_IN_ONE_HOUR);
- return totalHours;
- }
- function splitTotalSeconds(seconds) {
- let secondsLeft;
- const days = Math.floor(seconds / (NUMBER_OF_SECONDS_IN_ONE_DAY));
- secondsLeft = seconds - days * NUMBER_OF_SECONDS_IN_ONE_DAY;
- const hours = Math.floor(secondsLeft / (NUMBER_OF_SECONDS_IN_ONE_HOUR));
- secondsLeft = secondsLeft - hours * NUMBER_OF_SECONDS_IN_ONE_HOUR;
- const minutes = Math.floor(secondsLeft / NUMBER_OF_SECONDS_IN_ONE_MINUTE);
- secondsLeft = secondsLeft - minutes * NUMBER_OF_SECONDS_IN_ONE_MINUTE;
- return `${days} дн. ${hours} ч. ${minutes} мин. ${secondsLeft} сек.`
- }
- var totalGamesCountTemplate = '\n<style>\
- #totalGamesCounter { width: 100%; padding: 1px 0 5px 0; background-color: #f5f5f5; border-radius: 3px; margin-bottom: 20px; margin-right: 20px; }\
- #totalHours { width: 100%; padding: 1px 0 5px 0; background-color: #f5f5f5; border-radius: 3px; margin-bottom: 20px; }\
- #totalGamesCounter .title { padding: 5px; margin-left: 5px; font-size: 12px; font-weight: bold; color: #aaa; text-transform: uppercase; text-align: center; }\
- #totalHours .title { padding: 5px; margin-left: 5px; font-size: 12px; font-weight: bold; color: #aaa; text-transform: uppercase; text-align: center; }\
- #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; }\
- #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; }\
- #totalGamesCounter .value .icon-icomoon { margin-right: 2px; font-size: 16px; vertical-align: middle; }\
- #totalHours .value .icon-icomoon { margin-right: 2px; font-size: 16px; vertical-align: middle; }\
- #totalGamesCounter .value span { vertical-align: middle; }\
- #totalHours .value span { vertical-align: middle; }\
- #totalContainer { display: flex; flex-direction: row; justify-content: center; align-items: center }\
- </style>\n\
- <div id="totalContainer">\n\
- <div id="totalGamesCounter">\n\
- <div class="title">Общий пробег</div>\n\
- <div class="value">\n\
- <div class="icon-icomoon icon-road"></div>\n\
- <span>{{total_num_races}}</span>\n\
- </div>\n\
- </div>\n\
- <div id="totalHours">\n\
- <div class="title">Суммарное время чистого набора</div>\n\
- <div class="value">\n\
- <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\
- <span>{{total_hours}}</span>\n\
- </div>\n\
- </div>\n\
- </div>\n'
- ;
- angular.element('body').scope().$on('routeSegmentChange', function(e, obj) {
- if(!obj.segment || obj.segment.name != "overview") {
- return;
- }
- var profile = angular.element('body').injector().get('Profile');
- profile.getStatsOverview(obj.segment.locals.data.summary.user.id).then(function(stats) {
- const totalSeconds = calculateTotalSeconds(stats.gametypes);
- const totalHours = calculateTotalHours(totalSeconds);
- const splittedTimeStr = splitTotalSeconds(totalSeconds);
- e.targetScope.total_hours = `${totalHours} ч. (${splittedTimeStr})`;
- });
- profile.getIndexData(obj.segment.locals.data.summary.user.id).then(function(index) {
- e.targetScope.total_num_races = index.stats.total_num_races;
- });
- var template = obj.segment.locals.$template;
- var index = template.indexOf("<div class='recent'>\n<h4>Недавние режимы</h4>");
- template = template.substring(0, index) + totalGamesCountTemplate + template.substring(index, template.length);
- obj.segment.locals.$template = template;
- });
- }
- window.addEventListener("load", function() {
- exec(main);
- }, false);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement