Greasy Fork

Mutual and suscribe to youself on osu!

Allows you to mutual and subscribe to your own osu profile

当前为 2022-03-09 提交的版本,查看 最新版本

// ==UserScript==
// @name         Mutual and suscribe to youself on osu!
// @namespace    osu
// @version      1.5
// @description  Allows you to mutual and subscribe to your own osu profile
// @author       Magnus Cosmos
// @include      https://osu.ppy.sh/*
// @grant        none
// @run-at       document-idle
// @require      http://code.jquery.com/jquery-3.4.1.min.js
// ==/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.onloadend = 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);
}

async function main() {
    var regex = /\/users\/(\d+)/g;
    var pathname = window.location.pathname;
    if (regex.test(pathname)) {
        var checkExist = setInterval(function() {
            if ($(".profile-detail-bar").length) {
                var temp = $('.profile-detail-bar')[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 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);
    }
}

(function() {
    var observer = new MutationObserver(function(mutations){
        for(const mutation of mutations) {
            if (mutation.addedNodes.length > 0) {
                for (const node of mutation.addedNodes) {
                    if ($(node).hasClass("osu-layout")) {
                        main();
                        break;
                    } else if (node.hasChildNodes()) {
                        if ($(node.firstChild).hasClass("osu-layout")) {
                            main();
                            break;
                        }
                    }
                }
            }
        }
    });
    observer.observe(document, {childList: true, subtree: true});
    $(document).ready(function(){
        var utma= getCookie("__utma");
        if (utma == undefined) {
            return;
        }
    });
})();