-
Notifications
You must be signed in to change notification settings - Fork 16
/
mb-edit-replace_rec_artist_from_release_page.user.js
249 lines (240 loc) · 9.24 KB
/
mb-edit-replace_rec_artist_from_release_page.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/* global $ requests server helper sidebar edits */
'use strict';
// ==UserScript==
// @name MusicBrainz edit: Replace recording artists from a Release page
// @namespace mbz-loujine
// @author loujine
// @version 2023.3.9
// @downloadURL https://raw.githubusercontent.com/loujine/musicbrainz-scripts/master/mb-edit-replace_rec_artist_from_release_page.user.js
// @updateURL https://raw.githubusercontent.com/loujine/musicbrainz-scripts/master/mb-edit-replace_rec_artist_from_release_page.user.js
// @supportURL https://github.com/loujine/musicbrainz-scripts
// @icon https://raw.githubusercontent.com/loujine/musicbrainz-scripts/master/icon.png
// @description musicbrainz.org: Replace associated recording artist from a Release page
// @compatible firefox+tampermonkey
// @license MIT
// @require https://raw.githubusercontent.com/loujine/musicbrainz-scripts/master/mbz-loujine-common.js
// @include http*://*musicbrainz.org/release/*
// @exclude http*://*musicbrainz.org/release/add
// @exclude http*://*musicbrainz.org/release/*/edit
// @exclude http*://*musicbrainz.org/release/*/edit-relationships
// @grant none
// @run-at document-end
// ==/UserScript==
const editNoteMsg =
'CSG: Set performer(s) in recording relations as recording artist\n';
function showSelectors() {
const $rows = $(
$('table.tbl a[href*="/artist/"]').parents('tr').get().reverse()
);
if (!$('#selectorColumn').length) {
$('.subh').append('<th id="selectorColumn">rels</th>');
}
$rows.each(function (idx, tr) {
const mbid = $(tr).find('a[href*="/recording/"]')
.attr('href').split('/')[2];
$(tr).append(
$('<td>').append(
$('<input>', {
'id': 'replace-' + mbid,
'class': 'replace',
'type': 'checkbox',
'value': 'Replace artist',
})
)
);
});
let lastChecked;
$('.replace').click(function (evt) {
let currentChecked = $('.replace').index(evt.target);
if (evt.shiftKey && lastChecked !== undefined) {
if (lastChecked > currentChecked) {
// es6 syntax
[lastChecked, currentChecked] = [currentChecked, lastChecked];
}
$('.replace').slice(lastChecked,
currentChecked).prop('checked', evt.target.checked);
}
lastChecked = $('.replace').index(evt.target);
});
}
// Replace composer -> performer as recording artist (CSG)
function parseArtistEditData(data, performers) {
if (
performers.length === 0 &&
!document.getElementById('set-unknown').checked
) {
// erase completely data to prevent the POST request
return null;
} else if (performers.length === 0) {
// set [unknown] artist credit
data['artist_credit.names.0.name'] = '[unknown]';
data['artist_credit.names.0.join_phrase'] = '';
data['artist_credit.names.0.artist.name'] = '[unknown]';
data['artist_credit.names.0.artist.id'] = server.unknownArtistId;
return data;
}
performers.forEach(function (performer, idx) {
let creditedName = performer.name;
if (performer.creditedName) {
creditedName = performer.creditedName;
}
data['artist_credit.names.' + idx + '.name'] = edits.encodeName(creditedName);
data['artist_credit.names.' + idx + '.join_phrase'] = (idx === performers.length - 1)
? null : ',+';
data['artist_credit.names.' + idx + '.artist.name'] = edits.encodeName(performer.name);
data['artist_credit.names.' + idx + '.artist.id'] = performer.id;
});
return data;
}
function parseEditData(editData) {
const data = {};
const performers = [];
const uniqueIds = [];
data['name'] = edits.encodeName(editData.name);
if (editData.video === true) {
data['video'] = '1';
}
if (!editData.isrcs.length) {
data['isrcs.0'] = null;
} else {
editData.isrcs.forEach(function (isrc, idx) {
data['isrcs.' + idx] = isrc.isrc;
});
}
editData.relationships.forEach(function (rel) {
const linkType = rel.linkTypeID;
const filterPending = document.getElementById('pending').checked
? !rel.editsPending
: true;
const skipMinorRels = document.getElementById('skip-minor-rels').checked;
if (
server.performingLinkTypes(skipMinorRels).includes(linkType) &&
!uniqueIds.includes(rel.target.id) &&
filterPending &&
rel.target.name !== '[unknown]'
) {
uniqueIds.push(rel.target.id); // filter duplicates
performers.push({
'name': rel.target.name,
'creditedName': rel.entity0_credit,
'id': rel.target.id,
'link': linkType,
'mbid': rel.target.gid,
});
}
});
data['edit_note'] = $('#batch_replace_edit_note')[0].value;
if (document.getElementById('set-unknown').checked) {
data['edit_note'] = data['edit_note'].replace(
editNoteMsg,
'Set [unknown] performer when no rel'
);
}
data.make_votable = document.getElementById('votable').checked ? '1' : '0';
return parseArtistEditData(data, performers.sort(helper.comparefct));
}
function replaceArtist() {
$('.replace:input:checked:enabled').each(function (idx, node) {
const mbid = node.id.replace('replace-', '');
const url = edits.urlFromMbid('recording', mbid);
function success(xhr) {
const $status = $('#' + node.id + '-text');
node.disabled = true;
$status.text(
'Success (code ' + xhr.status + ')'
).parent().css('color', 'green');
const editId = new RegExp(
'/edit/(\\d+)">edit</a>'
).exec(xhr.responseText)[1];
$status.after(
$('<p>').append(
`<a href="/edit/${editId}" target="_blank">` +
`edit ${editId}</a>`
)
)
}
function fail(xhr) {
$('#' + node.id + '-text').text(
'Error (code ' + xhr.status + ')'
).parent().css('color', 'red');
}
function callback(editData) {
$('#' + node.id + '-text').text('Sending edit data');
const postData = parseEditData(editData);
console.info('Data ready to be posted: ', postData);
if (postData === null) {
$('#' + node.id + '-text').text('No artist data to send');
return;
}
requests.POST(
url,
edits.formatEdit('edit-recording', postData),
success,
fail
);
}
setTimeout(function () {
$('#' + node.id + '-text').empty();
$(node).after(
`<span id="${node.id}-text">Fetching required data</span>`
);
edits.getEditParams(url, callback);
}, 2 * idx * server.timeout);
});
}
(function displaySidebar() {
if (!helper.isUserLoggedIn()) {
return;
}
sidebar.container().insertAdjacentHTML('beforeend', `
<details>
<summary id="replace_script" style="display: block;margin-left: 8px;cursor: pointer;">
<h3 style="display: list-item;">
Replace artists
</h3>
</summary>
<div>
<p>First select recordings to update</p>
<input type="button" id="batch_select" value="Select all">
<table>
<tr>
<td><label for="pending">Exclude rels with pending edits</label></td>
<td><input type="checkbox" id="pending"></td>
</tr>
<tr>
<td><label for="votable">Make all edits votable</label></td>
<td><input type="checkbox" id="votable"></td>
</tr>
<tr>
<td><label for="set-unknown">Set [unknown] artist if no rel</label></td>
<td><input type="checkbox" id="set-unknown"></td>
</tr>
<tr>
<td><label for="skip-minor-rels">Skip minor rels (e.g. concertmaster)</label></td>
<td><input type="checkbox" id="skip-minor-rels"></td>
</tr>
</table>
<br />Edit note:
<textarea id="batch_replace_edit_note">
${sidebar.editNote(GM_info.script, editNoteMsg)}
</textarea>
<input type="button" id="batch_replace" value="Replace selected artists">
</div>
</details>
`);
})();
$(document).ready(function () {
if (!helper.isUserLoggedIn()) {
return false;
}
document.getElementById('replace_script').addEventListener('click', () => {
if (!document.getElementById('selectorColumn')) {
showSelectors();
}
});
document.getElementById('batch_select').addEventListener('click', () => {
$('.replace:input').filter(':visible').prop('checked', true);
});
document.getElementById('batch_replace').addEventListener('click', replaceArtist);
return false;
});