-
Notifications
You must be signed in to change notification settings - Fork 16
/
acoustid-merge-recordings.user.js
89 lines (85 loc) · 3.32 KB
/
acoustid-merge-recordings.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/* globals $ */
'use strict';
// ==UserScript==
// @name MusicBrainz: merge recordings from acoustID page
// @namespace mbz-loujine
// @author loujine
// @version 2020.9.14
// @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
// @icon https://raw.githubusercontent.com/loujine/musicbrainz-scripts/master/icon.png
// @description musicbrainz.org: merge recordings from acoustID page
// @compatible firefox+tampermonkey
// @license MIT
// @include http*://acoustid.org/track/*
// @grant GM_xmlhttpRequest
// @run-at document-end
// ==/UserScript==
function checkAll() {
document.querySelectorAll('.mbmerge').forEach(function (node) {
node.checked = true;
});
}
function launchMerge() {
const ids = [];
if (document.querySelectorAll('.mbmerge:checked').length < 2) {
document.getElementById('merge-text')
.textContent = 'You must merge at least two recordings';
return;
}
document.querySelectorAll('.mbmerge:checked').forEach((node, idx) => {
setTimeout(() => {
GM_xmlhttpRequest({
method: 'GET',
url: `https://musicbrainz.org/ws/js/entity/${node.value}`,
timeout: 1000,
onload: resp => {
document.getElementById(
'merge-text'
).textContent = `Fetched internal id for recording ${idx}`;
ids.push(JSON.parse(resp.responseText).id);
},
});
}, 1000 * idx);
});
setTimeout(function () {
const url =
'https://musicbrainz.org/recording/merge_queue?add-to-merge=' +
ids.join('&add-to-merge=');
document.getElementById('merge-text').textContent =
'Opening merge page';
console.log('Merge URL is ' + url);
window.open(url);
}, document.querySelectorAll('.mbmerge:checked').length * 1000 + 1000);
}
(function displayButtons () {
document.getElementsByTagName('table')[1].children[0].children[0].insertAdjacentHTML(
'beforeend', `
<th>Merge selection
<input id="checkAll" value="Select all" type="button">
</th>
`);
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>`
);
}
});
document.getElementsByTagName('table')[1].insertAdjacentHTML('afterend', `
<input id="merge" value="Launch merge in MusicBrainz" type="button">
<span id="merge-text"></span>
`);
})();
$(document).ready(function () {
document.getElementById('checkAll').addEventListener('click', checkAll);
document.getElementById('merge').addEventListener('click', launchMerge);
return false;
});