2015-01-08 18:37:49 +00:00
|
|
|
// ==UserScript==
|
|
|
|
// @name MusicBrainz: Expand/collapse release groups
|
|
|
|
// @description See what's inside a release group without having to follow its URL. Also adds convenient edit links for it.
|
|
|
|
// @namespace http://userscripts.org/users/266906
|
|
|
|
// @author Michael Wiencek <mwtuea@gmail.com>
|
2022-01-27 12:05:38 +00:00
|
|
|
// @version 2022.1.6.1
|
2015-01-08 18:37:49 +00:00
|
|
|
// @license GPL
|
2017-03-10 18:57:54 +00:00
|
|
|
// @downloadURL https://raw.githubusercontent.com/murdos/musicbrainz-userscripts/master/expand-collapse-release-groups.user.js
|
|
|
|
// @updateURL https://raw.githubusercontent.com/murdos/musicbrainz-userscripts/master/expand-collapse-release-groups.user.js
|
2015-09-04 17:21:01 +00:00
|
|
|
// @grant none
|
2015-01-08 18:37:49 +00:00
|
|
|
// @include *://musicbrainz.org/artist/*
|
|
|
|
// @include *://musicbrainz.org/label/*
|
|
|
|
// @include *://musicbrainz.org/release-group/*
|
2015-09-26 16:22:10 +00:00
|
|
|
// @include *://musicbrainz.org/series/*
|
2015-01-08 18:37:49 +00:00
|
|
|
// @include *://beta.musicbrainz.org/artist/*
|
|
|
|
// @include *://beta.musicbrainz.org/label/*
|
|
|
|
// @include *://beta.musicbrainz.org/release-group/*
|
2015-09-26 16:22:10 +00:00
|
|
|
// @include *://beta.musicbrainz.org/series/*
|
2015-01-08 18:37:49 +00:00
|
|
|
// @include *://test.musicbrainz.org/artist/*
|
|
|
|
// @include *://test.musicbrainz.org/label/*
|
|
|
|
// @include *://test.musicbrainz.org/release-group/*
|
2015-09-26 16:22:10 +00:00
|
|
|
// @include *://test.musicbrainz.org/series/*
|
2015-01-08 18:37:49 +00:00
|
|
|
// @match *://musicbrainz.org/artist/*
|
|
|
|
// @match *://musicbrainz.org/label/*
|
|
|
|
// @match *://musicbrainz.org/release-group/*
|
2015-09-26 16:22:10 +00:00
|
|
|
// @match *://musicbrainz.org/series/*
|
2015-01-08 18:37:49 +00:00
|
|
|
// @match *://beta.musicbrainz.org/artist/*
|
|
|
|
// @match *://beta.musicbrainz.org/label/*
|
|
|
|
// @match *://beta.musicbrainz.org/release-group/*
|
2015-09-26 16:22:10 +00:00
|
|
|
// @match *://beta.musicbrainz.org/series/*
|
2015-01-08 18:37:49 +00:00
|
|
|
// @match *://test.musicbrainz.org/artist/*
|
|
|
|
// @match *://test.musicbrainz.org/label/*
|
|
|
|
// @match *://test.musicbrainz.org/release-group/*
|
2015-09-26 16:22:10 +00:00
|
|
|
// @match *://test.musicbrainz.org/series/*
|
2015-09-04 17:21:01 +00:00
|
|
|
// @exclude *musicbrainz.org/label/*/*
|
|
|
|
// @exclude *musicbrainz.org/release-group/*/*
|
2015-09-26 16:22:10 +00:00
|
|
|
// @exclude *musicbrainz.org/series/*/*
|
2015-01-08 18:37:49 +00:00
|
|
|
// ==/UserScript==
|
|
|
|
|
2021-12-10 14:32:47 +00:00
|
|
|
const MBID_REGEX = /[0-9a-z]{8}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{12}/;
|
2015-01-08 18:37:49 +00:00
|
|
|
|
2021-12-10 14:32:47 +00:00
|
|
|
const releasesOrReleaseGroups = document.querySelectorAll("#content table.tbl > tbody > tr > td a[href^='/release']");
|
2021-12-10 15:16:41 +00:00
|
|
|
for (const entity of releasesOrReleaseGroups) {
|
|
|
|
const entityLink = entity.getAttribute('href');
|
2020-08-01 20:04:54 +00:00
|
|
|
if (entityLink.match(/\/release-group\//)) {
|
2021-12-10 15:16:41 +00:00
|
|
|
inject_release_group_button(entity.parentNode);
|
2020-08-01 20:04:54 +00:00
|
|
|
} else if (!entityLink.match(/\/cover-art/)) {
|
|
|
|
// avoid injecting a second button for a release's cover art link
|
2021-12-10 15:16:41 +00:00
|
|
|
inject_release_button(entity.parentNode);
|
2015-01-08 18:37:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function inject_release_group_button(parent) {
|
2018-11-20 22:18:49 +00:00
|
|
|
let mbid = parent.querySelector('a').href.match(MBID_REGEX),
|
|
|
|
table = document.createElement('table');
|
2015-01-08 18:37:49 +00:00
|
|
|
|
2018-11-20 22:18:49 +00:00
|
|
|
table.style.marginTop = '1em';
|
|
|
|
table.style.marginLeft = '1em';
|
|
|
|
table.style.paddingLeft = '1em';
|
2015-01-08 18:37:49 +00:00
|
|
|
|
2018-11-20 22:18:49 +00:00
|
|
|
let button = create_button(
|
|
|
|
`/ws/2/release?release-group=${mbid}&limit=100&inc=media&fmt=json`,
|
2020-04-05 14:01:21 +00:00
|
|
|
function (toggled) {
|
2018-11-20 22:18:49 +00:00
|
|
|
if (toggled) parent.appendChild(table);
|
|
|
|
else parent.removeChild(table);
|
2015-01-08 18:37:49 +00:00
|
|
|
},
|
2020-04-05 14:01:21 +00:00
|
|
|
function (json) {
|
2021-12-10 15:52:42 +00:00
|
|
|
parse_release_group(json, mbid, table);
|
2015-01-08 18:37:49 +00:00
|
|
|
},
|
2020-04-05 14:01:21 +00:00
|
|
|
function (status) {
|
2018-11-20 22:18:49 +00:00
|
|
|
table.innerHTML = `<tr><td style="color: #f00;">Error loading release group (HTTP status ${status})</td></tr>`;
|
2015-01-08 18:37:49 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
parent.insertBefore(button, parent.firstChild);
|
|
|
|
}
|
|
|
|
|
2015-01-20 17:05:56 +00:00
|
|
|
function inject_release_button(parent, _table_parent, _table, _mbid) {
|
2018-11-20 22:18:49 +00:00
|
|
|
let mbid = _mbid || parent.querySelector('a').href.match(MBID_REGEX),
|
|
|
|
table = _table || document.createElement('table');
|
2021-12-10 14:11:44 +00:00
|
|
|
let table_parent = _table_parent || parent; // fallback for pages where we do not inject the release groups
|
2015-01-20 17:05:56 +00:00
|
|
|
|
2018-11-20 22:18:49 +00:00
|
|
|
table.style.paddingLeft = '1em';
|
2015-01-08 18:37:49 +00:00
|
|
|
|
2018-11-20 22:18:49 +00:00
|
|
|
let button = create_button(
|
|
|
|
`/ws/2/release/${mbid}?inc=media+recordings+artist-credits&fmt=json`,
|
2020-04-05 14:01:21 +00:00
|
|
|
function (toggled) {
|
2021-12-10 14:11:44 +00:00
|
|
|
if (toggled) table_parent.appendChild(table);
|
|
|
|
else table_parent.removeChild(table);
|
2015-01-08 18:37:49 +00:00
|
|
|
},
|
2020-04-05 14:01:21 +00:00
|
|
|
function (json) {
|
2015-01-08 18:37:49 +00:00
|
|
|
parse_release(json, table);
|
|
|
|
},
|
2020-04-05 14:01:21 +00:00
|
|
|
function (status) {
|
2018-11-20 22:18:49 +00:00
|
|
|
table.innerHTML = `<tr><td style="color: #f00;">Error loading release (HTTP status ${status})</td></tr>`;
|
2015-01-08 18:37:49 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
parent.insertBefore(button, parent.childNodes[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
function create_button(url, dom_callback, success_callback, error_callback) {
|
2018-11-20 22:18:49 +00:00
|
|
|
let button = document.createElement('span'),
|
|
|
|
toggled = false;
|
|
|
|
|
|
|
|
button.innerHTML = '▶';
|
|
|
|
button.style.cursor = 'pointer';
|
|
|
|
button.style.marginRight = '4px';
|
|
|
|
button.style.color = '#777';
|
|
|
|
|
|
|
|
button.addEventListener(
|
|
|
|
'mousedown',
|
2020-04-05 14:01:21 +00:00
|
|
|
function () {
|
2018-11-20 22:18:49 +00:00
|
|
|
toggled = !toggled;
|
|
|
|
if (toggled) button.innerHTML = '▼';
|
|
|
|
else button.innerHTML = '▶';
|
|
|
|
dom_callback(toggled);
|
|
|
|
},
|
|
|
|
false
|
|
|
|
);
|
2015-01-08 18:37:49 +00:00
|
|
|
|
2018-11-20 22:18:49 +00:00
|
|
|
button.addEventListener(
|
|
|
|
'mousedown',
|
2020-04-05 14:01:21 +00:00
|
|
|
function () {
|
2018-11-20 22:18:49 +00:00
|
|
|
let this_event = arguments.callee;
|
|
|
|
button.removeEventListener('mousedown', this_event, false);
|
|
|
|
let req = new XMLHttpRequest();
|
|
|
|
|
2020-04-05 14:01:21 +00:00
|
|
|
req.onreadystatechange = function () {
|
2018-11-20 22:18:49 +00:00
|
|
|
if (req.readyState != 4) return;
|
|
|
|
|
|
|
|
if (req.status == 200 && req.responseText) {
|
|
|
|
success_callback(JSON.parse(req.responseText));
|
|
|
|
} else {
|
|
|
|
button.addEventListener(
|
|
|
|
'mousedown',
|
2020-04-05 14:01:21 +00:00
|
|
|
function () {
|
2018-11-20 22:18:49 +00:00
|
|
|
button.removeEventListener('mousedown', arguments.callee, false);
|
|
|
|
button.addEventListener('mousedown', this_event, false);
|
|
|
|
},
|
|
|
|
false
|
|
|
|
);
|
|
|
|
error_callback(req.status);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
req.open('GET', url, true);
|
|
|
|
req.send(null);
|
|
|
|
},
|
|
|
|
false
|
|
|
|
);
|
2015-01-08 18:37:49 +00:00
|
|
|
|
|
|
|
return button;
|
|
|
|
}
|
|
|
|
|
|
|
|
function format_time(ms) {
|
2018-11-20 22:18:49 +00:00
|
|
|
let ts = ms / 1000,
|
|
|
|
s = Math.round(ts % 60);
|
|
|
|
return `${Math.floor(ts / 60)}:${s >= 10 ? s : `0${s}`}`;
|
2015-01-08 18:37:49 +00:00
|
|
|
}
|
|
|
|
|
2021-12-10 15:52:42 +00:00
|
|
|
function parse_release_group(json, mbid, table) {
|
2018-11-20 22:18:49 +00:00
|
|
|
let releases = json.releases;
|
|
|
|
table.innerHTML = '';
|
2015-01-08 18:37:49 +00:00
|
|
|
|
2021-12-10 15:16:41 +00:00
|
|
|
for (const release of releases) {
|
|
|
|
let media = {},
|
2018-11-20 22:18:49 +00:00
|
|
|
tracks = [],
|
|
|
|
formats = [];
|
|
|
|
|
2021-12-10 15:16:41 +00:00
|
|
|
for (const medium of release.media) {
|
|
|
|
let format = medium.format,
|
2018-11-20 22:18:49 +00:00
|
|
|
count = medium['track-count'];
|
2021-12-10 15:16:41 +00:00
|
|
|
if (format) {
|
|
|
|
format in media ? (media[format] += 1) : (media[format] = 1);
|
|
|
|
}
|
2015-01-08 18:37:49 +00:00
|
|
|
tracks.push(count);
|
|
|
|
}
|
|
|
|
|
2021-12-10 14:32:47 +00:00
|
|
|
for (let format in media) {
|
|
|
|
let count = media[format];
|
2018-11-20 22:18:49 +00:00
|
|
|
if (count > 1) formats.push(`${count.toString()}×${format}`);
|
2015-01-08 18:37:49 +00:00
|
|
|
else formats.push(format);
|
|
|
|
}
|
|
|
|
|
2018-11-20 22:18:49 +00:00
|
|
|
release.tracks = tracks.join(' + ');
|
|
|
|
release.formats = formats.join(' + ');
|
2015-01-08 18:37:49 +00:00
|
|
|
}
|
|
|
|
|
2020-04-05 14:01:21 +00:00
|
|
|
releases.sort(function (a, b) {
|
2015-01-08 18:37:49 +00:00
|
|
|
if (a.date < b.date) return -1;
|
|
|
|
if (a.date > b.date) return 1;
|
|
|
|
return 0;
|
|
|
|
});
|
|
|
|
|
2021-12-10 15:16:41 +00:00
|
|
|
for (const release of releases) {
|
|
|
|
let track_tr = document.createElement('tr'),
|
|
|
|
track_td = document.createElement('td'),
|
|
|
|
track_table = document.createElement('table'),
|
|
|
|
format_td = document.createElement('td'),
|
|
|
|
tr = document.createElement('tr'),
|
|
|
|
td = document.createElement('td'),
|
|
|
|
a = createLink(`/release/${release.id}`, release.title);
|
|
|
|
|
|
|
|
track_td.colSpan = 6;
|
|
|
|
track_table.style.width = '100%';
|
|
|
|
track_table.style.marginLeft = '1em';
|
|
|
|
track_tr.appendChild(track_td);
|
|
|
|
inject_release_button(td, track_td, track_table, release.id);
|
|
|
|
td.appendChild(a);
|
|
|
|
if (release.disambiguation) {
|
|
|
|
td.appendChild(document.createTextNode(` (${release.disambiguation})`));
|
|
|
|
}
|
|
|
|
tr.appendChild(td);
|
|
|
|
format_td.innerHTML = release.formats;
|
|
|
|
tr.appendChild(format_td);
|
2015-01-08 18:37:49 +00:00
|
|
|
|
2021-12-10 15:16:41 +00:00
|
|
|
let columns = [release.tracks, release.date || '', release.country || '', release.status || ''];
|
|
|
|
for (const column of columns) {
|
|
|
|
tr.appendChild(createElement('td', column));
|
|
|
|
}
|
2015-01-08 18:37:49 +00:00
|
|
|
|
2021-12-10 15:16:41 +00:00
|
|
|
table.appendChild(tr);
|
|
|
|
table.appendChild(track_tr);
|
2018-11-20 22:18:49 +00:00
|
|
|
}
|
2015-01-08 18:37:49 +00:00
|
|
|
|
2018-11-20 22:18:49 +00:00
|
|
|
let bottom_tr = document.createElement('tr'),
|
|
|
|
bottom_td = document.createElement('td');
|
2015-01-08 18:37:49 +00:00
|
|
|
|
|
|
|
bottom_td.colSpan = 6;
|
2018-11-20 22:18:49 +00:00
|
|
|
bottom_td.style.padding = '1em';
|
2015-01-08 18:37:49 +00:00
|
|
|
|
2022-01-27 12:05:38 +00:00
|
|
|
bottom_td.appendChild(createNewTabLink(`/release-group/${mbid}/edit`, 'edit'));
|
2018-11-20 22:18:49 +00:00
|
|
|
bottom_td.appendChild(document.createTextNode(' | '));
|
2022-01-27 12:05:38 +00:00
|
|
|
bottom_td.appendChild(createNewTabLink(`/release/add?release-group=${mbid}`, 'add release'));
|
2018-11-20 22:18:49 +00:00
|
|
|
bottom_td.appendChild(document.createTextNode(' | '));
|
2022-01-27 12:05:38 +00:00
|
|
|
bottom_td.appendChild(createNewTabLink(`/release-group/${mbid}/edits`, 'editing history'));
|
2015-01-08 18:37:49 +00:00
|
|
|
|
|
|
|
bottom_tr.appendChild(bottom_td);
|
|
|
|
table.appendChild(bottom_tr);
|
|
|
|
}
|
|
|
|
|
|
|
|
function parse_release(json, table) {
|
2018-11-20 22:18:49 +00:00
|
|
|
let media = json.media;
|
|
|
|
table.innerHTML = '';
|
2015-01-08 18:37:49 +00:00
|
|
|
|
2018-11-20 22:18:49 +00:00
|
|
|
for (let i = 0; i < media.length; i++) {
|
|
|
|
let medium = media[i],
|
|
|
|
format = medium.format ? `${medium.format} ${i + 1}` : `Medium ${i + 1}`;
|
2015-01-08 18:37:49 +00:00
|
|
|
|
2018-11-20 22:18:49 +00:00
|
|
|
table.innerHTML += `<tr class="subh"><td colspan="4">${format}</td></tr>`;
|
2015-01-08 18:37:49 +00:00
|
|
|
|
2018-11-20 22:18:49 +00:00
|
|
|
for (let j = 0; j < medium.tracks.length; j++) {
|
|
|
|
let track = medium.tracks[j],
|
|
|
|
recording = track.recording,
|
|
|
|
disambiguation = recording.disambiguation ? ` (${recording.disambiguation})` : '',
|
2021-12-10 14:32:47 +00:00
|
|
|
length = track.length ? format_time(track.length) : '?:??',
|
|
|
|
artist_credit = track['artist-credit'] || track.recording['artist-credit'],
|
|
|
|
tr = document.createElement('tr');
|
2015-01-08 18:37:49 +00:00
|
|
|
|
2018-11-20 22:18:49 +00:00
|
|
|
tr.appendChild(createElement('td', j + 1));
|
|
|
|
let title_td = createElement('td', disambiguation);
|
|
|
|
title_td.insertBefore(createLink(`/recording/${recording.id}`, recording.title), title_td.firstChild);
|
2015-01-08 18:37:49 +00:00
|
|
|
tr.appendChild(title_td);
|
2018-11-20 22:18:49 +00:00
|
|
|
tr.appendChild(createElement('td', length));
|
|
|
|
let ac_td = document.createElement('td');
|
2015-01-08 18:37:49 +00:00
|
|
|
ac_td.appendChild(createAC(artist_credit));
|
|
|
|
tr.appendChild(ac_td);
|
|
|
|
|
|
|
|
table.appendChild(tr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-20 22:18:49 +00:00
|
|
|
let bottom_tr = document.createElement('tr'),
|
|
|
|
bottom_td = document.createElement('td');
|
2015-01-08 18:37:49 +00:00
|
|
|
|
|
|
|
bottom_td.colSpan = 4;
|
2018-11-20 22:18:49 +00:00
|
|
|
bottom_td.style.padding = '1em';
|
2015-01-08 18:37:49 +00:00
|
|
|
|
2022-01-27 12:05:38 +00:00
|
|
|
bottom_td.appendChild(createNewTabLink(`/release/${json.id}/edit`, 'edit'));
|
2018-11-20 22:18:49 +00:00
|
|
|
bottom_td.appendChild(document.createTextNode(' | '));
|
2022-01-27 12:05:38 +00:00
|
|
|
bottom_td.appendChild(createNewTabLink(`/release/${json.id}/edit-relationships`, 'edit relationships'));
|
2018-11-20 22:18:49 +00:00
|
|
|
bottom_td.appendChild(document.createTextNode(' | '));
|
2022-01-27 12:05:38 +00:00
|
|
|
bottom_td.appendChild(createNewTabLink(`/release/${json.id}/edits`, 'editing history'));
|
2020-04-04 15:03:00 +00:00
|
|
|
bottom_td.appendChild(document.createTextNode(' | '));
|
2022-01-27 12:05:38 +00:00
|
|
|
bottom_td.appendChild(createNewTabLink(`/release/${json.id}/add-cover-art`, 'add cover art'));
|
2015-01-08 18:37:49 +00:00
|
|
|
|
|
|
|
bottom_tr.appendChild(bottom_td);
|
|
|
|
table.appendChild(bottom_tr);
|
|
|
|
}
|
|
|
|
|
2021-12-10 15:16:41 +00:00
|
|
|
function createAC(artist_credit_array) {
|
2018-11-20 22:18:49 +00:00
|
|
|
let span = document.createElement('span');
|
2015-01-08 18:37:49 +00:00
|
|
|
|
2021-12-10 15:16:41 +00:00
|
|
|
for (const credit of artist_credit_array) {
|
|
|
|
let artist = credit.artist,
|
2018-11-20 22:18:49 +00:00
|
|
|
link = createLink(`/artist/${artist.id}`, credit.name || artist.name);
|
2015-01-08 18:37:49 +00:00
|
|
|
|
2018-11-20 22:18:49 +00:00
|
|
|
link.setAttribute('title', artist['sort-name']);
|
2015-01-08 18:37:49 +00:00
|
|
|
span.appendChild(link);
|
|
|
|
|
2018-11-20 22:18:49 +00:00
|
|
|
if (credit.joinphrase) span.appendChild(document.createTextNode(credit.joinphrase));
|
2015-01-08 18:37:49 +00:00
|
|
|
}
|
|
|
|
return span;
|
|
|
|
}
|
|
|
|
|
|
|
|
function createElement(name, text) {
|
2018-11-20 22:18:49 +00:00
|
|
|
let element = document.createElement(name);
|
2015-01-08 18:37:49 +00:00
|
|
|
element.textContent = text;
|
|
|
|
return element;
|
|
|
|
}
|
|
|
|
|
|
|
|
function createLink(href, text) {
|
2018-11-20 22:18:49 +00:00
|
|
|
let element = createElement('a', text);
|
2015-01-08 18:37:49 +00:00
|
|
|
element.href = href;
|
|
|
|
return element;
|
2015-09-26 16:22:10 +00:00
|
|
|
}
|
2022-01-27 12:05:38 +00:00
|
|
|
|
|
|
|
function createNewTabLink(href, text) {
|
|
|
|
let link = createLink(href, text);
|
|
|
|
link.target = '_blank';
|
|
|
|
return link;
|
|
|
|
}
|