-
Notifications
You must be signed in to change notification settings - Fork 26
/
mb_MASS-MERGE-RECORDINGS.user.js
1399 lines (1399 loc) · 70.5 KB
/
mb_MASS-MERGE-RECORDINGS.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ==UserScript==
// @name mb. MASS MERGE RECORDINGS
// @version 2024.10.25
// @description musicbrainz.org: Merges selected or all recordings from release A to release B – List all RG recordings
// @namespace https://github.com/jesus2099/konami-command
// @supportURL https://community.metabrainz.org/t/merge-duplicate-recordings-between-two-editions-of-the-same-album-with-mb-mass-merge-recordings/203168?u=jesus2099
// @downloadURL https://github.com/jesus2099/konami-command/raw/master/mb_MASS-MERGE-RECORDINGS.user.js
// @author jesus2099
// @licence CC-BY-NC-SA-4.0; https://creativecommons.org/licenses/by-nc-sa/4.0/
// @licence GPL-3.0-or-later; http://www.gnu.org/licenses/gpl-3.0.txt
// @since 2011-12-13; https://web.archive.org/web/20131103163401/userscripts.org/scripts/show/120382 / https://web.archive.org/web/20141011084015/userscripts-mirror.org/scripts/show/120382
// @icon data:image/gif;base64,R0lGODlhEAAQAMIDAAAAAIAAAP8AAP///////////////////yH5BAEKAAQALAAAAAAQABAAAAMuSLrc/jA+QBUFM2iqA2ZAMAiCNpafFZAs64Fr66aqjGbtC4WkHoU+SUVCLBohCQA7
// @require https://github.com/jesus2099/konami-command/raw/198d05ea555257eaf8a2a8d8333a8cdeda28d8ad/lib/CONTROL-POMME.js?version=2024.10.25
// @require https://github.com/jesus2099/konami-command/raw/89dce29b9cce6e92e552f7d8ce2f5cb0ed161f2a/lib/MB-JUNK-SHOP.js?version=2024.10.13
// @require https://github.com/jesus2099/konami-command/raw/de88f870c0e6c633e02f32695e32c4f50329fc3e/lib/SUPER.js?version=2022.3.24.224
// @grant none
// @include /^https?:\/\/(\w+\.)?musicbrainz\.org\/release\/[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}(\/(disc\/\d+)?)?(\?tport=\d+)?(#.*)?$/
// @include /^https?:\/\/(\w+\.)?musicbrainz\.org\/release-group\/[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}(\?|$)/
// @run-at document-end
// ==/UserScript==
"use strict";
let userjs = {
id: "MMR2099userjs120382", // linked to mb_INLINE-STUFF
name: GM_info.script.name.substr(4).replace(/\s/g, "\u00a0"),
version: GM_info.script.version
};
/* - --- - --- - --- - START OF CONFIGURATION - --- - --- - --- - */
/* COLOURS */
var cOK = "greenyellow";
var cNG = "pink";
var cInfo = "gold";
var cWarning = "yellow";
var cMerge = "#fcc";
var cCancel = "#cfc";
/* - --- - --- - --- - END OF CONFIGURATION - --- - --- - --- - */
var safeLengthDelta = 4;
var largeSpread = 15; // MBS-7417 / https://github.com/metabrainz/musicbrainz-server/blob/217111e3a12b705b9499e7fdda6be93876d30fb0/lib/MusicBrainz/Server/Edit/Utils.pm#L467
var lastTick = new Date().getTime();
var MBSminimumDelay = 1000;
var retryDelay = 2000;
var currentButt;
var MBS = location.protocol + "//" + location.host;
var sidebar = document.getElementById("sidebar");
var recid2trackIndex = {remote: {}, local: {}}; // recid:tracks index
var mergeQueue = []; // contains next mergeButts
var sregex_MBID = "[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}";
var regex_MBID = new RegExp(sregex_MBID, "i");
var css_track = "td:not(.pos):not(.video) > a[href^='/recording/'], td:not(.pos):not(.video) > :not(div):not(.ars) a[href^='/recording/']";
var css_track_ac = "td:not(.pos):not(.title):not(.rating):not(.treleases)";
var css_collapsed_medium = "div#content table.tbl.medium > thead > tr > th > a.expand-medium > span.expand-triangle";
var sregex_title = "(?:.+?[„“«‘] ?(.+) ?[“”»’] \\S+ (.+?)|(.+?)のリリース(?:グループ)?「(.+)」) - MusicBrainz";
var startpos, mergeStatus, from, to, swap, editNote, queuetrack, queueAll;
var localRelease, remoteRelease;
var matchMode = {current: null, sequential: null, title: null, titleAndAC: null};
var rem2loc = "◀";
var loc2rem = "▶";
var retry = {count: 0, checking: false};
var css = document.createElement("style");
css.setAttribute("type", "text/css");
document.head.appendChild(css);
css = css.sheet;
css.insertRule("body." + userjs.id + " div#" + userjs.id + " > .main-shortcut { display: none; }", 0);
css.insertRule("body." + userjs.id + " div#content table.tbl.medium > * > tr > .rating { display: none; }", 0);
css.insertRule("body." + userjs.id + " div#content table.tbl.medium > tbody > tr > td > div.ars { display: none !important; }", 0);
css.insertRule("body." + userjs.id + " div#content table.tbl.medium > tbody > tr > td a[href^='//acoustid.org/track/']{ display: none; }", 0); // link to Compare AcoustIDs easier! https://github.com/otringal/MB-userscripts/blob/master/Musicbrainz_acoustid.user.js
css.insertRule("body:not(." + userjs.id + ") div#" + userjs.id + " { margin-top: 12px; cursor: pointer; }", 0);
css.insertRule("body:not(." + userjs.id + ") div#" + userjs.id + " > :not(h2):not(.main-shortcut) { display: none; }", 0);
css.insertRule("body:not(." + userjs.id + ") div#" + userjs.id + " input[name='mergeStatus'] { font-size: 9px!important; background-color: #fcf; }", 0);
css.insertRule("div#" + userjs.id + " { background-color: #fcf; text-shadow: 1px 1px 2px #663; padding: 4px; margin: 0px -6px 12px; border: 2px dotted white; }", 0);
css.insertRule("div#" + userjs.id + " > .main-shortcut { margin: 0px; }", 0);
css.insertRule("div#" + userjs.id + " h2 { color: maroon; text-shadow: 2px 2px 4px #996; margin: 0px; }", 0);
css.insertRule("div#" + userjs.id + " kbd { background-color: silver; border: 2px grey outset; padding: 0px 4px; font-size: .8em; }", 0);
css.insertRule(".remoteRecordingLength.largeSpread { color: yellow; background-color: red; text-shadow: 2px 2px 4px black; }", 0);
css.insertRule("/*body." + userjs.id + "*/ div#content > table.tbl." + userjs.id + "reclist > tbody > tr:nth-child(even) > td { background-color: #f2f2f2; }", 0);
css.insertRule("/*body." + userjs.id + "*/ div#content > table.tbl." + userjs.id + "reclist { counter-reset: recording-index; }", 0);
css.insertRule("/*body." + userjs.id + "*/ div#content > table.tbl." + userjs.id + "reclist > tbody > tr > td:first-child:before { counter-increment: recording-index; content: counter(recording-index); opacity: .6; }", 0);
css.insertRule("/*body." + userjs.id + "*/ div#content > table.tbl." + userjs.id + "reclist > tbody > tr > td:first-child { text-align: center; }", 0);
css.insertRule("/*body." + userjs.id + "*/ div#content > table.tbl." + userjs.id + "reclist > thead > tr > th:first-child { text-align: center; }", 0);
css.insertRule("/*body." + userjs.id + "*/ div#content > table.tbl." + userjs.id + "reclist > tbody > tr.sameName > td:nth-child(2) { border-left: 2px solid red; }", 0);
var dtitle = document.title;
var ltitle = dtitle.match(new RegExp("^" + sregex_title + "$"));
var release_group_MBID = location.pathname.match(new RegExp("^/release-group/(" + sregex_MBID + ")$"));
var releases = document.querySelectorAll("div#content table.tbl > tbody > tr > td > a[href^='/release/'] > bdi, div#content table.tbl > tbody > tr > td > span.mp > a[href^='/release/'] > bdi");
if (ltitle) {
ltitle = {
artists: ltitle[2] || ltitle[3],
title: ltitle[1] || ltitle[4]
};
if (release_group_MBID) {
if (document.getElementsByClassName("account").length > 0 && releases.length > 0) {
releases = Array.prototype.slice.call(releases);
sidebar.insertBefore(RGRecordingsMassMergeGUI(), sidebar.querySelector("h2.collections"));
document.body.addEventListener("keydown", function(event) {
if (CONTROL_POMME.ctrl_shift.test(event) && event.key.match(/^m$/i)) {
loadRGRecordings(releases);
return stop(event);
}
});
}
} else {
localRelease = {
"release-group": document.querySelector("div.releaseheader > p.subheader a[href*='/release-group/']").getAttribute("href").match(regex_MBID)[0],
title: ltitle.title,
looseTitle: looseTitle(ltitle.title),
comment: document.querySelector("h1 > span.comment > bdi") || "",
ac: ltitle.artists,
id: location.pathname.match(regex_MBID)[0],
tracks: []
};
if (localRelease.comment) {
localRelease.comment = "(" + localRelease.comment.textContent + ")";
}
remoteRelease = {tracks: []};
if (document.getElementsByClassName("account").length > 0) {
sidebar.insertBefore(massMergeGUI(), sidebar.querySelector("h2.collections"));
document.body.addEventListener("keydown", function(event) {
if (CONTROL_POMME.ctrl_shift.test(event) && event.key.match(/^m$/i)) {
prepareLocalRelease();
return stop(event);
} else if (
startpos.children.length !== 0
&& matchMode.current == matchMode.sequential
&& CONTROL_POMME.ctrl_shift.test(event) && event.key.match(/^Arrow(Up|Down|Left|Right)$/i)
) {
if (event.key.match(/^Arrow(Up|Left)$/i) && startpos.selectedIndex > 0) {
startpos.selectedIndex -= 1;
} else if (event.key.match(/^Arrow(Down|Right)$/i) && startpos.selectedIndex < startpos.length - 1) {
startpos.selectedIndex += 1;
}
sendEvent(startpos, "change");
return stop(event);
}
});
}
// sidebar.querySelector("h2.editing + ul.links").insertBefore(createTag("li", {}, [createTag("a", {}, userjs.name)]), sidebar.querySelector("h2.editing + ul.links li"));
}
} else {
console.error("Local title (/^" + sregex_title + "$/) not found in document.title (" + document.title + ").");
}
function mergeRecsStep(_step) {
if (editNote.value && MBJS.isValidEditNote(editNote.value)) {
editNote.style.removeProperty("background-color");
if (editNote.nextSibling.matches("p.error." + userjs.id)) {
editNote.parentNode.removeChild(editNote.nextSibling);
}
var step = _step || 0;
var MMR = document.getElementById(userjs.id);
var statuses = ["adding recs. to merge", "applying merge edit"];
var buttStatuses = ["Stacking…", "Merging…"];
var urls = ["/recording/merge_queue", "/recording/merge"];
var params = [
"add-to-merge=" + to.value + "&add-to-merge=" + from.value,
"merge.merging.0=" + to.value + "&merge.target=" + to.value + "&merge.merging.1=" + from.value
];
disableInputs([matchMode.sequential, matchMode.title, matchMode.titleAndAC, startpos, mergeStatus]);
if (step == 1) {
disableInputs([editNote, currentButt, currentButt.parentNode.querySelector("input." + userjs.id + "dirbutt")]);
params[step] += "&merge.edit_note=";
var paramsup = MMR.getElementsByTagName("textarea")[0].value.trim();
if (paramsup != "") paramsup += "\n —\n";
paramsup += releaseInfoRow("source", swap.value == "no" ? remoteRelease : localRelease, swap.value == "no" ? recid2trackIndex.remote[from.value] : recid2trackIndex.local[from.value]);
paramsup += releaseInfoRow("target", swap.value == "no" ? localRelease : remoteRelease, swap.value == "no" ? recid2trackIndex.local[to.value] : recid2trackIndex.remote[to.value]);
paramsup += " —\n";
var targetID = parseInt(to.value, 10);
var sourceID = parseInt(from.value, 10);
if (sourceID > targetID) {
paramsup += "👍 '''Targeting oldest [MBID]''' (" + format(to.value) + " ← " + format(from.value) + ")" + "\n";
}
var locTrack = localRelease.tracks[recid2trackIndex.local[swap.value == "no" ? to.value : from.value]];
var remTrack = remoteRelease.tracks[recid2trackIndex.remote[swap.value == "no" ? from.value : to.value]];
if (locTrack.name == remTrack.name) paramsup += "👍 '''Same track title''' “" + protectEditNoteText(locTrack.name) + "”\n";
else if (locTrack.name.toUpperCase() == remTrack.name.toUpperCase()) paramsup += "👍 '''Same track title''' (case insensitive)\n";
else if (locTrack.looseName == remTrack.looseName) paramsup += "👍 '''Similar track title''' (loose comparison)\n";
if (locTrack.artistCredit == remTrack.artistCreditAsPlainText) paramsup += "👍 '''Same track artist credit ([AC])''' “" + locTrack.artistCredit + "”\n";
else if (locTrack.artistCredit.toUpperCase() == remTrack.artistCreditAsPlainText.toUpperCase()) paramsup += "👍 '''Same track artist credit ([AC])''' (case insensitive)\n";
else if (locTrack.looseAC == remTrack.looseAC) paramsup += "👍 '''Similar track artist credit ([AC])''' “" + locTrack.artistCredit + "”\n";
if (typeof locTrack.length == "number" && typeof remTrack.length == "number") {
var delta = Math.abs(locTrack.length - remTrack.length);
if (delta <= safeLengthDelta * 1000) paramsup += "👍 '''" + (delta === 0 ? "Same" : "Very close") + " track times''' " + /* temporary hidden until milliseconds are back (delta === 0 ? "(in milliseconds)" : */ "(" + (time(locTrack.length) == time(remTrack.length) ? time(locTrack.length) : "within " + safeLengthDelta + " seconds: " + time((swap.value == "no" ? locTrack : remTrack).length) + " ← " + time((swap.value == "no" ? remTrack : locTrack).length)) + ")" /* ) temporary */ + "\n";
}
if (localRelease.ac == remoteRelease.ac) paramsup += "👍 '''Same release artist''' “" + protectEditNoteText(localRelease.ac) + "”\n";
if (localRelease.title == remoteRelease.title) paramsup += "👍 '''Same release title''' “" + protectEditNoteText(localRelease.title) + "”\n";
else if (localRelease.title.toUpperCase() == remoteRelease.title.toUpperCase()) paramsup += "👍 '''Same release title''' (case insensitive)\n";
else if (localRelease.looseTitle == remoteRelease.looseTitle) paramsup += "👍 '''Almost same release title''' (loose comparison)\n";
// else if (leven(localRelease.looseTitle, remoteRelease.looseTitle)) paramsup += "👍 '''Almost same release title''' (loose comparison)\n";
if (localRelease["release-group"] == remoteRelease["release-group"]) paramsup += "👍 '''Same release group''' (" + MBS + "/release-group/" + localRelease["release-group"] + ")\n";
paramsup += " —\n" + userjs.name + " (" + userjs.version + ") in “" + matchMode.current.value.replace(/^Match unordered /i, "") + "” match mode";
if (retry.count > 0) {
paramsup += " — '''retry'''" + (retry.count > 1 ? " #" + retry.count : "") + " (" + protectEditNoteText(retry.message) + ")";
}
params[step] += encodeURIComponent(paramsup);
}
infoMerge("#" + from.value + " to #" + to.value + " " + statuses[step] + "…");
currentButt.setAttribute("value", buttStatuses[step] + " " + (step + 1) + "/2");
currentButt.setAttribute("ref", step);
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(event) {
if (this.readyState == 4) {
if (to.value === "") {
nextButt(false);
} else if (this.status == 200) {
if (step === 0) {
if (
this.responseText.match(new RegExp('<form method="post">'))
&& this.responseText.indexOf('value="' + from.value + '"') > -1
&& this.responseText.indexOf('<a href="/recording/' + from.getAttribute("ref") + '">') > -1
&& this.responseText.indexOf('value="' + to.value + '"') > -1
&& this.responseText.indexOf('<a href="/recording/' + to.getAttribute("ref") + '">') > -1
) {
mergeRecsStep(1);
} else {
tryAgain("Did not queue");
}
} else if (step === 1) {
if (
this.responseText.indexOf('<h1><span class="mp"><a href="/recording/' + to.getAttribute("ref") + '">') > -1
&& this.responseText.indexOf('href="/recording/merge_queue?add-to-merge=' + to.value) > -1
) {
nextButt(true);
} else {
checkMerge("Did not merge");
}
}
} else {
var errorText = "Error " + this.status + " “" + this.statusText + "” in step " + (step + 1) + "/2";
if (step === 0) {
tryAgain(errorText);
} else {
checkMerge(errorText);
}
}
}
};
xhr.open("POST", MBS + urls[step], true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
setTimeout(function() { xhr.send(params[step]); }, chrono(MBSminimumDelay));
} else {
if (!editNote.nextSibling.matches("p.error." + userjs.id)) {
editNote.parentNode.insertBefore(createTag("p", {a: {class: "error"}}, "Merging recordings is a destructive edit that is impossible to undo without losing ISRCs, AcoustIDs, edit histories, etc.\n\nPlease make sure your edit note makes it clear why you are sure that these recordings are exactly the same versions, mixes, cuts, etc."), editNote);
addAfter(createTag("p", {a: {class: "error " + userjs.id}}, MBJS.texts("invalid_edit_note")), editNote);
}
editNote.style.setProperty("background-color", cNG);
infoMerge("Invalid edit note.", false, true);
}
}
function releaseInfoRow(sourceOrTarget, rel, trackIndex) {
var spaced_comment = rel.comment ? " " + rel.comment : "";
return sourceOrTarget + ": " + MBS + "/release/" + rel.id + " #'''" + (trackIndex + 1) + "'''/" + rel.tracks.length + ". “'''" + protectEditNoteText(rel.title) + "'''”" + protectEditNoteText(spaced_comment) + " by '''" + protectEditNoteText(rel.ac) + "'''\n";
}
function checkMerge(errorText) {
retry.checking = true;
infoMerge("Checking merge (" + errorText + ")…", false);
var xhr = new XMLHttpRequest();
xhr.addEventListener("error", function(event) {
setTimeout(function() {
infoMerge("Retrying in 2s (error " + this. status + ": “" + this.statusText + "”)…", false);
checkMerge(errorText);
}, 2000);
});
xhr.addEventListener("load", function(event) {
if (this.status < 200 || this.status >= 400) {
sendEvent(this, "error");
} else {
if (typeof this.responseText == "string") {
if (this.responseText.indexOf('class="edit-list"') > -1) {
var editID = this.responseText.match(/>Edit #(\d+)/);
nextButt(editID ? editID[1] : true);
} else if (this.responseText.indexOf('id="remove.' + from.value + '"') > -1 && this.responseText.indexOf('id="remove.' + to.value + '"') > -1) {
retry.count += 1;
retry.message = errorText;
mergeRecsStep(1);
} else {
tryAgain(errorText);
}
} else {
sendEvent(this, "error");
}
retry.checking = false;
}
});
xhr.open("GET", MBS + "/search/edits?negation=0&combinator=and&conditions.0.field=recording&conditions.0.operator=%3D&conditions.0.name=" + from.value + "&conditions.0.args.0=" + from.value + "&conditions.1.field=recording&conditions.1.operator=%3D&conditions.1.name=" + to.value + "&conditions.1.args.0=" + to.value + "&conditions.2.field=type&conditions.2.operator=%3D&conditions.2.args=74&conditions.3.field=status&conditions.3.operator=%3D&conditions.3.args=1", true);
setTimeout(function() { xhr.send(null); }, chrono(retryDelay));
}
function nextButt(successOrEditID) {
if (successOrEditID !== false) {
remoteRelease.tracks[recid2trackIndex.remote[swap.value == "no" ? from.value : to.value]].recording.editsPending++;
cleanTrack(localRelease.tracks[recid2trackIndex.local[swap.value == "no" ? to.value : from.value]], successOrEditID || true, retry.count);
infoMerge("#" + from.value + " to #" + to.value + " merged OK", true, true);
} else {
infoMerge("Merge cancelled", true, true);
currentButt.value = "Merge";
enableInputs(currentButt);
}
retry.count = 0;
currentButt = null;
document.title = dtitle;
updateMatchModeDisplay();
enableInputs([mergeStatus, editNote]);
var nextButtFromQueue = mergeQueue.shift();
if (nextButtFromQueue) {
enableAndClick(nextButtFromQueue);
}
}
function tryAgain(errorText) {
retry.count += 1;
retry.message = errorText;
var errormsg = errorText;
if (currentButt) {
errormsg = "Retry in " + Math.ceil(retryDelay / 1000) + " seconds (" + errormsg + ").";
setTimeout(function() {
enableAndClick(currentButt);
}, retryDelay);
}
infoMerge(errormsg, false, true);
}
function enableAndClick(butt) {
enableInputs(butt);
butt.value = "Merge";
butt.click();
}
function infoMerge(msg, goodNews, reset) {
mergeStatus.value = msg;
if (goodNews != null) {
mergeStatus.style.setProperty("background-color", goodNews ? cOK : cNG);
} else {
mergeStatus.style.setProperty("background-color", cInfo);
}
if (reset) {
from.value = "";
to.value = "";
}
}
function queueTrack() {
queuetrack.replaceChild(document.createTextNode(mergeQueue.length + " queued merge" + (mergeQueue.length > 1 ? "s" : "")), queuetrack.firstChild);
queuetrack.style.setProperty("display", mergeQueue.length > 0 ? "block" : "none");
document.title = (mergeQueue.length + 1) + "⌛ " + dtitle;
}
function cleanTrack(track, editID, retryCount) {
var rmForm = track.tr.querySelector("td:not(.pos):not(.video) form." + userjs.id);
if (rmForm) {
if (editID) {
mp(track.tr.querySelector(css_track), true);
var noPendingOpenEdits = document.querySelector("div#sidebar :not(.mp) > a[href='/release/" + localRelease.id + "/open_edits']");
var mb_PENDING_EDITS = document.querySelectorAll("div#sidebar .jesus2099PendingEditsCount");
for (let counts = 0; counts < mb_PENDING_EDITS.length; counts++) {
var currentCount = mb_PENDING_EDITS[counts].textContent.trim();
if ((currentCount = currentCount.match(/^\d+$/)) && mb_PENDING_EDITS[counts].style.getPropertyValue("background-color") != "pink") {
mb_PENDING_EDITS[counts].replaceChild(document.createTextNode(parseInt(currentCount, 10) + 1), mb_PENDING_EDITS[counts].firstChild);
}
}
if (noPendingOpenEdits) {
if (mb_PENDING_EDITS.length > 0) {
noPendingOpenEdits.parentNode.classList.add("mp");
noPendingOpenEdits.style.removeProperty("text-decoration");
for (let counts = 0; counts < mb_PENDING_EDITS.length; counts++) {
mb_PENDING_EDITS[counts].parentNode.parentNode.removeAttribute("title");
mb_PENDING_EDITS[counts].parentNode.parentNode.style.removeProperty("opacity");
}
} else {
mp(noPendingOpenEdits, true);
}
}
removeChildren(rmForm);
if (typeof editID == "number" || typeof retryCount == "number" && retryCount > 0) {
var infoSpan = addAfter(createTag("span", {s: {opacity: ".5"}}, [" (", createTag("span"), ")"]), rmForm).querySelector("span > span");
if (typeof editID == "number") {
infoSpan.appendChild(createTag("span", {a: {class: "mp"}}, createA("edit:" + editID, "/edit/" + editID)));
}
if (typeof retryCount == "number" && retryCount > 0) {
if (infoSpan.childNodes.length > 0) {
infoSpan.appendChild(document.createTextNode(", "));
}
var retryLabel = "retr";
if (retryCount > 1) {
retryLabel = retryCount + " " + retryLabel + "ies";
} else {
retryLabel += "y";
}
infoSpan.appendChild(createA(retryLabel, track.a.getAttribute("href") + "/edits"));
}
}
} else {
removeNode(rmForm);
}
} else {
var lengthcell = track.tr.querySelector("td.treleases");
if (track.length && lengthcell) {
lengthcell.replaceChild(document.createTextNode(time(track.length, false)), lengthcell.firstChild);
lengthcell.style.setProperty("font-family", "monospace");
lengthcell.style.setProperty("text-align", "right");
}
}
}
function changeMatchMode(event) {
matchMode.current = event.target;
updateMatchModeDisplay();
if (matchMode.current == matchMode.sequential) {
spreadTracks(event);
} else {
var matchedRemoteTracks = [];
for (let loc = 0; loc < localRelease.tracks.length; loc++) {
cleanTrack(localRelease.tracks[loc]);
var rem = bestStartPosition(loc, matchMode.current == matchMode.titleAndAC);
if (rem !== null) {
rem = 0 - rem + loc;
if (matchedRemoteTracks.indexOf(rem) < 0) {
matchedRemoteTracks.push(rem);
buildMergeForm(loc, rem);
}
}
}
var notMatched = remoteRelease.tracks.length - matchedRemoteTracks.length;
infoMerge((notMatched === 0 ? "All" : "☞") + " " + matchedRemoteTracks.length + " remote track title" + (matchedRemoteTracks.length == 1 ? "" : "s") + " matched" + (notMatched > 0 ? " (" + notMatched + " left)" : ""), matchedRemoteTracks.length > 0);
}
}
function updateMatchModeDisplay() {
for (let mode in matchMode) if (Object.prototype.hasOwnProperty.call(matchMode, mode)) {
disableInputs(matchMode[mode], matchMode[mode] == matchMode.current);
}
enableInputs(startpos, matchMode.sequential == matchMode.current);
}
function massMergeGUI() {
var MMRdiv = createTag("div", {a: {id: userjs.id}, e: {
keydown: function(event) {
if (event.target == editNote && CONTROL_POMME.ctrl.test(event)) {
switch (event.key) {
case "s":
return saveEditNote(event);
case "o":
return loadEditNote(event);
}
}
},
click: prepareLocalRelease
}}, [
createTag("h2", {}, userjs.name),
createTag("p", {}, "version " + userjs.version),
createTag("p", {a: {"class": "main-shortcut"}}, ["☞ ", CONTROL_POMME.ctrl_shift.label, "M"]),
createTag("p", {s: {marginBottom: "0px!"}}, ["Remote release: ", createTag("span", {a: {"class": "remote-release-link"}})]),
]);
mergeStatus = MMRdiv.appendChild(createInput("text", "mergeStatus", "", userjs.name + " remote release URL"));
mergeStatus.style.setProperty("width", "100%");
mergeStatus.addEventListener("input", function(event) {
matchMode.current = matchMode.sequential;
updateMatchModeDisplay();
var mbid = this.value.match(new RegExp("/release/(" + sregex_MBID + ")(/disc/(\\d+))?"));
if (mbid) {
localRelease.tracks = [];
recid2trackIndex.local = {};
removeChildren(startpos);
var trs = document.querySelectorAll("div#content table.tbl.medium > tbody > tr");
/* var jsonRelease, scripts = document.querySelectorAll("script:not([src])");
for (let s = 0; s < scripts.length && !jsonRelease; s++) {
jsonRelease = scripts[s].textContent.match(/MB\.Release\.init\(([^<]+)\)/);
}
if (jsonRelease) jsonRelease = JSON.parse(jsonRelease[1]); */
var multiDiscRelease = document.querySelectorAll(css_collapsed_medium).length > 1;
for (let itrs = 0, t = 0, d = 0, dt = 0; itrs < trs.length; itrs++) {
if (!trs[itrs].classList.contains("subh")) {
var tracka = trs[itrs].querySelector(css_track);
var recoid = trs[itrs].querySelector("td.rating a.set-rating").getAttribute("href").match(/id=([0-9]+)/)[1];
var trackname = tracka.textContent;
var trackLength = trs[itrs].querySelector("td.treleases").textContent.match(/(\d+:)?\d+:\d+/);
if (trackLength) trackLength = strtime2ms(trackLength[0]);
var trackAC = trs[itrs].querySelector(css_track_ac);
localRelease.tracks.push({
tr: trs[itrs],
disc: d,
track: dt,
a: tracka,
recid: recoid,
name: trackname,
artistCredit: trackAC ? trackAC.textContent.trim() : localRelease.ac,
length: trackLength
});
localRelease.tracks[t].looseName = looseTitle(localRelease.tracks[t].name);
localRelease.tracks[t].looseAC = looseTitle(localRelease.tracks[t].artistCredit);
/* if (jsonRelease) {
// localRelease.tracks[localRelease.tracks.length - 1] = jsonRelease.mediums[d - 1].tracks[dt];
for (let key in jsonRelease.mediums[d - 1].tracks[dt]) if (jsonRelease.mediums[d - 1].tracks[dt].hasOwnProperty(key)) {
localRelease.tracks[localRelease.tracks.length - 1][key] = jsonRelease.mediums[d - 1].tracks[dt][key];
}
} */
dt++;
recid2trackIndex.local[recoid] = t;
addOption(startpos, t, (multiDiscRelease ? d + "." : "") + dt + ". " + trackname);
t++;
} else if (!trs[itrs].querySelector("div.data-track")) {
d++; dt = 0;
}
}
this.setAttribute("ref", this.value);
remoteRelease.id = mbid[1];
remoteRelease.disc = mbid[2] || "";
infoMerge("Fetching recordings…");
loadReleasePage();
// loadReleaseWS(remoteRelease.id);
}
});
MMRdiv.appendChild(createTag("p", {}, "Once you paste the remote release URL, all its recordings will be loaded and made available for merge with the local recordings in the left hand tracklist."));
MMRdiv.appendChild(createTag("p", {}, "Herebelow, you can shift the alignement of local and remote tracklists."));
MMRdiv.appendChild(createTag("p", {s: {marginBottom: "0px"}}, "Remote tracklist start position:"));
/* track parsing */
startpos = MMRdiv.appendChild(createTag("select", {s: {fontSize: ".8em", width: "100%"}, e: {change: function(event) {
/* hitting ENTER on a once changed <select> triggers onchange even if no recent change */
if (this.getAttribute("previousValue") != this.value) {
this.setAttribute("previousValue", this.value);
if (remoteRelease.id && remoteRelease.tracks.length > 0) {
spreadTracks(event);
} else {
mergeStatus.focus();
}
}
}}}));
if (navigator.userAgent.match(/firefox/i)) startpos.addEventListener("keyup", function(event) {
if (event.key != "Enter") {
this.blur();
this.focus();
}
});
MMRdiv.appendChild(createTag("p", {}, [
"☞ ", CONTROL_POMME.ctrl_shift.label,
createTag("kbd", {a: {class: userjs.id + "arrowButton"}, s: {cursor: "pointer"}}, "↑"),
"/",
createTag("kbd", {a: {class: userjs.id + "arrowButton"}, s: {cursor: "pointer"}}, "↓"),
"/",
createTag("kbd", {a: {class: userjs.id + "arrowButton"}, s: {cursor: "pointer"}}, "←"),
"/",
createTag("kbd", {a: {class: userjs.id + "arrowButton"}, s: {cursor: "pointer"}}, "→"),
": shift"
]));
MMRdiv.addEventListener("click", function(event) {
if (matchMode.current == matchMode.sequential && event.target.classList.contains(userjs.id + "arrowButton")) {
if (event.target.textContent.match(/[↑←]/) && startpos.selectedIndex > 0) {
startpos.selectedIndex -= 1;
} else if (event.target.textContent.match(/[↓→]/) && startpos.selectedIndex < startpos.length - 1) {
startpos.selectedIndex += 1;
}
sendEvent(startpos, "change");
}
});
matchMode.sequential = createInput("button", "", "Sequential");
matchMode.sequential.setAttribute("title", "Restore remote tracks order");
matchMode.sequential.addEventListener("click", changeMatchMode);
matchMode.title = createInput("button", "", "Match unordered track titles");
matchMode.title.setAttribute("title", "Find matching local title for each remote title");
matchMode.title.addEventListener("click", changeMatchMode);
matchMode.titleAndAC = createInput("button", "", "Match unordered track titles and artist credits");
matchMode.titleAndAC.setAttribute("title", "Find matching local title for each remote title with same artist credit");
matchMode.titleAndAC.addEventListener("click", changeMatchMode);
matchMode.current = matchMode.sequential;
disableInputs(matchMode.sequential);
MMRdiv.appendChild(createTag("p", {}, [matchMode.sequential, matchMode.title, matchMode.titleAndAC]));
MMRdiv.appendChild(createTag("p", {s: {marginBottom: "0px"}}, "Merge edit notes:"));
editNote = MMRdiv.appendChild(createInput("textarea", "merge.edit_note"));
var lastEditNote = (localStorage && localStorage.getItem(userjs.id));
if (lastEditNote) {
editNote.appendChild(document.createTextNode(lastEditNote));
editNote.style.setProperty("background-color", cOK);
editNote.selectionEnd = 0;
}
editNote.style.setProperty("width", "100%");
editNote.setAttribute("rows", "5");
editNote.addEventListener("input", function(event) {
this.style.removeProperty("background-color");
this.removeAttribute("title");
});
var saveEditNoteButt = createInput("button", "", "Save edit note");
saveEditNoteButt.setAttribute("tabindex", "-1");
saveEditNoteButt.setAttribute("title", "Save edit note text to local storage for next time");
saveEditNoteButt.addEventListener("click", saveEditNote);
var loadEditNoteButt = createInput("button", "", "Load edit note");
loadEditNoteButt.setAttribute("tabindex", "-1");
loadEditNoteButt.setAttribute("title", "Reload edit note text from local storage");
loadEditNoteButt.addEventListener("click", loadEditNote);
MMRdiv.appendChild(createTag("p", {}, ["☞ ", CONTROL_POMME.ctrl.label, "S", ": ", saveEditNoteButt, document.createElement("br"), "☞ ", CONTROL_POMME.ctrl.label, "O", ": ", loadEditNoteButt]));
MMRdiv.appendChild(createTag("p", {}, "Each recording merge will automatically target the oldest, unless direction is manually changed by clicking each arrow button or below batch button."));
from = MMRdiv.appendChild(createInput("hidden", "from", ""));
to = MMRdiv.appendChild(createInput("hidden", "to", ""));
swap = MMRdiv.appendChild(createInput("hidden", "swap", "yes"));
var changeAllDirButt = createInput("button", "", "Change all merge targets to " + (swap.value == "no" ? "remote" : "local"));
changeAllDirButt.style.setProperty("background-color", cOK);
changeAllDirButt.addEventListener("click", function(event) {
var allbutts = document.querySelectorAll("input." + userjs.id + "dirbutt:not([disabled])");
var direction = this.value.match(/local/) ? rem2loc : loc2rem;
for (let iab = 0; iab < allbutts.length; iab++) if (allbutts[iab].value != direction) allbutts[iab].click();
swap.value = direction == rem2loc ? "no" : "yes";
this.value = this.value.replace(/\w+$/, swap.value == "no" ? "remote" : "local");
this.style.setProperty("background-color", swap.value == "no" ? cInfo : cOK);
});
var resetAllDirButt = createInput("button", "", "Reset all merge directions to oldest");
resetAllDirButt.addEventListener("click", function(event) {
var allbutts = document.querySelectorAll("input." + userjs.id + "dirbutt:not([disabled])");
for (let iab = 0; iab < allbutts.length; iab++) {
var remoteRowID = parseInt(allbutts[iab].parentNode.querySelector("input[name='merge.merging.1']").value, 10);
var localRowID = parseInt(allbutts[iab].parentNode.querySelector("input[name='merge.merging.0']").value, 10);
if (remoteRowID > localRowID && allbutts[iab].value == loc2rem || remoteRowID < localRowID && allbutts[iab].value == rem2loc) {
allbutts[iab].click();
}
}
});
MMRdiv.appendChild(createTag("p", {}, [changeAllDirButt, resetAllDirButt]));
MMRdiv.appendChild(createTag("p", {}, "You can add/remove recordings to/from the merge queue by clicking their merge buttons or add them all at once with the button below."));
queueAll = createInput("button", "", "Merge all found recordings");
queueAll.setAttribute("ref", queueAll.value);
queueAll.style.setProperty("background-color", cMerge);
queueAll.addEventListener("click", function(event) {
var allbutts = document.getElementsByClassName(userjs.id + "mergebutt");
for (let iab = 0; iab < allbutts.length; iab++) {
if (allbutts[iab].value == "Merge") allbutts[iab].click();
}
});
var emptyQueueButt = createInput("button", "", "Empty merge queue");
emptyQueueButt.style.setProperty("background-color", cCancel);
emptyQueueButt.addEventListener("click", function(event) {
if (mergeQueue.length > 0) {
while (mergeQueue.length > 0) {
var unqueuedbutt = mergeQueue.shift();
unqueuedbutt.style.setProperty("background-color", cMerge);
enableInputs(unqueuedbutt);
unqueuedbutt.value = "Merge";
}
queueTrack();
}
});
MMRdiv.appendChild(createTag("p", {}, [queueAll, emptyQueueButt]));
queuetrack = MMRdiv.appendChild(createTag("div", {s: {textAlign: "center", backgroundColor: cInfo, display: "none"}}, "\u00A0"));
return MMRdiv;
}
function loadReleasePage() {
for (let ltrack = 0; ltrack < localRelease.tracks.length; ltrack++) {
// TODO: should probably remove some in spreadTracks() etc.
cleanTrack(localRelease.tracks[ltrack]);
}
var mbidInfo = document.getElementById(userjs.id).querySelector(".remote-release-link");
removeChildren(mbidInfo);
mbidInfo.setAttribute("title", remoteRelease.id + remoteRelease.disc);
mbidInfo.appendChild(document.createTextNode(" "));
mbidInfo.appendChild(createA(remoteRelease.id.match(/[\w\d]+/), "/release/" + remoteRelease.id));
var xhr = new XMLHttpRequest();
xhr.addEventListener("error", function() { infoMerge("Error " + this.status + ": “" + this.statusText + "”", false); });
xhr.addEventListener("load", function(event) {
if (this.status < 200 || this.status >= 400) {
sendEvent(this, "error");
} else {
var releaseWithoutARs = this.responseText.replace(/<dl class="ars">[\s\S]+?<\/dl>/g, "");
var mediums = releaseWithoutARs.match(/<table class="tbl medium">[\s\S]+?<\/table>/g);
var rtitle = releaseWithoutARs.match(new RegExp("<title>" + sregex_title + "</title>"));
var discount = releaseWithoutARs.match(/<a class="expand-medium"/g).length;
if (!remoteRelease.disc && releaseWithoutARs.match(/<tbody style="display:none"><\/tbody>/)) {
var disc = prompt("This " + discount + " medium release has some collapsed mediums.\nIn this case I can only load one medium at a time.\n\nPlease enter the medium number that you want to load.\n\nNext time you can directly paste the medium link:\n " + MBS + "/release/" + remoteRelease.id + "/disc/1.", "1");
if (disc && disc.match(/^\d+$/) && disc > 0 && disc <= discount) {
remoteRelease.disc = "/disc/" + disc;
loadReleasePage();
} else {
infoMerge("Disc number out of bounds (1–" + discount + ") or unreadable", false);
}
} else if (rtitle) {
rtitle = {
artists: rtitle[2] || rtitle[3],
title: rtitle[1] || rtitle[4]
};
remoteRelease["release-group"] = releaseWithoutARs.match(/\((?:<span[^>]*>){0,2}<a href=".*\/release-group\/([^"/]+)">(?:<bdi>)?[^<]+(?:<\/bdi>)?<\/a>(?:<\/span>){0,2}\)/)[1];
remoteRelease.title = HTMLToText(rtitle.title);
remoteRelease.looseTitle = looseTitle(remoteRelease.title);
remoteRelease.comment = releaseWithoutARs.match(/<h1>.+<span class="comment">\(<bdi>([^<]+)<\/bdi>\)<\/span><\/h1>/) || "";
if (remoteRelease.comment) {
remoteRelease.comment = "(" + HTMLToText(remoteRelease.comment[1]) + ")";
}
remoteRelease.ac = rtitle.artists;
removeChildren(mbidInfo);
if (remoteRelease.id == localRelease.id) {
mbidInfo.appendChild(document.createTextNode(" (same" + (remoteRelease.disc ? ", " + remoteRelease.disc.substr(1).replace(/\//, "\u00a0") + "/" + discount : "") + ")"));
} else {
mbidInfo.appendChild(createA(remoteRelease.title, "/release/" + remoteRelease.id));
if (remoteRelease.comment) {
mbidInfo.appendChild(document.createTextNode(" "));
mbidInfo.appendChild(createTag("span", {a: {class: "comment"}}, remoteRelease.comment));
}
if (remoteRelease.disc) {
mbidInfo.appendChild(createTag("fragment", null, [" (", createA(remoteRelease.disc.substr(1).replace(/\//, "\u00a0"), "/release/" + remoteRelease.id + remoteRelease.disc + "#" + remoteRelease.disc.replace(/\//g, "")), "/" + discount + ")"]));
}
if (remoteRelease["release-group"] === localRelease["release-group"]) {
mbidInfo.appendChild(document.createTextNode(" (same release group)"));
}
}
remoteRelease.tracks = [];
for (var m = 0; m < mediums.length; m++) {
var current_medium = mediums[m].match(/<th colSpan="\d+">.+?<\/span>[<!->\s]*?([^<:]+)[<:]/);
if (current_medium) {
current_medium = current_medium[1].replace(/"/, "ʺ") + "\u00a0– ";
} else if (mediums.length > 1) {
current_medium = (m + 1) + ".";
} else {
current_medium = "";
}
var recIDx5 = mediums[m].match(/entity_id=\d+[^"]*entity_type=recording|entity_type=recording[^"]*entity_id=\d+/g);
var trackRows = mediums[m].match(/<tr class="(even|odd)" id="[-\da-z]{36}">[\s\S]+?<td class="treleases">[\s\S]+?<\/tr>/g);
var trackInfos = mediums[m].match(new RegExp("<a href=\"/recording/" + sregex_MBID + "\"( title=\"[^\"]*\")?><bdi>[^<]*</bdi></a>", "g"));
var trackTimes = mediums[m].match(/<td class="treleases">[^<]*<\/td>/g);
if (recIDx5 && trackInfos && trackTimes) {
var recIDs = [];
for (let i5 = 0; i5 < recIDx5.length; i5 += 5) {
recIDs.push(recIDx5[i5].match(/id=([0-9]+)/)[1]);
}
for (var t = 0; t < recIDs.length; t++) {
var trackLength = trackTimes[t].match(/(\d+:)?\d+:\d+/);
if (trackLength) trackLength = strtime2ms(trackLength[0]);
var current_track = {
number: current_medium + trackRows[t].match(new RegExp("<td class=\"pos[\\s\\S]+?<a href=\"" + "/track/" + sregex_MBID + "\">(.*?)</a>"))[1],
name: HTMLToText(trackInfos[t].match(/<bdi>([^<]*)<\/bdi>/)[1]),
artistCredit: trackRows[t].match(/<td class="wrap-anywhere">/g) && trackRows[t].match(/<td class="wrap-anywhere">/g).length === 1 ? trackRows[t].match(/[\s\S]*<td class="wrap-anywhere">([\s\S]+?)<\/td>/)[1].trim().replace(/<a/g, '<a target="_blank"') : remoteRelease.ac,
length: trackLength,
recording: {
rowid: recIDs[t],
id: trackInfos[t].match(/\/recording\/([^"]+)/)[1],
video: trackRows[t].match(/<span class="video"/) !== null,
editsPending: trackRows[t].match(/<td class="title"><span class="mp">/) !== null ? 1 : 0,
},
isDataTrack: false
};
current_track.artistCreditAsPlainText = HTMLToText(current_track.artistCredit);
current_track.looseName = looseTitle(current_track.name);
current_track.looseAC = looseTitle(current_track.artistCreditAsPlainText);
remoteRelease.tracks.push(current_track);
recid2trackIndex.remote[recIDs[t]] = remoteRelease.tracks.length - 1;
}
}
}
/* for (let rd = 0; rd < jsonRelease.mediums.length; rd++) {
for (let rt = 0; rt < jsonRelease.mediums[rd].tracks.length; rt++) {
remoteRelease.tracks.push(jsonRelease.mediums[rd].tracks[rt]);
recid2trackIndex.remote[jsonRelease.mediums[rd].tracks[rt].recording.rowid] = remoteRelease.tracks.length - 1;
}
}
jsonRelease = null; /* maybe it frees up memory */
/* (re)build negative startpos */
var negativeOptions = startpos.querySelectorAll("option[value^='-']");
for (let nopt = 0; nopt < negativeOptions.length; nopt++) {
removeNode(negativeOptions[nopt]);
}
for (let rtrack = 0; rtrack < remoteRelease.tracks.length - 1; rtrack++) {
addOption(startpos, 0 - rtrack - 1, 0 - rtrack - 1, true);
}
startpos.value = bestStartPosition() || 0;
spreadTracks(event);
mergeStatus.blur();
}
}
});
xhr.open("GET", MBS + "/release/" + remoteRelease.id + remoteRelease.disc, true);
setTimeout(function() { xhr.send(null); }, chrono(MBSminimumDelay));
}
function bestStartPosition(localTrack, matchAC) {
var singleTrackMode = typeof localTrack != "undefined";
for (let loc = singleTrackMode ? localTrack : 0; loc < (singleTrackMode ? localTrack + 1 : localRelease.tracks.length); loc++) {
for (let rem = 0; rem < remoteRelease.tracks.length; rem++) {
if (
localRelease.tracks[loc].looseName == remoteRelease.tracks[rem].looseName
&& (!matchAC || localRelease.tracks[loc].looseAC == remoteRelease.tracks[rem].looseAC)
// leven(localRelease.tracks[loc].looseName, remoteRelease.tracks[rem].looseName) < 5
// && (!matchAC || leven(localRelease.tracks[loc].looseAC, remoteRelease.tracks[rem].looseAC) < 5)
) {
return loc - rem;
}
}
}
return null;
}
// function loadReleaseWS(mbid) {
// }
function spreadTracks(event) {
var rtrack = startpos.value < 0 ? 0 - startpos.value : 0;
for (let ltrack = 0; ltrack < localRelease.tracks.length; ltrack++) {
cleanTrack(localRelease.tracks[ltrack]);
if (ltrack >= startpos.value && rtrack < remoteRelease.tracks.length) {
var ntitl = "local recording #" + format(localRelease.tracks[ltrack].recid) + "\n" + localRelease.tracks[ltrack].looseName + "\n" + localRelease.tracks[ltrack].looseAC;
var ntit = localRelease.tracks[ltrack].a.getAttribute("title");
if (!ntit || (ntit && !ntit.match(new RegExp(ntitl)))) {
localRelease.tracks[ltrack].a.setAttribute("title", (ntit ? ntit + " — " : "") + ntitl);
}
buildMergeForm(ltrack, rtrack);
rtrack++;
}
}
var mergebutts = document.getElementsByClassName(userjs.id + "mergebutt").length;
var outOfView = Math.max(0, parseInt(startpos.value, 10) + remoteRelease.tracks.length - localRelease.tracks.length);
if (startpos.value < 0) outOfView -= startpos.value;
infoMerge("☞ " + mergebutts + " recording" + (mergebutts == 1 ? "" : "s") + " ready to merge" + (outOfView > 0 ? " (" + outOfView + " out of view)" : ""), mergebutts > 0);
disableInputs(queueAll, mergebutts < 1);
}
function buildMergeForm(loc, rem) {
var locTrack = localRelease.tracks[loc];
var remTrack = remoteRelease.tracks[rem];
var rmForm = document.createElement("form");
rmForm.setAttribute("action", "/recording/merge");
rmForm.setAttribute("method", "post");
// rmForm.setAttribute("title", "AC: " + ac2str(remTrack.artistCredit) + "\nremote recording #" + remTrack.recording.rowid);
rmForm.setAttribute("title", "remote recording #" + format(remTrack.recording.rowid) + "\n" + remTrack.looseName + "\n" + remTrack.looseAC);
rmForm.setAttribute("class", userjs.id);
rmForm.style.setProperty("display", "inline");
rmForm.appendChild(createInput("hidden", "merge.merging.0", locTrack.recid)).setAttribute("ref", locTrack.a.getAttribute("href").match(regex_MBID)[0]);
rmForm.appendChild(createInput("hidden", "merge.target", locTrack.recid));
rmForm.appendChild(createInput("hidden", "merge.merging.1", remTrack.recording.rowid)).setAttribute("ref", remTrack.recording.id);
rmForm.appendChild(createInput("hidden", "merge.edit_note", "mass rec merger"));
if (remTrack.recording.rowid != locTrack.recid) {
rmForm.style.setProperty("background-color", cWarning);
var dirButt = rmForm.appendChild(createInput("button", "direction", swap.value == "no" ? rem2loc : loc2rem));
dirButt.setAttribute("class", userjs.id + "dirbutt");
dirButt.style.setProperty("background-color", swap.value == "no" ? cOK : cInfo);
dirButt.style.setProperty("padding", "0 1em .5em 1em");
dirButt.style.setProperty("margin", "0 4px");
dirButt.addEventListener("click", function(event) {
this.value = this.value == rem2loc ? loc2rem : rem2loc;
this.style.setProperty("background-color", this.value == rem2loc ? cOK : cInfo);
});
var remrec = rmForm.appendChild(createA(remTrack.number + ". “", "/recording/" + remTrack.recording.id));
if (remTrack.recording.video) {
remrec.parentNode.insertBefore(createTag("span", {a: {class: "video"}}), remrec);
} else if (remTrack.isDataTrack) {
remrec.parentNode.insertBefore(createTag("span", {a: {class: "data-track icon img"}, s: {display: "inline-block", height: "14px", width: "16px"}}), remrec);
}
var rectitle = remrec.appendChild(document.createElement("span"));
rectitle.appendChild(document.createTextNode(remTrack.name));
remrec.appendChild(document.createTextNode("” "));
if (remTrack.looseName == locTrack.looseName) {
rectitle.style.setProperty("background-color", cOK);
rectitle.setAttribute("title", "(almost) same title");
}
if (remTrack.recording.editsPending > 0) {
remrec = mp(remrec, true);
}
var reclen = remrec.appendChild(document.createElement("span"));
reclen.style.setProperty("float", "right");
reclen.style.setProperty("font-family", "monospace");
reclen.classList.add("remoteRecordingLength");
reclen.appendChild(document.createTextNode(" " + time(remTrack.length, true)));
if (typeof locTrack.length == "number" && typeof remTrack.length == "number") {
var delta = Math.abs(locTrack.length - remTrack.length);
if (delta != false && delta > safeLengthDelta * 1000) {
if (delta >= largeSpread * 1000) {
reclen.classList.add("largeSpread");
reclen.setAttribute("title", "MORE THAN " + 15 + " SECONDS DIFFERENCE");
} else {
reclen.style.setProperty("background-color", cNG);
reclen.setAttribute("title", "more than " + safeLengthDelta + " seconds difference");
}
} else {
reclen.style.setProperty("background-color", delta && delta > 500 ? cWarning : cOK);
}
}
rmForm.appendChild(document.createTextNode(" by "));
// rmForm.appendChild(ac2dom(remTrack.artistCredit));
var AC = document.createElement("span");
AC.innerHTML = remTrack.artistCredit;
if (locTrack.looseAC == remTrack.looseAC) {
for (let spanMp = AC.querySelectorAll("span.mp"), m = 0; m < spanMp.length; m++) {
spanMp[m].classList.remove("mp");
}
AC.style.setProperty("background-color", cOK);
}
rmForm.appendChild(AC);
var mergeButt = rmForm.appendChild(createInput("button", "", "Merge"));
mergeButt.setAttribute("class", userjs.id + "mergebutt");
mergeButt.style.setProperty("background-color", cMerge);
mergeButt.style.setProperty("float", "right");
mergeButt.addEventListener("click", function(event) {
var swapbutt = this.parentNode.querySelector("input." + userjs.id + "dirbutt");
this.style.setProperty("background-color", cInfo);
if (this.value == "Merge") {
if (from.value === "") {
/* if no merge is ongoing, launch this merge */
var swapped = (swapbutt.value == loc2rem);
var mergeFrom = this.parentNode.getElementsByTagName("input")[swapped ? 0 : 2];
var mergeTo = this.parentNode.getElementsByTagName("input")[swapped ? 2 : 0];
from.value = mergeFrom.value;
from.setAttribute("ref", mergeFrom.getAttribute("ref"));
to.value = mergeTo.value;
to.setAttribute("ref", mergeTo.getAttribute("ref"));
swap.value = (swapped ? "yes" : "no");
currentButt = this;
mergeRecsStep();
} else if (retry.checking || retry.count > 0 || mergeQueue.indexOf(this) < 0) {
/* if a merge is ongoing or a checking/retry is pending, queue this one */
this.value = "Unqueue";
enableInputs([this, swapbutt]);
mergeQueue.push(this);
}
} else if (this.value == "Unqueue") {
var queuedItem = mergeQueue.indexOf(this);
if (queuedItem > -1) {
/* unqueue this one */
mergeQueue.splice(queuedItem, 1);
this.value = "Merge";
}
} else if (this.getAttribute("ref") === "0") {
/* if this merge is being stacked (step 0), cancel its submission (step 1) */
infoMerge("Cancelling merge…", true, true);
disableInputs(this);
this.removeAttribute("ref");
this.value = "Cancelling…";
} else {
/* shit happens */
enableInputs([this, swapbutt]);
this.style.setProperty("background-color", cWarning);
this.value += " error?";
}
queueTrack();
});
} else {
rmForm.style.setProperty("background-color", cCancel);
rmForm.appendChild(document.createTextNode(" (same recording) "));
rmForm.appendChild(createA(remTrack.name, locTrack.a.getAttribute("href")));
}
if (!locTrack.a.parentNode) {
locTrack.a = locTrack.tr.querySelector(css_track);
}
var tracktd = getParent(locTrack.a, "td");
var bestPos = tracktd.querySelector("td > span.mp");
bestPos = bestPos ? bestPos : locTrack.a;
var recording_comment = tracktd.querySelector("span.comment");
if (recording_comment && !recording_comment.closest(".ars")) {
// only if it is local track recording comment, added by userscripts like mb_INLINE-STUFF
// ignore inline credit (hidden by script) (frequent) comments
bestPos = recording_comment;
}
addAfter(rmForm, bestPos);
if (remTrack.recording.rowid != locTrack.recid) {
var remoteRowID = parseInt(remTrack.recording.rowid, 10);
var localRowID = parseInt(locTrack.recid, 10);
var dirbutt = rmForm.querySelector("input[type='button']." + userjs.id + "dirbutt");
if (remoteRowID > localRowID && dirbutt.value == loc2rem || remoteRowID < localRowID && dirbutt.value == rem2loc) {
dirbutt.click();
}
}
}
function expandCollapseAllMediums(clickThis) {
// Native a#expand-all-mediums randomly collapses mediums if called by my script
if (clickThis) for (let collapsedMediums = document.querySelectorAll(css_collapsed_medium), a = collapsedMediums.length - 1; a >= 0; a--) {
if (collapsedMediums[a].textContent.trim() == clickThis) {
collapsedMediums[a].click();
}
}
}
function prepareLocalRelease() {
if (location.pathname.match(/\/disc\/\d+/)) {
if (confirm(userjs.name + " only works on normal release pages (not on this kind of disc anchor version).\n\nDo you agree to reload page?")) {
location.assign(MBS + "/release/" + localRelease.id);
}
return;
}
// link to mb_INLINE-STUFF (start)
var inlineStuffedRecordingNames = document.querySelectorAll("a[jesus2099userjs81127recname]");
for (let n = 0; n < inlineStuffedRecordingNames.length; n++) {
replaceChildren(createTag("bdi", {}, inlineStuffedRecordingNames[n].getAttribute("jesus2099userjs81127recname")), inlineStuffedRecordingNames[n]);
inlineStuffedRecordingNames[n].removeAttribute("jesus2099userjs81127recname");
}
// link to mb_INLINE-STUFF (end)
document.body.appendChild(createTag("div", {a: {class: "loading-" + userjs.id}, s: {position: "fixed", background: "#FEF", opacity: ".6", top: "0px", right: "0px", bottom: "0px", left: "0px"}}));
document.body.appendChild(createTag("h1", {a: {class: "loading-" + userjs.id}, s: {position: "fixed", top: "50%", left: "50%", transform: "translate(-50%, -50%)", textShadow: "0 0 8px white"}}, "LOADING ALL MEDIUMS…"));
expandCollapseAllMediums("▶");
setTimeout(loadingAllMediums, 10);
}
function loadingAllMediums() {
var loadingMessage = document.querySelector("h1.loading-" + userjs.id);
var loading_things = document.querySelectorAll("div#content table.tbl.medium > tbody .loading-message").length;
if (loading_things > 0 && loadingMessage.textContent.match(/MEDIUMS?…$/)) {
loadingMessage.replaceChild(document.createTextNode("LOADING " + loading_things + " MEDIUM" + (loading_things !== 1 ? "S" : "") + "…"), loadingMessage.firstChild);
setTimeout(loadingAllMediums, 200);
} else {
var loadTracks = document.querySelector("div#content table.tbl.medium > tbody a.load-tracks");
if (loadTracks) {
if (loadingMessage.textContent.match(/MEDIUMS?…$/)) {
loadingMessage.replaceChild(document.createTextNode("LOADING ALL TRACKS"), loadingMessage.firstChild);
} else {
loadingMessage.appendChild(document.createTextNode("."));
}
loadTracks.click();
setTimeout(loadingAllMediums, 200);
} else {
showGUI();
}
}
}
function showGUI() {
for (var loadingMessages = document.querySelectorAll(".loading-" + userjs.id), i = 0; i < loadingMessages.length; i++) {
removeNode(loadingMessages[i]);
}
if (!document.body.classList.contains(userjs.id)) {
document.body.classList.add(userjs.id);
var MMRdiv = document.getElementById(userjs.id);
var tracklistTop = document.querySelector("h2.tracklist");
if (tracklistTop && tracklistTop.offsetTop) {
var margin = tracklistTop.offsetTop - startpos.offsetTop + MMRdiv.offsetTop;
if (margin > 0) {
MMRdiv.style.setProperty("margin-top", margin + "px");
}
tracklistTop.scrollIntoView();
}
MMRdiv.removeEventListener("click", prepareLocalRelease);