// ==UserScript== // @name Mutual and subscribe to youself on osu! // @namespace osu // @version 1.3 // @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 // @require http://code.jquery.com/jquery-3.4.1.min.js // @downloadURL none // ==/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(''); $(button).children()[1].innerHTML = numberWithCommas(curr_val - 1); $(button).removeClass("user-action-button--mutual"); } else { isMutual = true; $(button).children().first().html('
'); 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(''); }; $(button).children().first().html(''); 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 utma= getCookie("__utma"); if (utma == 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(' '); $(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(' '); } else { isMutual = false; $(el[0]).children().first().html(''); } if ($(el[1]).hasClass("user-action-button--friend")) { isSubscribed = true; } else { isSubscribed = false; } } }, 100); } } }, 200); }); })();