Greasy Fork

osu! PP Save

A Simple script to save users pp to compare later.

当前为 2016-01-05 提交的版本,查看 最新版本

// ==UserScript==
// @name         osu! PP Save
// @version      1.0
// @description  A Simple script to save users pp to compare later.
// @author       Rafael Moreira Fonseca
// @match        https://osu.ppy.sh/*
// @icon         http://osu.ppy.sh/favicon.ico
// @namespace    https://twitter.com/RafaelMoreiraFo
// @grant        none
// ==/UserScript==

$(window).ready(function() {
    
    var pathname = $.trim(window.location.pathname); 

    // Create styles
    var styles = ".ppButton{background:rgba(0,0,0,0.07);border-radius:5px;border:1px solid rgba(0,0,0,0.07);padding: 1px 14px;font-size:13px;float:right;cursor:pointer;}";
        styles += ".deleteppButton{background: #ffe2fa;border-radius:5px;border: solid 1px #ffbff1;padding: 1px 14px;font-size:13px;float:right;cursor:pointer;}";
        styles += ".oldPP{font-size:80%;color:green;float:right;margin:2px 10px;}";
        styles += ".alertPP{font-size:80%;color:red;float:right;margin:2px 5px;}";
        styles += ".morePP{color: #3843a6;}";
        styles += ".arrow{font-family: Verdana,sans-serif;}";
        styles += ".saveAllPP{background:rgba(0,0,0,0.07);margin:3px;border-radius:5px;border:1px solid rgba(0,0,0,0.07);padding: 1px 14px;font-size:13px;float:right;cursor:pointer;}";
        styles += ".oldPPTable{color:green;margin-left:10px;float:right;}";
        styles += ".oldPPTableUpdated{color: #3843a6;margin-left:10px;float:right;}";
        styles += ".noPPTable{color:black;margin-left:10px;float:right;}";
    
    $('<style type="text/css">'+styles+'</style>').appendTo($('head'));
     
    // Some JSON useful functions
    Storage.prototype.getObject = function(key) {
        var value = this.getItem(key);
        return value && JSON.parse(value);
    }
    
    Storage.prototype.setObject = function(key, userName, gameMode, userValue) {
        var getValue = localStorage.getObject('usersPP');
        if(getValue == null){
            var getValue = {};
            getValue[userName] = {"gm-0":"", "gm-1":"", "gm-2":"", "gm-3":""};
        }else{
            if(getValue[userName] == null){
                getValue[userName] = {"gm-0":"", "gm-1":"", "gm-2":"", "gm-3":""};
            }
        }
        getValue[userName][gameMode] = userValue;
        this.setItem(key, JSON.stringify(getValue));
    }

    //Another Useful Functions
    $.urlParam = function(name){
        var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location.href);
        if (results==null){
            return null;
        }
        else{
            return results[1] || 0;
        }
    }
    
    //Osu! website useful functions
    function saveCurrentGameMode(userPP){
        var userName = $.trim($(".profile-username").html());
        var userCurrentMode = $(".profileGameModes").find(".active").attr("id");

        localStorage.setObject('usersPP', userName, userCurrentMode, userPP);
        $("#saveUserPP").css("border","1px solid green");
        
        showReloadAlert();
    }
    
    function deleteCurrentUser(userName){
        var getValue = localStorage.getObject('usersPP');
        delete getValue[userName];
        localStorage.setItem('usersPP', JSON.stringify(getValue));
        
        $("#deleteUserPP").css("border","1px solid red");
        showReloadAlert();
    }
    
    function showReloadAlert(){
        var oldPP = $('.oldPP');
        if(oldPP.length > 0){
            oldPP.addClass("alertPP");
            oldPP.html("Reload the page.");
        }else{
            $(".profileStatLine").first().append("<span class='oldPP alertPP'>Reload the page.</span>");
        }
    }
        
    function showOldPP(){
        var userName = $.trim($(".profile-username").html());
        var userCurrentMode = $(".profileGameModes").find(".active").attr("id");
        var getValue = localStorage.getObject('usersPP');
        if(getValue != null){
            var oldPP = getValue[userName][userCurrentMode];
            $(".profileStatLine").first().append("<span class='oldPP'>"+oldPP+"</span>");
        }
        
    }
    
    function countProgress(currentPP, currentPerformance){
        var userName = $.trim($(".profile-username").html());
        var userCurrentMode = $(".profileGameModes").find(".active").attr("id");
        var getValue = localStorage.getObject('usersPP');
        if(getValue != null){
            var oldPP = getValue[userName][userCurrentMode];
            if(oldPP != ""){
                var justOldPP = parsePP(oldPP);
                var justNewPP = parsePP(currentPP);
                var countPP = justNewPP - justOldPP;
                
                var splitPP = currentPP.split("(");
                var arrow = "&#x25B2;";
                if(countPP < 0){ arrow = "&#x25BC;"; }
                var newCurrentPP = ": " +splitPP[0] + " <span class='morePP'>(<span class='arrow'>" + arrow + "</span>" + countPP + "pp)</span> " + splitPP[1].replace(")","");
                
                $(".profileStatLine b").first().html(currentPerformance+newCurrentPP);
            }
        }
    }
    
    function parsePP(numberPP){
        return parseInt($.trim($.trim(numberPP.split("(")[0]).replace("pp","").replace(",",""))); 
    }
    
    // Userpage
    if(pathname.indexOf("/u/") > -1){
        
        // Variable to save PP
        var currentPP;
        var currentPerformance;
        
    	// Add the button when the game mode changes
    	document.getElementById("general").addEventListener("DOMNodeInserted", function (ev) {
            
            // Check general div
    		if($('#saveUserPP').length == 0 && $('.profileStatLine').first().length > 0 && $('#chart1').length > 0){
                
                // Save current PP
                currentPP = $.trim($.trim($(".profileStatLine b").html()).split(":")[2]);
                
                // Check play
                if(currentPP != "-"){
                    currentPerformance = $(".profileStatLine b a")[0].outerHTML;

                    // Show save button
                    $(".profileStatLine").first().append("<div id='saveUserPP' class='ppButton'>SavePP</div>");
                    
                    //Show delete button
                    var getValue = localStorage.getObject('usersPP');
                    
                    try{
                        
                        var userName = $.trim($(".profile-username").html());
                        
                        if(userName in getValue){
                            
                             // Show delete button
                            $("<div id='deleteUserPP' class='deleteppButton'>DeletePP</div>").insertAfter($(".profileStatLine").first());

                            // Add click event to the delete button
                            $("#deleteUserPP").click(function(){
                                var deleteConfirm = confirm("Are you sure you want to delete?");
                                if(deleteConfirm){
                                    deleteCurrentUser(userName);
                                }
                            });
                            
                        }
                        
                    }catch(e){ }

                    // Add click event to the button
                    $("#saveUserPP").click(function(){
                        saveCurrentGameMode(currentPP);
                    });

                    // Show the old PP
                    showOldPP();

                    // Count
                    countProgress(currentPP, currentPerformance);
                }
    		}
    		
    	}, false);

        
   	}else if(pathname.indexOf("/p/pp") > -1){
        
        //getCurrentGameMode
        var userCurrentMode = $.urlParam("m");
        if(userCurrentMode == null){
            userCurrentMode = 0;
        }
        userCurrentMode = "gm-"+$.trim(userCurrentMode);
        
        var getValue = localStorage.getObject('usersPP');
        
        $(".beatmapListing tr").each(function(){
            var classList = $(this).attr('class');
            if(classList == "row1p" || classList == "row2p"){
                
                var userName = $.trim($(this).children(":nth-child(2)").children("a").text());
                try{
                    if(getValue != null){

                        var oldPP = getValue[userName][userCurrentMode];
                        var currentPP = $.trim($(this).children(":nth-child(5)").text());
                        var oldPPCompare = $.trim(oldPP.split("(")[0]);
                        var classPP = "oldPPTable";
                        if(currentPP != oldPPCompare){
                            classPP = "oldPPTableUpdated";
                        }
                        $(this).children(":nth-child(5)").css("width","150px").children("span").append("<span class='"+classPP+"'>"+oldPP+"</span>");

                    }
                }catch(e){
                    $(this).children(":nth-child(5)").css("width","150px").children("span").append("<span class='noPPTable'> - </span>");
                }
            }
        });
        
    }
});