-
Notifications
You must be signed in to change notification settings - Fork 16
/
mb-reledit-set_relation_attrs.user.js
122 lines (109 loc) · 5.08 KB
/
mb-reledit-set_relation_attrs.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
/* global $ helper MB relEditor */
'use strict';
// ==UserScript==
// @name MusicBrainz relation editor: Set relation attributes
// @namespace mbz-loujine
// @author loujine
// @version 2024.11.25
// @downloadURL https://raw.githubusercontent.com/loujine/musicbrainz-scripts/master/mb-reledit-set_relation_attrs.user.js
// @updateURL https://raw.githubusercontent.com/loujine/musicbrainz-scripts/master/mb-reledit-set_relation_attrs.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 attributes (live, partial, solo...)
// @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 setAttributes = (targetType, attrName, toggle) => {
const attrType = Object.values(MB.linkedEntities.link_attribute_type).filter(
attr => attr.name === attrName
)[0];
relEditor.orderedSelectedRecordings().forEach(async (recording, recIdx) => {
await helper.delay(recIdx * 100);
recording.relationships.filter(
rel => rel.target_type === targetType
).forEach(async (rel, relIdx) => {
await helper.delay(relIdx * 10);
const attrs = rel.attributes;
const attr = attrs.filter(el => el.typeID === attrType.id);
if (!attr.length) {
attrs.push({
type: {gid: attrType.gid},
typeID: attrType.id,
typeName: attrType.name
});
} else if (toggle) {
attrs.splice(attrs.indexOf(attr[0]), 1);
}
const relType = rel.backward
? `${rel.target_type}-${rel.source_type}`
: `${rel.source_type}-${rel.target_type}`;
await helper.waitFor(() => !MB.relationshipEditor.relationshipDialogDispatch, 1);
document.getElementById(`edit-relationship-${relType}-${rel.id}`).click();
await helper.waitFor(() => !!MB.relationshipEditor.relationshipDialogDispatch, 1);
MB.relationshipEditor.relationshipDialogDispatch({
type: 'set-attributes',
attributes: attrs,
});
await helper.delay(1);
document.querySelector('.relationship-dialog button.positive').click();
});
});
};
(function displayToolbar() {
relEditor.container(document.querySelector('div.tabs'))
.insertAdjacentHTML('beforeend', `
<details id="relattrs_script_toggle">
<summary style="display: block;margin-left: 8px;cursor: pointer;">
<h3 style="display: list-item;">
Relation attributes
</h3>
</summary>
<div>
<h3>Recording-Work relation attributes</h3>
<table>
<tr>
<td><input type="button" id="setCover" value="Set cover"></td>
<td><input type="button" id="setLive" value="Set live"></td>
<td><input type="button" id="setPartial" value="Set partial"></td>
<td><input type="button" id="setInstrumental" value="Set instrumental"></td>
<td><input type="button" id="setMedley" value="Set medley"></td>
</tr>
<tr>
<td><input type="button" id="toggleCover" value="Toggle cover"></td>
<td><input type="button" id="toggleLive" value="Toggle live"></td>
<td><input type="button" id="togglePartial" value="Toggle partial"></td>
<td><input type="button" id="toggleInstrumental" value="Toggle instrumental"></td>
<td><input type="button" id="toggleMedley" value="Toggle medley"></td>
</tr>
</table>
<h3>Recording-Artist relation attributes</h3>
<input type="button" id="toggleSolo" value="Toggle solo">
<input type="button" id="toggleAdditional" value="Toggle additional">
<input type="button" id="toggleGuest" value="Toggle guest">
</div>
</details>
`);
})();
$(document).ready(function() {
for (const attr of ['Cover', 'Live', 'Partial', 'Instrumental', 'Medley']) {
document.getElementById(`set${attr}`).addEventListener('click', () => {
setAttributes('work', attr.toLowerCase(), false);
relEditor.editNote(GM_info.script);
});
document.getElementById(`toggle${attr}`).addEventListener('click', () => {
setAttributes('work', attr.toLowerCase(), true);
relEditor.editNote(GM_info.script);
});
}
for (const attr of ['Solo', 'Additional', 'Guest']) {
document.getElementById(`toggle${attr}`).addEventListener('click', () => {
setAttributes('artist', attr.toLowerCase(), true);
relEditor.editNote(GM_info.script);
});
}
return false;
});