Sleeper.com - FFL - Output Weekly Median.

Add week Median to league screen on Sleeper.com.

이 스크립트를 설치하려면 Tampermonkey, Greasemonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

You will need to install an extension such as Tampermonkey to install this script.

이 스크립트를 설치하려면 Tampermonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Userscripts와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 유저 스크립트 관리자 확장 프로그램이 필요합니다.

(이미 유저 스크립트 관리자가 설치되어 있습니다. 설치를 진행합니다!)

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

(이미 유저 스타일 관리자가 설치되어 있습니다. 설치를 진행합니다!)

// ==UserScript==
// @name         Sleeper.com - FFL - Output Weekly Median.
// @description  Add week Median to league screen on Sleeper.com.
// @version      1.0.1
// @namespace    http://jbout.in/
// @author       Jeremy 'HLVE'
// @match        *://sleeper.com/leagues/*/*
// @require      https://code.jquery.com/jquery-1.9.1.min.js
// @grant        GM_setValue
// @grant		  GM_getValue
// @grant        GM_addStyle
// ==/UserScript==

$(window).on("load popstate", function (event) {
	tabIDs();
	if (window.location.pathname.split('/')[3].indexOf("league") != -1) {
		var medianValue = calculateMedian();
	}

	$(document).on('click touchend', '#tabLeague', function () {
		setTimeout(function () {
			var medianValue = calculateMedian();
		}, 150);
	});
});

function tabIDs() {
  const tabEl = $("div.center-tab-selector > div.item-tab"),
		  matchupTab = $("div.center-tab-selector > div.item-tab:contains('MATCHUP')"),
  		  teamTab = $("div.center-tab-selector > div.item-tab:contains('TEAM')"),
  		  leagueTab = $("div.center-tab-selector > div.item-tab:contains('LEAGUE')"),
  		  playersTab = $("div.center-tab-selector > div.item-tab:contains('PLAYERS')"),
  		  trendTab = $("div.center-tab-selector > div.item-tab:contains('TREND')"),
  		  tradeTab = $("div.center-tab-selector > div.item-tab:contains('TRADES')"),
  		  scoreTab = $("div.center-tab-selector > div.item-tab:contains('SCORES')");

  setTimeout(function () {
	   $(matchupTab).attr('id', 'tabMatchup');
		$(teamTab).attr('id', 'tabTeam');
		$(leagueTab).attr('id', 'tabLeague');
		$(playersTab).attr('id', 'tabPlayers');
		$(trendTab).attr('id', 'tabTrending');
		$(tradeTab).attr('id', 'tabTrades');
		$(scoreTab).attr('id', 'tabScores');
	}, 35);
}

function calculateMedian() {
	var matchupTitleContainer = $(".league-tab-container .title").first(),
		 scoreContainer = $("div.league-matchups div.roster-score-and-projection-matchup div.score"),
		 numbers = [];

	$(scoreContainer).each(function () {
		var value = parseFloat($(this).text());
		if (!isNaN(value)) {
			numbers.push(value);
		}
	});

	// Sort the scores in ascending order
	numbers.sort(function (a, b) {
		return a - b;
	});

	// Calculate the median
	var median;
	var len = numbers.length;
	if (len === 0) {
		median = 0; // If no numbers found, median is 0
	} else if (len % 2 === 1) {
		median = numbers[Math.floor(len / 2)];
	} else {
		var mid1 = numbers[len / 2 - 1];
		var mid2 = numbers[len / 2];
		median = (mid1 + mid2) / 2;
	}

	var calcMedian = Math.ceil(median * 100) / 100;

	// Round up to the next full decimal place.
	if (!$(".league-panel-header .post-draft-league-header .name").text().includes('Guillotine')) {
		if (!$("#weeklyMedian").length) {
			$(matchupTitleContainer).append(' <span id="weeklyMedian">(<b>Weekly Median</b>: ' + calcMedian + ')</span>');
		};
	}

}