osu top ranks data adder

Adds beatmap difficulty data/images/sound previews to user top ranks

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         osu top ranks data adder
// @namespace    
// @version      0.1
// @description  Adds beatmap difficulty data/images/sound previews to user top ranks
// @author       Piotrekol
// @match        *://osu.ppy.sh/u/*
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
// @grant GM_xmlhttpRequest
// ==/UserScript==
/* jshint -W097 */
'use strict';
//==============================
//==============================
//==============================
var YOUR_OSU_API_KEY = "FILL WITH YOUR KEY THERE";
//==============================
//==============================
//==============================
//Do not edit below unless you know what you are doing

var imageField = "<td width=\"61\">\
					<a href=\"#\" onclick=\"playBeatmapPreview({0}); return false;\">\
					<img  height=\"45\" src=\"https://b.ppy.sh/thumb/{0}.jpg\">\
					</a>\
					</td>";
var dataField = "</br>\
					<b>AR</b> {0}"+
	                " <b>CS</b> {1}"+
	                " <b>OD</b> {2}"+
	                " <b>HP</b> {3}"+
	                " ({4}<div class=\"starfield\" style=\"width:14px; display:inline-block;\"><div class=\"active\" style=\"width:14px\"></div></div>)";
var useTest=false;
if(useTest){
	var highlightScore = function(LookFor,node){
		if(node.text().indexOf(LookFor) > -1)
			node.css({
                        "background-color": "#B0C4DE"
                    });
	}
}

function createObserver(target,cfg,nodeCallback){
    var observer = new MutationObserver(function( mutations ) {
        mutations.forEach(function( mutation ) {
            var newNodes = mutation.addedNodes;
            if( newNodes !== null ) {
                var $nodes = $( newNodes );
                $nodes.each(function() {
                    nodeCallback($(this));
                });
            }
        });    
    });
    try{
    	observer.observe(target, cfg);
	}catch(e){}
}


function run(){
	var target_top = $( "#leader" )[0];
	var target_fav = $("#beatmaps")[0];
	createObserver(target_top,{ 
					childList: true, 
					subtree: true
					},function(node){
	    var node = node.find(".h b a[href]")[0];

	    if (typeof node != 'undefined')
	    {
	        var mapID = node.attributes.href.value.split("/")[2].split("?")[0];
	        GetPage("https://osu.ppy.sh/api/get_beatmaps?k="+YOUR_OSU_API_KEY+"&b="+mapID,0,function(data){
	            data = JSON.parse(data);
	            $(node.parentNode.parentNode.parentNode.parentNode).prepend(imageField.format(data[0].beatmapset_id));
	            $(node.parentNode.parentNode).append(dataField.format(data[0].diff_approach,data[0].diff_size,
	            	data[0].diff_overall,data[0].diff_drain,(Math.round(data[0].difficultyrating*100)/100)));
	            
				if(useTest){
	            	highlightScore("HR",$(node.parentNode.parentNode.parentNode.parentNode));
	            }
	        });
	    }
	});
}

function GetPage(url,retryNumber,callback) {
    GM_xmlhttpRequest({
        method: "GET",
        url: url,
        synchronous: false,
        timeout: 10000,
        headers: {
            Referer: location.href
        },
        onload: function(resp) {
            callback(resp.responseText);
        },
        ontimeout: function() {
            if(retryNumber<1){
            	retryNumber++;
            	setTimeout(function(){
            		GetPage(url,retryNumber,callback);
            	},1500);
            }else{
            	callback(null);
            }
        }
    });
}
if (!String.prototype.format) {
  String.prototype.format = function() {
    var args = arguments;
    return this.replace(/{(\d+)}/g, function(match, number) { 
      return typeof args[number] != 'undefined'
        ? args[number]
        : match
      ;
    });
  };
}

run();