Skip to content

Commit

Permalink
Added beatleader support
Browse files Browse the repository at this point in the history
  • Loading branch information
web-dev-sam committed Sep 16, 2023
1 parent ff416c7 commit 13d6e68
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@


# ScoreSaber Beatsaver Buttons
Chrome extension that adds the one-click install, twitch !bsr, beatsaver link and download buttons to the ScoreSaber leaderboards.
Chrome extension that adds a one-click install, twitch !bsr, BeatSaver/Beatleader link, and download button to the ScoreSaber leaderboards.


# How to install
Expand Down
6 changes: 3 additions & 3 deletions background.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

// Open a sample page where the buttons are added after installation
chrome.runtime.onInstalled.addListener(function() {
chrome.tabs.create({
url: 'https://scoresaber.com/leaderboard/371613',
});
// chrome.tabs.create({
// url: 'https://scoresaber.com/leaderboard/371613',
// });
});
Binary file modified demo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 34 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,41 +24,52 @@ async function load() {
// Get leaderboard id from url
const leaderboardId = window.location.pathname.split('/').pop();
if (leaderboardId == null) {
log(`Failed to get leaderboard ID from URL ${window.location.pathname}`);
return;
}

// Get leaderboardInfos from scoresaber leaderboard api
const leaderboardInfos = await fetchLeaderboardInfos(leaderboardId);
if (leaderboardInfos == null) {
log(`Failed to get leaderboard information from URL ${SCORESABER_LEADERBOARD_API.replace('{0}', leaderboardId)}`);
return;
}

// Get the songHash from the leaderboardInfos
const songHash = leaderboardInfos.songHash;
const songHash = leaderboardInfos.songHash?.toLowerCase();
if (!songHash) {
log(`Couldn't find song hash in leaderboard information:`, leaderboardInfos);
return;
}
const beatsaverURL = BEATSAVER_API.replace('{0}', songHash);
const downloadURL = `https://eu.cdn.beatsaver.com/${songHash.toLowerCase()}.zip`;

// Get the beatmap from beatsaver api
const beatmap = await fetchBeatmap(beatsaverURL);
const beatsaverURL = BEATSAVER_API.replace('{0}', songHash);
const beatleaderUIDFetchURL = `https://beatsaver.com/api/scores/${songHash}/1?difficulty=${leaderboardInfos.difficulty.difficulty}&type=BeatLeader`; // "?difficulty=9&gameMode=0&type=BeatLeader"
const [{ value: beatleader }, { value: beatmap }] = await Promise.allSettled([
safeFetch(beatleaderUIDFetchURL),
safeFetch(beatsaverURL),
]);
if (beatmap == null) {
log(`Failed to get beatsaver data from URL:`, beatsaverURL);
return;
}

const beatKey = beatmap.id;
if (beatKey == null) {
log(`Failed to get map bsr code from beatsaver data:`, beatmap, beatsaverURL);
return;
}

const quickInstallURL = QUICK_INSTALL_URL.replace('{0}', beatKey);
const bsrCopy = `!bsr ${beatKey}`;
const beatsaverPage = `https://beatsaver.com/maps/${beatKey}`;
const beatleaderLeaderboardURL = beatleader.uid ? `https://www.beatleader.xyz/leaderboard/global/${beatleader.uid}` : null;
const downloadURL = `https://eu.cdn.beatsaver.com/${songHash}.zip`;

log(beatsaverURL);
log(downloadURL);
log(quickInstallURL);
log(beatleaderLeaderboardURL);
log(bsrCopy);

// Add buttons
Expand All @@ -81,6 +92,19 @@ async function load() {
</svg>
</div>`;
const downloadButtonHTML = `<div class="download" title="Download Files"><i class="fas fa-download"></i></div>`;
const beatleaderLeaderboardHTML = `<div class="beatleader" title="Open BeatLeader Leaderboard"><svg width="269" height="254" viewBox="0 0 269 254" fill="none" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_761_37)">
<path d="M173.792 133.373C177.331 111.567 162.522 91.0201 140.716 87.481C118.91 83.942 98.3631 98.7506 94.824 120.557C91.285 142.363 106.094 162.91 127.9 166.449C149.706 169.988 170.253 155.179 173.792 133.373Z" fill="white"/>
<path d="M233.3 19.2C238.2 19.2 242.1 23.2 242.1 28V225.8C242.1 230.7 238.1 234.6 233.3 234.6H35.3999C30.4999 234.6 26.5999 230.6 26.5999 225.8V28C26.5999 23.1 30.5999 19.2 35.3999 19.2H233.3ZM233.3 0H35.3999C19.8999 0 7.3999 12.5 7.3999 28V225.9C7.3999 241.4 19.8999 253.9 35.3999 253.9H233.3C248.8 253.9 261.3 241.4 261.3 225.9V28C261.3 12.5 248.7 0 233.3 0Z" fill="white"/>
</g>
<defs>
<clipPath id="clip0_761_37">
<rect width="268.7" height="253.9" fill="white"/>
</clipPath>
</defs>
</svg>
</div>
`;
buttonContainer.innerHTML = `
<style>
.ssbtns-wrapper {
Expand Down Expand Up @@ -117,6 +141,7 @@ async function load() {
${twitchButtonHTML}
${quickInstallButtonHTML}
${beatsaverButtonHTML}
${beatleaderLeaderboardHTML}
${downloadButtonHTML}
</div>
`;
Expand All @@ -126,6 +151,7 @@ async function load() {
const quickInstallButton = buttonContainer.getElementsByClassName("quick")[0];
const downloadButton = buttonContainer.getElementsByClassName("download")[0];
const beatsaverButton = buttonContainer.getElementsByClassName("beatsaver")[0];
const beatleaderButton = buttonContainer.getElementsByClassName("beatleader")[0];

twitchButton.addEventListener("click", () => {
navigator.clipboard.writeText(bsrCopy);
Expand All @@ -143,6 +169,10 @@ async function load() {
window.open(downloadURL);
});

beatleaderButton.addEventListener("click", () => {
window.open(beatleaderLeaderboardURL);
});

return true;
}

Expand Down
2 changes: 1 addition & 1 deletion js/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function fetchLeaderboardInfos(leaderboardId) {
* Fetches the beatmap from beatsaver.
* @returns {Promise<Object|null>} The beatmap or null if fetching didn't succeed.
*/
function fetchBeatmap(beatsaverURL) {
function safeFetch(beatsaverURL) {
return new Promise(resolve => {
fetch(beatsaverURL).then(response => response.json()).then(data => {
if (data == null) {
Expand Down
4 changes: 2 additions & 2 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"manifest_version": 3,
"name": "ScoreSaber Buttons",
"description": "Adds the one-click install, twitch !bsr, beatsaver link and download buttons to the ScoreSaber leaderboards.",
"version": "0.0.1",
"description": "Adds the one-click install, twitch !bsr, BeatSaver/Beatleader link, and download buttons to the ScoreSaber leaderboards.",
"version": "0.0.3",
"permissions": [],
"icons": {
"16": "icons/16.png",
Expand Down
Binary file removed screenshot.png
Binary file not shown.
Binary file added webstoreAsset.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added webstoreAsset_marqueetile.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added webstoreAsset_smalltile.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 13d6e68

Please sign in to comment.