forked from loujine/musicbrainz-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmb-edit-edit_subworks.user.js
146 lines (136 loc) · 6 KB
/
mb-edit-edit_subworks.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
/* global $ relEditor requests edits server sidebar helper */
'use strict';
// ==UserScript==
// @name MusicBrainz edit: Replace subwork titles and attributes in Work edit page
// @namespace mbz-loujine
// @author loujine
// @version 2020.11.16
// @downloadURL https://raw.githubusercontent.com/loujine/musicbrainz-scripts/master/mb-edit-edit_subworks.user.js
// @updateURL https://raw.githubusercontent.com/loujine/musicbrainz-scripts/master/mb-edit-edit_subworks.user.js
// @supportURL https://github.com/loujine/musicbrainz-scripts
// @icon https://raw.githubusercontent.com/loujine/musicbrainz-scripts/master/icon.png
// @description musicbrainz.org edit: Replace subwork titles/attributes in Work edit page
// @compatible firefox+tampermonkey
// @license MIT
// @require https://raw.githubusercontent.com/loujine/musicbrainz-scripts/master/mbz-loujine-common.js
// @include http*://*musicbrainz.org/work/*/edit
// @exclude http*://*musicbrainz.org/work/*/alias/*/edit
// @grant none
// @run-at document-end
// ==/UserScript==
function replaceSubworksTitles() {
let idx = 0;
$('table label:contains("parts:")').parents('tr')
.find('a[href*="/work/"]').each(function (_idx, node) {
const searchExp = document.getElementById('subwork-regexp-search').value;
const replaceExp = document.getElementById('subwork-regexp-replace').value;
if (!searchExp || searchExp === replaceExp) {
return;
}
const name = searchExp.match(/^\/.+\/[gi]*$/) ?
node.textContent.replace(eval(searchExp), replaceExp) : // eslint-disable-line no-eval
node.textContent.split(searchExp).join(replaceExp);
if (name === node.textContent) {
$(node).after('<span>nothing to replace</span>');
return;
}
const mbid = helper.mbidFromURL(node.href);
function success(xhr) {
const $status = $('#replace' + _idx);
const rx = new RegExp(
'/edit/(\\d+)">edit</a>'
).exec(xhr.responseText);
if (rx === null) {
// usually means the POST was accepted but nothing was changed
// i.e. the attributes names are wrong
$status.text(
'Edit was probably not applied, please check'
).parent().css('color', 'red');
return;
}
const editId = rx[1];
$status.parent().css('color', 'green');
$status.after(
$('<p>').append(
'<a href="/edit/' + editId + '" target="_blank">edit ' + editId + '</a>'
)
)
}
function fail(xhr) {
$('#replace' + _idx).text(
'Error (code ' + xhr.status + ')'
).parent().css('color', 'red');
}
function callback(editData) {
// no need to POST relations
delete editData.relations;
$('#replace' + _idx).text('Sending edit data');
editData.name = name;
$('#replace' + _idx).text(' replaced by ' + name);
const postData = edits.prepareEdit(editData);
postData.edit_note = sidebar.editNote(GM_info.script);
console.info('Data ready to be posted: ', postData);
requests.POST(
edits.urlFromMbid('work', mbid),
edits.formatEdit('edit-work', postData),
success,
fail
);
}
setTimeout(function () {
$(node).after('<span id="replace' + _idx + '">' +
'Fetching required data</span>');
edits.getWorkEditParams(helper.wsUrl('work', [], mbid), callback);
}, 2 * idx * server.timeout);
idx += 1;
});
}
function setSubworksAttributes(attrIdx) {
$('table label:contains("parts:")').parents('tr').find('button[class*="edit-item"]').each(
function (_idx, node) {
node.click();
$('.attribute-container input')[attrIdx].click();
$('.rel-editor-dialog button.positive').click();
}
);
}
(function displayToolbar() {
document.getElementsByClassName('half-width')[0].insertAdjacentHTML(
'afterend', '<div id="side-col" style="float: right;"></div>');
relEditor.container(document.getElementById('side-col')).insertAdjacentHTML(
'beforeend', `
<h3>Replace subworks titles</h3>
<p>
Search for a string or regular expression (e.g. /sonata Op.(.+)/i).
Replace with a string that can call groups from the search regexp ($1, $2...).
</p>
<input type="text" id="subwork-regexp-search" value=""
placeholder="Searched string or regexp">
<input type="text" id="subwork-regexp-replace" value="" placeholder="Replacing string">
<input type="button" id="replaceTitles" value="Apply" disabled="True">
<h3>Set subworks attributes</h3>
<select id="subwork_attribute">
<option value=""></option>
<option value=0>act</option>
<option value=1>movement</option>
<option value=2>number</option>
<option value=3>part of collection</option>
</select>
<input type="button" id="setSubworksAttributes" value="Set attribute on all subworks">
`);
document.getElementById('loujine-menu').style.marginLeft = '550px';
})();
$(document).ready(function () {
$('#subwork-regexp-search').keydown(function () {
$('#replaceTitles').prop('disabled', false);
});
document.getElementById('replaceTitles').addEventListener('click', () => {
replaceSubworksTitles();
});
document.getElementById('setSubworksAttributes').addEventListener('click', () => {
setSubworksAttributes($('select#subwork_attribute')[0].value);
document.getElementById('id-edit-work.edit_note')
.value = sidebar.editNote(GM_info.script, 'Set subworks attributes');
});
return false;
});