pp for osu score pages

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

目前为 2014-11-04 提交的版本,查看 最新版本

// ==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);