forked from loujine/musicbrainz-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mb-edit-set_work_attributes.user.js
152 lines (140 loc) · 5.69 KB
/
mb-edit-set_work_attributes.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
/* global $ requests server works sidebar edits helper */
'use strict';
// ==UserScript==
// @name MusicBrainz edit: Set work attributes from the composer Work page
// @namespace mbz-loujine
// @author loujine
// @version 2021.9.19
// @downloadURL https://raw.githubusercontent.com/loujine/musicbrainz-scripts/master/mb-edit-set_work_attributes.user.js
// @updateURL https://raw.githubusercontent.com/loujine/musicbrainz-scripts/master/mb-edit-set_work_attributes.user.js
// @supportURL https://github.com/loujine/musicbrainz-scripts
// @icon https://raw.githubusercontent.com/loujine/musicbrainz-scripts/master/icon.png
// @description musicbrainz.org: Set attributes (type, lang, key) from the composer Work page
// @compatible firefox+tampermonkey
// @license MIT
// @require https://raw.githubusercontent.com/loujine/musicbrainz-scripts/master/mbz-loujine-common.js
// @include http*://*musicbrainz.org/artist/*/works
// @include http*://*musicbrainz.org/artist/*/works?page=*
// @grant none
// @run-at document-end
// ==/UserScript==
const $rows = $('table.tbl tr:gt(0)');
const idxType = $('table.tbl th:contains("Type")').index();
const idxLang = $('table.tbl th:contains("Language")').index();
const idxKey = $('table.tbl th:contains("Attributes")').index();
$rows.each(function (idx, row) {
const mbid = $(row).find('a[href*="/work/"]').attr('href').split('/')[2];
const title = $(row).find('a[href*="/work/"]')[0].text;
if (!row.children[idxType].textContent.trim()) {
$(row.children[idxType]).append($('<form>')
.append($(works.type).clone()));
}
if (!row.children[idxLang].textContent.trim()) {
$(row.children[idxLang]).append($('<form>')
.append($(works.lang).clone()));
}
if (!row.children[idxKey].textContent.trim()) {
$(row.children[idxKey]).append($('<form>')
.append($(works.key).clone()));
if (title.toLowerCase().includes('major') ||
title.toLowerCase().includes('minor')) {
const cell = row.children[idxKey];
$(cell).find('option').each(function (idx, option) {
if (title.toLowerCase().includes(option.text.toLowerCase())) {
option.selected = true;
}
});
}
}
const $button = $('<input>', {
'id': 'edit-' + mbid,
'class': 'commit',
'type': 'checkbox',
});
const $td = $('<td></td>').append($button);
$(row).append($td);
});
function updateFromPage(editData, node) {
const row = $(node).parents('tr')[0];
const type = $(row.children[idxType]).find('select');
const optionType = type.length ? type[0].value : null;
if (optionType) {
editData.type_id = optionType;
}
const lang = $(row.children[idxLang]).find('select');
const optionLang = lang.length ? lang[0].selectedOptions[0].text : null;
if (optionLang) {
editData.languages = [optionLang];
}
const key = $(row.children[idxKey]).find('select');
const optionKey = key.length ? key[0].value : null;
if (optionKey) {
const keyAttribute = {'type_id': 1, 'value': parseInt(optionKey)};
editData.attributes.push(keyAttribute);
}
return editData;
}
function editWork() {
$('.commit:input:checked:enabled').each(function (idx, node) {
const mbid = node.id.replace('edit-', '');
function success(xhr) {
const $status = $('#' + node.id + '-text');
node.disabled = true;
$status.text(
'Success (code ' + xhr.status + ')'
).parent().css('color', 'green');
const editId = new RegExp(
'/edit/(\\d+)">edit</a>'
).exec(xhr.responseText)[1];
$status.after(
$('<p>').append(
'<a href="/edit/' + editId + '" target="_blank">edit ' + editId + '</a>'
)
);
}
function fail(xhr) {
$('#' + node.id + '-text').text(
'Error (code ' + xhr.status + ')'
).parent().css('color', 'red');
}
function callback(editData) {
// no need to POST relations
delete editData.relations;
$('#' + node.id + '-text').text('Sending edit data');
const postData = edits.prepareEdit(updateFromPage(editData, node));
postData.edit_note = $('#batch_replace_edit_note')[0].value;
console.info('Data ready to be posted: ', postData);
requests.POST(
edits.urlFromMbid('work', mbid),
edits.formatEdit('edit-work', postData),
success,
fail
);
}
setTimeout(function () {
$('#' + node.id + '-text').empty();
$(node).after('<span id="' + node.id + '-text">' +
'Fetching required data</span>');
edits.getWorkEditParams(helper.wsUrl('work', [], mbid), callback);
}, 2 * idx * server.timeout);
});
}
(function displaySidebar() {
if (!helper.isUserLoggedIn()) {
return;
}
sidebar.container().insertAdjacentHTML('beforeend', `
<h3>Edit works</h3>
<p>Edit note:</p>
<textarea id="batch_replace_edit_note"
cols="20" rows="7">${sidebar.editNote(GM_info.script)}</textarea>
<input type="button" id="batch_edit" value="Edit selected works">
`);
})();
$(document).ready(function () {
if (!helper.isUserLoggedIn()) {
return false;
}
document.getElementById('batch_edit').addEventListener('click', editWork);
return false;
});