-
Notifications
You must be signed in to change notification settings - Fork 16
/
mb-reledit-set_rec_artist_as_writer.user.js
141 lines (132 loc) · 4.39 KB
/
mb-reledit-set_rec_artist_as_writer.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
/* global $ helper MB relEditor requests server */
'use strict';
// ==UserScript==
// @name MusicBrainz relation editor: Set writer relation from recording artist
// @namespace mbz-loujine
// @author loujine
// @version 2024.11.25
// @downloadURL https://raw.githubusercontent.com/loujine/musicbrainz-scripts/master/mb-reledit-set_rec_artist_as_writer.user.js
// @updateURL https://raw.githubusercontent.com/loujine/musicbrainz-scripts/master/mb-reledit-set_rec_artist_as_writer.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: Set writer relation from recording artist
// @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 fillWriterDialog = async (work, track, artistCredit) => {
const writerLinkType = MB.linkedEntities.link_type[server.workLinkType.composer];
MB.relationshipEditor.dispatch({
type: 'update-dialog-location',
location: {
batchSelection: false,
source: work,
track: track,
},
});
await helper.waitFor(() => !!MB.relationshipEditor.relationshipDialogDispatch, 1);
MB.relationshipEditor.relationshipDialogDispatch({
type: 'update-link-type',
source: work,
action: {
type: 'update-autocomplete',
source: work,
action: {
type: 'select-item',
item: {
id: writerLinkType.id,
name: writerLinkType.name,
type: 'option',
entity: writerLinkType,
}
},
},
});
await helper.delay(10);
MB.relationshipEditor.relationshipDialogDispatch({
type: 'update-target-entity',
source: work,
action: {
type: 'update-autocomplete',
source: work,
action: {
type: 'select-item',
item: {
type: 'option',
entity: artistCredit.artist,
id: artistCredit.artist.id,
name: artistCredit.name,
},
},
},
});
await helper.delay(10);
MB.relationshipEditor.relationshipDialogDispatch({
type: 'update-target-entity',
source: work,
action: {
type: 'update-credit',
action: {
type: 'set-credit',
creditedAs: artistCredit.name,
},
},
});
await helper.delay(10);
if (document.querySelector('.relationship-dialog p.error')) {
console.error('Dialog error, probably an identical relation already exists');
document.querySelector('.relationship-dialog button.negative').click();
} else {
document.querySelector('.relationship-dialog button.positive').click();
}
};
const applyWriter = () => {
relEditor.orderedSelectedRecordings().forEach(async (recording, recIdx) => {
const medium = MB.relationshipEditor.state.mediumsByRecordingId.get(recording.id)[0];
const mediumIdx = medium.position - 1;
const trackIdx = medium.tracks.filter(t => t.recording.id === recording.id)[0].position -1;
const track = MB.relationshipEditor.state.entity.mediums[mediumIdx].tracks[trackIdx];
await helper.delay(recIdx * 1000);
requests.GET(`/ws/js/entity/${recording.gid}`, resp => {
const artistCredits = JSON.parse(resp).artistCredit.names;
recording.relationships.filter(
rel => rel.target_type === 'work'
).map(async (rel, relIdx) => {
await helper.delay(relIdx * 400);
const work = rel.target;
artistCredits.forEach(async (credit, creditIdx) => {
await helper.delay(creditIdx * 300);
fillWriterDialog(work, track, credit);
});
});
});
});
};
(function displayToolbar() {
relEditor.container(
document.querySelector('div.tabs')
).insertAdjacentHTML('beforeend', `
<h3>
<span>
Set writer on selected recordings
</span>
</h3>
<div>
<input type="button" id="setWriter" value="Apply">
</div>
`);
})();
$(document).ready(function () {
let appliedNote = false;
document.getElementById('setWriter').addEventListener('click', () => {
applyWriter();
if (!appliedNote) {
relEditor.editNote(GM_info.script);
appliedNote = true;
}
});
return false;
});