-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCopyRecordingsFromRelease.user.js
More file actions
212 lines (175 loc) · 9.08 KB
/
CopyRecordingsFromRelease.user.js
File metadata and controls
212 lines (175 loc) · 9.08 KB
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
// ==UserScript==
// @name MB: Copy Recordings From Release
// @namespace https://github.com/YoGo9
// @version 5/28/2026
// @description On the Recordings tab of the release editor, paste a release MBID or URL to auto-assign all recordings by track position
// @author YoGo9
// @homepage https://github.com/YoGo9/Scripts
// @updateURL https://raw.githubusercontent.com/YoGo9/Scripts/main/CopyRecordingsFromRelease.user.js
// @downloadURL https://raw.githubusercontent.com/YoGo9/Scripts/main/CopyRecordingsFromRelease.user.js
// @match *://*.musicbrainz.org/release/add*
// @match *://*.musicbrainz.org/release/*/edit*
// @grant none
// ==/UserScript==
(function () {
'use strict';
const MBID_RE = /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/i;
// ── UI injection ──────────────────────────────────────────────────────────
function injectUI() {
if (document.getElementById('cfr-widget')) return;
const anchor = document.querySelector('.changes');
if (!anchor) return;
const wrapper = document.createElement('div');
wrapper.id = 'cfr-widget';
wrapper.style.cssText = [
'margin: 12px 0 0 0',
'padding: 10px 12px',
'background: #f0f4ff',
'border: 1px solid #99a8d0',
'border-radius: 4px',
'font-size: 13px',
'clear: both',
].join(';');
wrapper.innerHTML =
'<strong style="display:block;margin-bottom:6px;">📋 Copy recordings from another release</strong>' +
'<div style="display:flex;gap:6px;align-items:center;flex-wrap:wrap;">' +
'<input id="cfr-input" type="text" placeholder="Paste release MBID or URL\u2026"' +
' style="flex:1;min-width:220px;padding:4px 6px;font-size:13px;border:1px solid #aaa;border-radius:3px;" />' +
'<button id="cfr-btn" type="button"' +
' style="padding:4px 10px;font-size:13px;cursor:pointer;border-radius:3px;border:1px solid #888;background:#e8eaf0;">' +
'Apply</button>' +
'</div>' +
'<div id="cfr-status" style="margin-top:5px;min-height:16px;font-style:italic;color:#555;"></div>';
anchor.appendChild(wrapper);
document.getElementById('cfr-btn').addEventListener('click', onApply);
document.getElementById('cfr-input').addEventListener('keydown', function (e) {
if (e.key === 'Enter') onApply();
});
}
// ── Helpers ───────────────────────────────────────────────────────────────
function setStatus(msg, color) {
var el = document.getElementById('cfr-status');
if (el) { el.textContent = msg; el.style.color = color || '#555'; }
}
async function onApply() {
var raw = (document.getElementById('cfr-input') || {}).value;
if (!raw || !raw.trim()) { setStatus('Please paste a release MBID or URL.', '#a00'); return; }
var match = raw.trim().match(MBID_RE);
if (!match) { setStatus('Could not find a valid MBID in the input.', '#a00'); return; }
var mbid = match[0];
setStatus('Fetching release ' + mbid + '\u2026');
var releaseData;
try {
var resp = await fetch('/ws/2/release/' + mbid + '?inc=recordings+artist-credits&fmt=json');
if (!resp.ok) throw new Error('HTTP ' + resp.status);
releaseData = await resp.json();
} catch (err) {
setStatus('Error fetching release: ' + err.message, '#a00');
return;
}
applyRecordings(releaseData);
}
// ── Apply recordings ──────────────────────────────────────────────────────
function applyRecordings(releaseData) {
// Build map: "mediumPos:trackPos" -> WS2 recording object
// WS2 medium.position and track.position are both 1-based integers
var trackMap = new Map();
(releaseData.media || []).forEach(function (medium) {
var medPos = medium.position;
(medium.tracks || []).forEach(function (track) {
if (track.recording) {
trackMap.set(medPos + ':' + track.position, track.recording);
}
});
});
if (!trackMap.size) {
setStatus('No recordings found in that release.', '#a00');
return;
}
var vm = getReleaseEditorVM();
if (!vm) {
setStatus('Could not access the release editor view-model.', '#a00');
return;
}
var release = vm.rootField.release();
if (!release) { setStatus('No release loaded in editor.', '#a00'); return; }
var applied = 0, skipped = 0, notFound = 0;
release.mediums().forEach(function (medium) {
var medPos = medium.position(); // KO observable, 1-based
medium.tracks().forEach(function (track) {
var trackPos = track.position(); // 1-based (0 = pregap)
var key = medPos + ':' + trackPos;
var recData = trackMap.get(key);
if (!recData) { notFound++; return; }
// Already has this exact recording
if (track.hasExistingRecording() && track.recording() && track.recording().gid === recData.id) {
skipped++;
return;
}
try {
// WS2 uses rec.id for MBIDs; MB.entity expects rec.gid
// WS2 artist-credit uses artist.id; MB.entity expects artist.gid
var names = (recData['artist-credit'] || [])
.filter(function (ac) { return ac && typeof ac === 'object' && ac.artist; })
.map(function (ac) {
return {
name: ac.name || ac.artist.name || '',
joinPhrase: ac.joinphrase || '',
artist: {
gid: ac.artist.id,
name: ac.artist.name || '',
sortName: ac.artist['sort-name'] || '',
entityType: 'artist',
},
};
});
var entityData = {
gid: recData.id,
name: recData.title,
length: recData.length || null,
artistCredit: { names: names },
};
var entity = MB.entity(entityData, 'recording');
track.recording(entity);
applied++;
} catch (e) {
console.error('[CFR] Error on track', trackPos, e);
notFound++;
}
});
});
var color = applied > 0 ? '#007700' : '#a00';
setStatus('Done: ' + applied + ' applied, ' + skipped + ' already set, ' + notFound + ' not matched.', color);
}
// ── Access the KO view-model ──────────────────────────────────────────────
// viewModel.js: MB.releaseEditor = { rootField: { release: ko.observable() } }
// init.js: MB._releaseEditor = releaseEditor (full instance)
function getReleaseEditorVM() {
try {
if (window.MB && window.MB.releaseEditor && window.MB.releaseEditor.rootField)
return window.MB.releaseEditor;
if (window.MB && window.MB._releaseEditor && window.MB._releaseEditor.rootField)
return window.MB._releaseEditor;
// Fallback: KO context walk
var changesDiv = document.querySelector('.changes[data-bind]');
if (changesDiv) {
var ctx = ko.contextFor(changesDiv);
if (ctx && ctx.$root && ctx.$root.rootField) return ctx.$root;
if (ctx && ctx.$parents) {
for (var i = 0; i < ctx.$parents.length; i++) {
if (ctx.$parents[i] && ctx.$parents[i].rootField) return ctx.$parents[i];
}
}
}
} catch (e) {
console.error('[CFR] getReleaseEditorVM error:', e);
}
return null;
}
// ── Tab-change observer ───────────────────────────────────────────────────
var observer = new MutationObserver(function () {
if (!document.getElementById('cfr-widget')) injectUI();
});
observer.observe(document.body, { childList: true, subtree: true });
injectUI();
})();