Greasy Fork

Mutual and subscribe to youself on osu!

Allows you to friend and subscribe to your own osu profile

当前为 2021-09-06 提交的版本,查看 最新版本

// ==UserScript==
// @name         Mutual and subscribe to youself on osu!
// @namespace    osu
// @version      1.1
// @description  Allows you to friend and subscribe to your own osu profile
// @author       Magnus Cosmos
// @include      https://osu.ppy.sh/*
// @grant        none
// @run-at       document-idle
// ==/UserScript==

var isMutual = null;
var isSubscribed = null;

function getCookie(name) {
    var value = `; ${document.cookie}`;
    var parts = value.split(`; ${name}=`);
    if (parts.length === 2) {
        return parts.pop().split(';').shift();
    }
}

function numberWithCommas(x) {
    return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}

function FriendsClick(e) {
    var xsrf_token = getCookie("XSRF-TOKEN");
    var xhr = new XMLHttpRequest();
    var button = e.currentTarget;
    xhr.onload = function(data) {
        var curr_val = parseInt($(button).children()[1].innerText.replace(/,/g, ""));
        if (isMutual) {
            isMutual = false;
            $(button).children().first().html('<i class="fas fa-user-plus"></i>');
            $(button).children()[1].innerHTML = numberWithCommas(curr_val - 1);
            $(button).removeClass("user-action-button--mutual");
        } else {
            isMutual = true;
            $(button).children().first().html('<span class="user-action-button__icon user-action-button__icon--hover-visible"><i class="fas fa-user-times"></i></span><span class="user-action-button__icon user-action-button__icon--hover-hidden"><i class="fas fa-user-friends"></i></span>');
            $(button).children()[1].innerHTML = numberWithCommas(curr_val + 1);
            $(button).addClass("user-action-button--mutual");
        }
    };
    var regex = /\/users\/(\d+)/g;
    var user_id = regex.exec(window.location.pathname)[1];
    $(button).children().first().html('<div class="la-ball-clip-rotate"></div>');
    if (isMutual) {
        xhr.open('DELETE', `https://osu.ppy.sh/home/friends/${user_id}`, true);
    } else {
        xhr.open('POST', `https://osu.ppy.sh/home/friends?target=${user_id}`, true);
    }
    xhr.setRequestHeader('x-requested-with', 'XMLHttpRequest');
    xhr.setRequestHeader('x-csrf-token', xsrf_token);
    xhr.withCredentials = true;
    xhr.send();
}

function SubscribeClick(e) {
    var xsrf_token = getCookie("XSRF-TOKEN");
    var xhr = new XMLHttpRequest();
    var regex = /\/users\/(\d+)/g;
    var user_id = regex.exec(window.location.pathname)[1];
    var formData = new FormData();
    formData.append("follow[notifiable_id]", user_id)
    formData.append("follow[notifiable_type]", "user")
    formData.append("follow[subtype]", "mapping")
    var button = e.currentTarget;
    xhr.onload = function(data) {
        var curr_val = parseInt($(button).children()[1].innerText.replace(/,/g, ""));
        if (isSubscribed) {
            isSubscribed = false;
            $(button).children()[1].innerHTML = numberWithCommas(curr_val - 1);
            $(button).removeClass("user-action-button--friend");
        } else {
            isSubscribed = true;
            $(button).children()[1].innerHTML = numberWithCommas(curr_val + 1);
            $(button).addClass("user-action-button--friend");
        }
        $(button).children().first().html('<i class="fas fa-bell"></i>');
    };
    $(button).children().first().html('<div class="la-ball-clip-rotate"></div>');
    if (isSubscribed) {
        xhr.open('DELETE', "https://osu.ppy.sh/home/follows", true);
    } else {
        xhr.open('POST', "https://osu.ppy.sh/home/follows", true);
    }
    xhr.setRequestHeader('x-requested-with', 'XMLHttpRequest');
    xhr.setRequestHeader('x-csrf-token', xsrf_token);
    xhr.withCredentials = true;
    xhr.send(formData);
}

(function() {
    $(document).ready(function(){
        var encid = getCookie("_encid");
        if (encid == undefined) {
            return;
        }
        var current = null;
        setInterval(function(){
            if(current != document.body){
                current = document.body;
                var regex = /\/users\/(\d+)/g;
                var pathname = window.location.pathname;
                if (regex.test(pathname)) {
                    var checkExist = setInterval(function() {
                        if ($(".profile-detail-bar__entry").length) {
                            var temp = $('.profile-detail-bar__entry')[0].children[0];
                            var title = $(temp).attr("title");
                            if (title) {
                                if (title != "followers") {
                                    clearInterval(checkExist);
                                    return;
                                }
                            } else {
                                title = $(temp).attr("data-orig-title");
                                if (title && title != "followers") {
                                    clearInterval(checkExist);
                                    return;
                                }
                            }
                            clearInterval(checkExist);
                            var el = $('.profile-detail-bar__entry div button');
                            el[0].disabled = false;
                            $(el[0]).on("click", FriendsClick);
                            el[1].disabled = false;
                            $(el[1]).on("click", SubscribeClick);
                            if ($(el[0]).hasClass("user-action-button--mutual")) {
                                isMutual = true;
                                $(el[0]).children().first().html('<span class="user-action-button__icon user-action-button__icon--hover-visible"><i class="fas fa-user-times"></i></span><span class="user-action-button__icon user-action-button__icon--hover-hidden"><i class="fas fa-user-friends"></i></span>');
                            } else {
                                isMutual = false;
                                $(el[0]).children().first().html('<i class="fas fa-user-plus"></i>');
                            }
                            if ($(el[1]).hasClass("user-action-button--friend")) {
                                isSubscribed = true;
                            } else {
                                isSubscribed = false;
                            }
                        }
                    }, 100);
                }
            }
        }, 200);
    });
})();