-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmb-reledit-copy_dates.user.js
158 lines (143 loc) · 5.34 KB
/
mb-reledit-copy_dates.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
/* global $ helper MB relEditor server */
'use strict';
// ==UserScript==
// @name MusicBrainz relation editor: Copy dates on recording relations
// @namespace mbz-loujine
// @author loujine
// @version 2024.11.25
// @downloadURL https://raw.githubusercontent.com/loujine/musicbrainz-scripts/master/mb-reledit-copy_dates.user.js
// @updateURL https://raw.githubusercontent.com/loujine/musicbrainz-scripts/master/mb-reledit-copy_dates.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: Copy/remove dates on recording relations
// @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==
/** look for one recording relation with a date
* give priority to the most precise one (day > month > year)
*/
const referenceDate = relations => {
for (const rel of relations) {
if (Object.values(server.recordingLinkType).includes(parseInt(rel.linkTypeID))) {
for (const prop of ['end_date', 'begin_date']) {
if (rel[prop] !== null) {
for (const unit of ['day', 'month', 'year']) {
if (rel[prop][unit] !== null) {
return rel;
}
}
}
}
}
}
return -1;
};
async function applyNewDate(rel, dateProps) {
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: 'update-date-period',
action: {
type: 'update-begin-date',
action: {
type: 'set-date',
date: relEditor.parseDate(dateProps.begin_date),
},
},
});
MB.relationshipEditor.relationshipDialogDispatch({
type: 'update-date-period',
action: {
type: 'update-end-date',
action: {
type: 'set-date',
date: relEditor.parseDate(dateProps.end_date),
},
},
});
MB.relationshipEditor.relationshipDialogDispatch({
type: 'update-date-period',
action: {
type: 'set-ended',
enabled: dateProps.ended,
},
});
await helper.delay(1);
document.querySelector('.relationship-dialog button.positive').click();
}
const propagateDates = (replace) => {
relEditor.orderedSelectedRecordings().forEach(async (recording, recIdx) => {
await helper.delay(recIdx * 100);
let relIdx = 0;
const refRel = referenceDate(recording.relationships);
if (refRel === -1) {
return;
}
recording.relationships.map(async rel => {
// TODO do not touch relations pending removal
if (!Object.values(server.recordingLinkType).includes(parseInt(rel.linkTypeID))) {
return;
}
if (!replace && (rel.begin_date || rel.end_date)) {
return;
}
relIdx += 1;
await helper.delay(relIdx * 10);
await applyNewDate(rel, refRel);
});
});
};
const removeDates = () => {
relEditor.orderedSelectedRecordings().forEach(async (recording, recIdx) => {
await helper.delay(recIdx * 100);
recording.relationships.forEach(async (rel, relIdx) => {
await helper.delay(relIdx * 10);
if (rel.begin_date === null && rel.end_date === null && !rel.ended) {
return;
}
await applyNewDate(rel, {begin_date: null, end_date: null, ended: false});
});
});
};
(function displayToolbar() {
relEditor.container(document.querySelector('div.tabs'))
.insertAdjacentHTML('beforeend', `
<h3>Dates</h3>
<label for="replaceDates">Replace dates if pre-existing: </label>
<input type="checkbox" id="replaceDates">
<br />
<input type="button" id="copyDates" value="Copy dates">
<input type="button" id="removeDates" value="Removes dates">
`);
})();
$(document).ready(function () {
let appliedNote = false;
document.getElementById('removeDates').addEventListener('click', () => {
removeDates();
if (!appliedNote) {
relEditor.editNote(GM_info.script);
appliedNote = true;
}
});
document.getElementById('copyDates').addEventListener('click', () => {
propagateDates(
document.querySelector('input#replaceDates').checked
);
if (!appliedNote) {
relEditor.editNote(
GM_info.script,
'Propagate recording dates from other relationships'
);
appliedNote = true;
}
});
return false;
});