pp for osu score pages

shows pp data from osustats.ppy.sh on osu score pages.

目前為 2014-11-04 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name        pp for osu score pages
// @namespace   http://osustats.ppy.sh
// @description shows pp data from osustats.ppy.sh on osu score pages.
// @include     http*://osu.ppy.sh/b/*
// @include     http*://osu.ppy.sh/s/*
// @include     http*://osu.ppy.sh/p/beatmap?b=*
// @include     http*://osu.ppy.sh/p/beatmap?s=*
// @grant GM_setValue
// @grant GM_getValue
// @grant GM_xmlhttpRequest
// @version     3
// ==/UserScript==

 
var result=null;
var mapID=null;
var mapMode=null;
var scoresMissing=false;
var time = 10;
var interval;
var requestedUpdate=false;
function Start()
{
	scoresMissing=false;
	if(mapID!=null && mapMode!=null)
	{
		GetScores(mapID,mapMode,function(res){
			result = JSON.parse(res);
			UpdateOsuScoresTable();
			
			if(scoresMissing && !requestedUpdate)
			{
				RequestBeatmapUpdate(mapID,mapMode,function(accepted){
					if(accepted)
					{
						interval = setInterval(Countdown, 1000);
					}
				});
			}
			else
				if(requestedUpdate)
				{
					SetInfoText("Updated successfully");
				}
		});
	}

}
function Init()
{
	var mapTab = document.getElementsByClassName("beatmapTab active");
	if(mapTab.length==1)
	{
		mapID = mapTab[0].href.split("/")[4];
		mapID = mapID.split("&")[0];

		mapMode = document.getElementsByClassName("active");
		mapMode = mapMode[mapMode.length-1].href.split("&m=")[1];
	}
}
function UpdateOsuScoresTable()
{
	var rows,username,score,row,pp;

	rows = document.evaluate('//div[@class=\'beatmapListing\']/table/tbody/tr',
	  document,
	  null,
	  XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
	  null);
	rows.snapshotItem(0).children[5].innerHTML="Combo / pp";

	for (var i=1;i<rows.snapshotLength;i++) {
		row = rows.snapshotItem(i);
		username = row.children[4].children[1].innerHTML;
		score = row.children[2].innerHTML.replace(/,/g,'').replace(/<b>/g,'').replace(/<\/b>/g,'');
		pp = GetPpFromUsername(username,score);
		console.log(requestedUpdate);
		if(requestedUpdate)
		{
			row.children[5].innerHTML = row.children[5].innerHTML.substring(0, row.children[5].innerHTML.indexOf('/'));
		}
		row.children[5].innerHTML += pp; 
	}
}
function GetPpFromUsername(username,score)
{
	for (var i=0;i<result.length;i++) {
		if(username == result[i].name)
		{
			if(score == result[i].score)
			{
				return " / "+(Math.round(result[i].pp * 100) / 100)+"pp";
			}
			scoresMissing=true;
			return " / N/U";
		}
	}
	scoresMissing=true;
	return " / N/D";
}
function GetScores(mapID,mapMode,callback) 
{
	GetPage("http://osustats.ppy.sh/api/beatmap/getScores/"+ mapID + "/"+ mapMode,function(res){
		callback(res);
	});
	
}
function RequestBeatmapUpdate(mapID,mapMode,callback)
{
	GetPage("http://osustats.ppy.sh/api/beatmap/updateRequest/"+ mapID + "/"+ mapMode,function(res){
		requestedUpdate=true;
		res = JSON.parse(res);
		console.log(res.status);
		if(res.status=="OK"){
			callback(true);	
		}
		else{
			callback(false);
			SetInfoText("Request failed- try again later.");
		}
	});
}
function GetPage(url,callback)
{
	GM_xmlhttpRequest({
		method: "GET",
		url: url,
		synchronous: true,
		headers: {
			Referer: location.href
		},
		onload: function(resp) {
			callback(resp.responseText);
		}
	});
}
function Countdown()
{
	time--;
	SetInfoText("Missing scores detected- Update requested</br>Updating in "+time+" seconds");
	console.log(time);
	if(time == 0){
		clearInterval(interval);
		SetInfoText("Updating...");
		Start();
	}
}
function SetInfoText(text)
{
	rows = document.evaluate('//div[@class=\'content-with-bg\']/h2',
  	  document,
  	  null,
  	  XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
  	  null);
	row = rows.snapshotItem(0)
	row.previousElementSibling.innerHTML = "<h1 style=\"font: 30px; text-align: center;\">"+text+"</h1>";
}
window.addEventListener('load', function() {
	Init();
    Start();
}, false);