Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: fetch info and change buttons for MBID only recordings #113

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 84 additions & 9 deletions acoustid-merge-recordings.user.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// @name MusicBrainz: merge recordings from acoustID page
// @namespace mbz-loujine
// @author loujine
// @version 2020.9.14
// @version 2024.12.2
// @downloadURL https://raw.githubusercontent.com/loujine/musicbrainz-scripts/master/acoustid-merge-recordings.user.js
// @updateURL https://raw.githubusercontent.com/loujine/musicbrainz-scripts/master/acoustid-merge-recordings.user.js
// @supportURL https://github.com/loujine/musicbrainz-scripts
Expand All @@ -23,6 +23,77 @@ function checkAll() {
});
}

function checkRecordings(undefined_recordings) {
// fetch info and change buttons for MBID only recordings
undefined_recordings.forEach((undefined_recording, idx) => {
setTimeout(() => {
GM_xmlhttpRequest({
method: 'GET',
url: `https://musicbrainz.org/ws/2/recording/${undefined_recording.mbid}?inc=artists&fmt=json`,
onreadystatechange: resp => {
const mbid = resp.finalUrl.split(/[/?&]/)[6];
let obsolete_elements = '';
if (resp.status == '404') {
if (resp.readyState === 2) {
// recording was deleted
obsolete_elements += '.mbmerge, form, .loading';
undefined_recording.tr.style.setProperty('background-color', '#fee');
undefined_recording.tr.cells[0].insertAdjacentHTML('afterbegin', 'deleted: ');
}
} else if (
mbid != undefined_recording.mbid
&& undefined_recording.tr.parentNode.querySelector(`tr[id='${mbid}']`)
) {
if (resp.readyState === 2) {
// recording was merged into a recording visible in this page
obsolete_elements += '.mbmerge, form, .loading';
Array.from(undefined_recording.tr.cells).forEach(cell => {
cell.style.setProperty('border', 'none');
});
undefined_recording.tr.style.setProperty('opacity', '.6');
undefined_recording.tr.cells[0].insertAdjacentHTML('afterbegin', '  └ merged: ');
undefined_recording.tr.parentNode.querySelector(`tr[id='${mbid}']`).insertAdjacentElement(
'afterend',
undefined_recording.tr.parentNode.removeChild(undefined_recording.tr)
);
}
} else {
if (resp.readyState == 4) {
// recording was merged into a recording not visible in this page
obsolete_elements += '.loading';
const response = JSON.parse(resp.responseText);
undefined_recording.tr.querySelector('a[href]').textContent = response.title;
undefined_recording.tr.cells[0].removeAttribute('colspan');
undefined_recording.tr.cells[0].insertAdjacentHTML(
'afterend',
`<td>${artistCreditToString(response['artist-credit'])}</td><td>${response.length ? msToDuration(response.length) : '?:??'}</td>`
);
}
}
if (obsolete_elements) {
undefined_recording.tr.querySelectorAll(obsolete_elements).forEach(element => {
element.parentNode.removeChild(element);
});
}
},
});
}, 1000 * idx);
});
}

function artistCreditToString(ac) {
let str = '';
for (let c = 0; c < ac.length; c++) {
str += ac[c].name + ac[c].joinphrase;
}
return str;
}

function msToDuration(durationMs) {
let d = new Date(durationMs);
return (d.getUTCHours() > 0 ? d.getUTCHours() + ":" + (d.getUTCMinutes() / 100).toFixed(2).slice(2) : d.getUTCMinutes()) + ":" + (d.getUTCSeconds() / 100).toFixed(2).slice(2) + (d.getUTCMilliseconds() > 0 ? "." + (d.getUTCMilliseconds() / 1000).toFixed(3).slice(2) : "");
}

function launchMerge() {
const ids = [];
if (document.querySelectorAll('.mbmerge:checked').length < 2) {
Expand Down Expand Up @@ -63,19 +134,23 @@ function launchMerge() {
<input id="checkAll" value="Select all" type="button">
</th>
`);
let undefined_recordings = [];
document.querySelectorAll('table a[href*="/recording/"]').forEach(node => {
const mbid = node.href.split('/')[4];
const tr = node.parentElement.parentElement;
if (
node.parentElement.tagName != 'I' &&
!tr.classList.contains('mbid-disabled')
) {
tr.insertAdjacentHTML(
'beforeend',
`<td><input class="mbmerge" value="${mbid}" type="checkbox"></td>`
const tr = node.closest('tr');
tr.insertAdjacentHTML(
'beforeend',
`<td><input class="mbmerge" value="${mbid}" type="checkbox"></td>`
);
if (node.parentElement.tagName == 'I') {
undefined_recordings.push({tr: tr, mbid: mbid});
tr.cells[0].insertAdjacentHTML(
'afterbegin',
'<span class="loading"><img src="https://musicbrainz.org/static/images/icons/loading.gif"> </span>'
);
}
});
checkRecordings(undefined_recordings);
document.getElementsByTagName('table')[1].insertAdjacentHTML('afterend', `
<input id="merge" value="Launch merge in MusicBrainz" type="button">
<span id="merge-text"></span>
Expand Down
Loading