Skip to content

Instantly share code, notes, and snippets.

@MonkeyDo
Last active December 19, 2022 15:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MonkeyDo/6b919302e97dc979f8277b9c71ff82ab to your computer and use it in GitHub Desktop.
Save MonkeyDo/6b919302e97dc979f8277b9c71ff82ab to your computer and use it in GitHub Desktop.
MusicBrainz UserScript: play on ListenBrainz
// ==UserScript==
// @name Show "play on ListenBrainz" button on MusicBrainz pages
// @description Add a button on some MusicBrainz pages to open ListenBrainz with a ready-to-play playlist (supported pages: Recording, Release, Release Group, Collection)
// @version 2022.12.19.0
// @namespace https://listenbrainz.org/
// @downloadURL https://gist.github.com/MonkeyDo/6b919302e97dc979f8277b9c71ff82ab
// @updateURL https://gist.github.com/MonkeyDo/6b919302e97dc979f8277b9c71ff82ab
// @include /musicbrainz.org\/(release|release-group|recording|collection)/
// @require https://cdnjs.cloudflare.com/ajax/libs/cash/8.1.2/cash.min.js
// @icon https://github.com/metabrainz/metabrainz-logos/raw/master/logos/ListenBrainz/PNG/ListenBrainz_logo_no_text.png
// @grant none
// @run-at document-start
// ==/UserScript==
// MBID regular expression
var MBIDRegexp = "[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}";
var regexp = new RegExp(`musicbrainz.org/(release|release-group|recording|collection)/(${MBIDRegexp})`, 'i');
var regexpResults = window.location.href.match(regexp);
// Bail early if we don't support this page
if(!regexpResults || !regexpResults.length){
return;
}
// This function gathers the recording MBIDs from a collection
function getCollectionRecordingMBIDs(){
var matches = $('#content table > tbody > tr > td:nth-child(2) > a')
.map(function(){
var recordingLinkRegexp = `^/recording/(${MBIDRegexp})`;
var match = $(this).attr("href").match(recordingLinkRegexp);
return match && match[1] || null;
})
return Array.from(matches);
}
// This function gathers the MBID of the first Release in from a Release Group page
function getFirstReleaseMBID(){
var releaseLinkRegexp = `^/release/(${MBIDRegexp})`;
var firstMatch = $('#content table > tbody > tr > td:nth-child(2) > a')
.first();
var mbidMatch = firstMatch.attr("href").match(releaseLinkRegexp);
return mbidMatch && mbidMatch[1] || null;
}
$(document).ready(function () {
var logoURL = "https://raw.githubusercontent.com/metabrainz/metabrainz-logos/master/logos/ListenBrainz/SVG/ListenBrainz_logo_icon.svg"
// Get the entity type and MBID from the URL
var entityType = regexpResults[1];
var entityMBID = regexpResults[2];
var LBLink;
// Depending on the entity type we need to hit different LB endpoints
switch(entityType){
case 'release':
LBLink = `//listenbrainz.org/player/release/${entityMBID}`;
break;
case 'release-group':
var releaseMBID = getFirstReleaseMBID();
LBLink = `//listenbrainz.org/player/release/${releaseMBID}`;
break;
case 'recording':
LBLink = `//listenbrainz.org/player?recording_mbids=${entityMBID}`;
break;
case 'collection':
var recordingMBIDs = getCollectionRecordingMBIDs();
// getCollectionRecordingMBIDs will return an empty array if no matches
if(recordingMBIDs.length){
LBLink = `//listenbrainz.org/player?recording_mbids=${recordingMBIDs.join(",")}`;
}
break;
}
// We couldn't construct a link to play on LB so don't show a button
if(!LBLink){
return;
}
$('#sidebar').prepend(
`<a class="styled-button" style="margin-bottom: 0.5rem; display: block; text-align: center;"
target="_blank"
href="${LBLink}">
<img src="${logoURL}" style="vertical-align: bottom; height: 16px;"> Play on ListenBrainz
</a>
`)
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment