forked from loujine/musicbrainz-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmb-reledit-clone_relations.user.js
267 lines (246 loc) · 10.5 KB
/
mb-reledit-clone_relations.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/* global $ MB relEditor requests */
'use strict';
// ==UserScript==
// @name MusicBrainz relation editor: Clone recording relations onto other recordings
// @namespace mbz-loujine
// @author loujine
// @version 2022.1.28
// @downloadURL https://raw.githubusercontent.com/loujine/musicbrainz-scripts/master/mb-reledit-clone_relations.user.js
// @updateURL https://raw.githubusercontent.com/loujine/musicbrainz-scripts/master/mb-reledit-clone_relations.user.js
// @supportURL https://github.com/loujine/musicbrainz-scripts
// @icon https://raw.githubusercontent.com/loujine/musicbrainz-scripts/master/icon.png
// @description musicbrainz.org relation editor: Clone recording relations onto other recordings
// @compatible firefox+tampermonkey
// @license MIT
// @require https://raw.githubusercontent.com/loujine/musicbrainz-scripts/master/mbz-loujine-common.js
// @include http*://*musicbrainz.org/release/*/edit-relationships
// @grant none
// @run-at document-end
// ==/UserScript==
const MBID_REGEX = /[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}/;
const cloneIdxHelp = `Index(es) to clone relationships from.
Indexes start at 1 and are related to the list of selected recordings, not the tracklist indexes.
Indexes can be empty (= 1, first selected), a number n or a range n1-n2.
Relations are cloned by looping over the 'source' recordings (e.g. '1-2' on 7 selected recordings
will clone the relations of R1 to R3,R5,R7 and the ones from R2 to R4,R6
`;
function autoComplete(nodeId, entityType) {
const $input = $(`input#${nodeId}`);
const match = $input.val().match(MBID_REGEX);
if (match) {
const mbid = match[0];
requests.GET(`/ws/2/${entityType}/${mbid}?fmt=json`, data => {
data = JSON.parse(data);
$input.data('mbid', mbid);
$input.val(data.title || data.name);
$input.css('background', '#bbffbb');
});
} else {
$input.data().mbid = "";
$input.css('background', '#ffaaaa');
}
}
function autoCompleteRec() {
return autoComplete('cloneExtRecording', 'recording');
}
function autoCompleteRel() {
return autoComplete('cloneExtRelease', 'release');
}
function cloneAR(refIdx) {
let startIdx;
let range;
if (refIdx.includes('-')) {
startIdx = parseInt(refIdx.split('-')[0]) - 1;
range = parseInt(refIdx.split('-')[1]) - startIdx;
} else {
startIdx = parseInt(refIdx) - 1 || 0;
range = 1;
}
const vm = MB.releaseRelationshipEditor;
const selectedRecordings = MB.relationshipEditor.UI.checkedRecordings();
const sourceRecordings = selectedRecordings.splice(startIdx, range);
let dialog;
selectedRecordings.map((rec, idx) => {
const sourceRecording = sourceRecordings[idx % sourceRecordings.length];
const sourceRels = sourceRecording.relationships().filter(
rel => !['recording-recording', 'recording-work'].includes(rel.entityTypes)
);
sourceRels.map(sourceRel => {
dialog = new MB.relationshipEditor.UI.AddDialog({
viewModel: vm,
source: rec,
target: sourceRel.entities().filter(ent => ent.entityType !== 'recording')[0],
});
dialog.accept(); // apparently required for Firefox
dialog.relationship().linkTypeID(sourceRel.linkTypeID());
const attrs = sourceRel.attributes();
attrs.map(attr => {
if (attr.creditedAs()) {
attr.credited_as = attr.creditedAs();
}
});
dialog.relationship().setAttributes(attrs);
dialog.relationship().entity0_credit(sourceRel.entity0_credit());
dialog.relationship().entity1_credit(sourceRel.entity1_credit());
if (sourceRel.begin_date) {
dialog.relationship().begin_date.year(sourceRel.begin_date.year());
dialog.relationship().begin_date.month(sourceRel.begin_date.month());
dialog.relationship().begin_date.day(sourceRel.begin_date.day());
}
if (sourceRel.end_date) {
dialog.relationship().end_date.year(sourceRel.end_date.year());
dialog.relationship().end_date.month(sourceRel.end_date.month());
dialog.relationship().end_date.day(sourceRel.end_date.day());
}
dialog.accept();
});
});
}
function cloneExtAR(recMBID) {
if (recMBID.split('/').length > 1) {
recMBID = recMBID.split('/')[4];
}
const vm = MB.releaseRelationshipEditor;
const selectedRecordings = MB.relationshipEditor.UI.checkedRecordings();
let dialog;
requests.GET(`/ws/js/entity/${recMBID}?inc=rels`, resp => {
const sourceRels = JSON.parse(resp).relationships.filter(
rel => rel.target_type != 'work'
);
selectedRecordings.map(rec => {
sourceRels.map(sourceRel => {
dialog = new MB.relationshipEditor.UI.AddDialog({
viewModel: vm,
source: rec,
target: sourceRel.target,
});
dialog.accept(); // apparently required for Firefox
dialog.relationship().linkTypeID(sourceRel.linkTypeID);
dialog.relationship().setAttributes(sourceRel.attributes);
dialog.relationship().entity0_credit(sourceRel.entity0_credit);
dialog.relationship().entity1_credit(sourceRel.entity1_credit);
if (sourceRel.target_type === 'recording'
&& sourceRel.backward) {
dialog.changeDirection();
}
if (sourceRel.begin_date) {
dialog.relationship().begin_date.year(sourceRel.begin_date.year);
dialog.relationship().begin_date.month(sourceRel.begin_date.month);
dialog.relationship().begin_date.day(sourceRel.begin_date.day);
}
if (sourceRel.end_date) {
dialog.relationship().end_date.year(sourceRel.end_date.year);
dialog.relationship().end_date.month(sourceRel.end_date.month);
dialog.relationship().end_date.day(sourceRel.end_date.day);
}
dialog.accept();
});
});
});
}
function cloneReleaseExtAR(relMBID) {
if (relMBID.split('/').length > 1) {
relMBID = relMBID.split('/')[4];
}
const vm = MB.releaseRelationshipEditor;
const release = MB.entity({entityType: 'release', gid: document.URL.split('/')[4]});
let dialog;
requests.GET(`/ws/js/entity/${relMBID}?inc=rels`, resp => {
const sourceRels = JSON.parse(resp).relationships.filter(
rel => rel.target_type != 'url'
);
sourceRels.map(sourceRel => {
dialog = new MB.relationshipEditor.UI.AddDialog({
viewModel: vm,
source: release,
target: sourceRel.target,
});
dialog.relationship().linkTypeID(sourceRel.linkTypeID);
dialog.relationship().setAttributes(sourceRel.attributes);
dialog.relationship().entity0_credit(sourceRel.entity0_credit);
dialog.relationship().entity1_credit(sourceRel.entity1_credit);
if (sourceRel.begin_date) {
dialog.relationship().begin_date.year(sourceRel.begin_date.year);
dialog.relationship().begin_date.month(sourceRel.begin_date.month);
dialog.relationship().begin_date.day(sourceRel.begin_date.day);
}
if (sourceRel.end_date) {
dialog.relationship().end_date.year(sourceRel.end_date.year);
dialog.relationship().end_date.month(sourceRel.end_date.month);
dialog.relationship().end_date.day(sourceRel.end_date.day);
}
dialog.accept();
});
});
}
(function displayToolbar() {
relEditor.container(document.querySelector('div.tabs'))
.insertAdjacentHTML('beforeend', `
<details id="clone_rels_script_toggle">
<summary style="display: block;margin-left: 8px;cursor: pointer;">
<h3 style="display: list-item;">
Clone recording relations to selected recordings
</h3>
</summary>
<div>
<span>
Source recording index in selection:
</span>
<input type="text" id="cloneRef" placeholder="empty or n or 'n1-n2'">
<span title="${cloneIdxHelp}">🛈</span>
<span>OR recording link: </span>
<input type="text" id="cloneExtRecording" placeholder="recording mbid">
<br />
<input type="button" id="cloneAR" value="Apply">
</div>
</details>
<details id="clone_release_rels_script_toggle">
<summary style="display: block;margin-left: 8px;cursor: pointer;">
<h3 style="display: list-item;">
Clone release relations from another release
</h3>
</summary>
<div>
<span>release link: </span>
<input type="text" id="cloneExtRelease" placeholder="release mbid">
<br />
<input type="button" id="cloneReleaseAR" value="Apply">
</div>
</details>
`);
})();
$(document).ready(function () {
$('input#cloneExtRecording').on('input', autoCompleteRec);
$('input#cloneExtRelease').on('input', autoCompleteRel);
$('input#cloneRef').on('input', () => {
document.getElementById('cloneExtRecording').value = '';
autoCompleteRec();
});
let appliedNote = false;
document.getElementById('cloneAR').addEventListener('click', () => {
const recMBID = $('input#cloneExtRecording').data('mbid');
if (recMBID) {
cloneExtAR(recMBID);
} else {
const refIdx = document.getElementById('cloneRef').value;
cloneAR(refIdx);
}
if (!appliedNote) {
relEditor.editNote(GM_info.script);
appliedNote = true;
}
});
document.getElementById('cloneReleaseAR').addEventListener('click', () => {
const relMBID = $('input#cloneExtRelease').data('mbid');
if (relMBID) {
cloneReleaseExtAR(relMBID);
}
if (!appliedNote) {
relEditor.editNote(GM_info.script, 'Cloned from https://musicbrainz.org/release/' + relMBID);
appliedNote = true;
}
});
return false;
});