ScoreSaber OneClick install button

Add OneClick Install for BeatSaber songs on ScoreSaber

当前为 2019-12-26 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         ScoreSaber OneClick install button
// @namespace    https://github.com/Invertex/
// @version      1.1
// @description  Add OneClick Install for BeatSaber songs on ScoreSaber
// @author       Invertex
// @license      GPL-3.0-or-later; http://www.gnu.org/licenses/gpl-3.0.txt
// @match        https://scoresaber.com/leaderboard/*
// @require      http://code.jquery.com/jquery-3.4.1.min.js
// @grant        GM.xmlHttpRequest
// @connect      beatsaver.com
// ==/UserScript==

(function() {
    'use strict';

    let songInfoElem = document.querySelector("html body div.section div.container div.content div.columns.is-desktop.is-flex-reverse div.column.is-one-third-desktop div.box.has-shadow");
    let boldElems = songInfoElem.getElementsByTagName("B");

    if(boldElems.length > 0)
    {
        let songIDElem = boldElems[boldElems.length - 1];
        GetOneClickLink(songIDElem, SetupOneClickButton);
    }
})();


function GetOneClickLink(idElem, response)
{
    GM.xmlHttpRequest({
        method: "GET",
        url: "https://beatsaver.com/api/search/text/0?q=" + idElem.innerText,
        responseType: "json",
        headers: { "Content-type" : "application/json" },

        onload: function(e)
        {
            let results = e.response;

            if(results.totalDocs > 0)
            {
                var key = results.docs[0].key;
                response(idElem,"beatsaver://" + key, key);
            }
        }
    });
}

function SetupOneClickButton(idElem, clickUrl, key)
{
    var button = document.createElement("button");
    button.innerText = "OneClick™ Install";
    button.addEventListener("click", function() { window.location = clickUrl }, false);
    idElem.parentNode.appendChild(button);
    $(idElem).wrap("<a href='http://beatsaver.com/beatmap/" + key + "'></a>");
}