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>
|
2018-02-18 17:16:45 +00:00
|
|
|
// @version 2018.2.18.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==
|
|
|
|
|
2015-11-25 16:21:04 +00:00
|
|
|
var 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
|
|
|
|
2016-02-09 11:19:20 +00:00
|
|
|
var releasesOrReleaseGroups = document.querySelectorAll("#content table.tbl > tbody > tr > td a[href^='/release']");
|
2015-11-25 16:21:04 +00:00
|
|
|
for (var r = 0; r < releasesOrReleaseGroups.length; r++) {
|
2018-11-20 22:18:49 +00:00
|
|
|
if (releasesOrReleaseGroups[r].getAttribute('href').match(/\/release-group\//)) {
|
2015-11-25 16:21:04 +00:00
|
|
|
inject_release_group_button(releasesOrReleaseGroups[r].parentNode);
|
|
|
|
} else {
|
|
|
|
inject_release_button(releasesOrReleaseGroups[r].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) {
|
2015-01-08 18:37:49 +00:00
|
|
|
parse_release_group(json, mbid, parent, 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 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');
|
2015-01-20 17:05:56 +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/${mbid}?inc=media+recordings+artist-credits&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) {
|
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
|
|
|
}
|
|
|
|
|
|
|
|
function parse_release_group(json, mbid, parent, table) {
|
2018-11-20 22:18:49 +00:00
|
|
|
let releases = json.releases;
|
|
|
|
table.innerHTML = '';
|
2015-01-08 18:37:49 +00:00
|
|
|
|
|
|
|
for (var i = 0; i < releases.length; i++) {
|
2018-11-20 22:18:49 +00:00
|
|
|
let release = releases[i],
|
|
|
|
media = {},
|
|
|
|
tracks = [],
|
|
|
|
formats = [];
|
|
|
|
|
|
|
|
for (let j = 0; j < release.media.length; j++) {
|
|
|
|
var medium = release.media[j],
|
|
|
|
format = medium.format,
|
|
|
|
count = medium['track-count'];
|
|
|
|
if (format) format in media ? (media[format] += 1) : (media[format] = 1);
|
2015-01-08 18:37:49 +00:00
|
|
|
tracks.push(count);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (format in media) {
|
2018-11-20 22:18:49 +00:00
|
|
|
var count = media[format],
|
|
|
|
txt;
|
|
|
|
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;
|
|
|
|
});
|
|
|
|
|
2018-11-20 22:18:49 +00:00
|
|
|
for (var i = 0; i < releases.length; i++) {
|
2020-04-05 14:01:21 +00:00
|
|
|
(function (release) {
|
2018-11-20 22:18:49 +00:00
|
|
|
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
|
|
|
|
2018-11-20 22:18:49 +00:00
|
|
|
let columns = [release.tracks, release.date || '', release.country || '', release.status || ''];
|
2015-01-08 18:37:49 +00:00
|
|
|
|
2018-11-20 22:18:49 +00:00
|
|
|
for (let i = 0; i < columns.length; i++) tr.appendChild(createElement('td', columns[i]));
|
2015-01-08 18:37:49 +00:00
|
|
|
|
2018-11-20 22:18:49 +00:00
|
|
|
table.appendChild(tr);
|
|
|
|
table.appendChild(track_tr);
|
|
|
|
})(releases[i]);
|
|
|
|
}
|
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
|
|
|
|
2018-11-20 22:18:49 +00:00
|
|
|
bottom_td.appendChild(createLink(`/release-group/${mbid}/edit`, 'edit'));
|
|
|
|
bottom_td.appendChild(document.createTextNode(' | '));
|
|
|
|
bottom_td.appendChild(createLink(`/release/add?release-group=${mbid}`, 'add release'));
|
|
|
|
bottom_td.appendChild(document.createTextNode(' | '));
|
|
|
|
bottom_td.appendChild(createLink(`/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})` : '',
|
|
|
|
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
|
|
|
|
2018-11-20 22:18:49 +00:00
|
|
|
bottom_td.appendChild(createLink(`/release/${json.id}/edit`, 'edit'));
|
|
|
|
bottom_td.appendChild(document.createTextNode(' | '));
|
|
|
|
bottom_td.appendChild(createLink(`/release/${json.id}/edit-relationships`, 'edit relationships'));
|
|
|
|
bottom_td.appendChild(document.createTextNode(' | '));
|
|
|
|
bottom_td.appendChild(createLink(`/release/${json.id}/edits`, 'editing history'));
|
2015-01-08 18:37:49 +00:00
|
|
|
|
|
|
|
bottom_tr.appendChild(bottom_td);
|
|
|
|
table.appendChild(bottom_tr);
|
|
|
|
}
|
|
|
|
|
|
|
|
function createAC(obj) {
|
2018-11-20 22:18:49 +00:00
|
|
|
let span = document.createElement('span');
|
2015-01-08 18:37:49 +00:00
|
|
|
|
2018-11-20 22:18:49 +00:00
|
|
|
for (let i = 0; i < obj.length; i++) {
|
|
|
|
let credit = obj[i],
|
|
|
|
artist = credit.artist,
|
|
|
|
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
|
|
|
}
|