forked from loujine/musicbrainz-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mb-edit-add_aliases.user.js
113 lines (109 loc) · 4.36 KB
/
mb-edit-add_aliases.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
/* global $ helper aliases edits sidebar requests */
'use strict';
// ==UserScript==
// @name MusicBrainz edit: Add entity aliases in batch
// @namespace mbz-loujine
// @author loujine
// @version 2022.1.28
// @downloadURL https://raw.githubusercontent.com/loujine/musicbrainz-scripts/master/mb-edit-add_aliases.user.js
// @updateURL https://raw.githubusercontent.com/loujine/musicbrainz-scripts/master/mb-edit-add_aliases.user.js
// @supportURL https://github.com/loujine/musicbrainz-scripts
// @icon https://raw.githubusercontent.com/loujine/musicbrainz-scripts/master/icon.png
// @description musicbrainz.org edit: Add entity aliases in batch
// @compatible firefox+tampermonkey
// @license MIT
// @require https://raw.githubusercontent.com/loujine/musicbrainz-scripts/master/mbz-loujine-common.js
// @include http*://*musicbrainz.org/*/*/aliases*
// @exclude http*://*musicbrainz.org/doc/*
// @grant none
// @run-at document-end
// ==/UserScript==
const aliasType = helper.isArtistURL() ? aliases.artistType :
helper.isInstrumentURL() ? aliases.instrumentType : aliases.type;
function addRow() {
document.querySelector('tbody tr:last-child').insertAdjacentHTML('afterend', `
<tr class="newAlias">
<td><input type="text" value=""></td>
<td><input type="text" value=""
placeholder="leave empty to use the name"></td>
<td></td>
<td></td>
<td>${aliasType}</td>
<td>
${aliases.locale}
<input type="checkbox">
<span>primary</span>
</td>
<td><a href="#" class="deleteRow" style="color:red;">×</a></td>
`);
document.querySelector('tr:last-child a.deleteRow').addEventListener('click', evt => {
evt.target.closest('tr').remove();
});
}
function submitAliases() {
for (const node of document.getElementsByClassName('newAlias')) {
const cols = node.children;
const postData = {
name: edits.encodeName(cols[0].children[0].value),
sort_name: edits.encodeName(cols[1].children[0].value),
type_id: cols[4].children[0].value,
locale: cols[5].children[0].value,
primary_for_locale: cols[5].children[1].checked ? 1 : 0,
edit_note: sidebar.editNote(GM_info.script),
};
if (postData.sort_name === '') {
postData.sort_name = postData.name;
}
cols[6].textContent = 'Sending edit data';
console.info('Data ready to be posted: ', postData);
requests.POST(
document.URL.replace('aliases', 'add-alias'),
edits.formatEdit('edit-alias', postData),
// success
xhr => {
cols[6].textContent = `Success (code ${xhr.status})`;
cols[6].parentElement.style.color = 'green';
},
// fail
xhr => {
cols[6].textContent = `Error (code ${xhr.status})`;
cols[6].parentElement.style.color = 'red';
}
);
node.classList.remove('newAlias');
}
}
$(document).ready(function () {
if (!helper.isUserLoggedIn()) {
return false;
}
// doesn't work on translated pages
for (const node of document.getElementById('content').getElementsByTagName('p')) {
if (node.innerHTML.includes('has no aliases')) {
node.innerHTML = `
<table class="tbl">
<thead>
<tr>
<th>Alias</th>
<th>Sort name</th>
<th>Begin Date</th>
<th>End Date</th>
<th>Type</th>
<th>Locale</th>
</tr>
</thead>
<tbody>
<tr></tr>
</tbody>
</table>`;
}
}
document.getElementsByTagName('table')[0].insertAdjacentHTML('beforebegin', `
<h3>Add aliases manually</h3>
<input type="button" id="addRow" value="+ Add a new row">
<input type="button" id="submitAliases" value="Submit new aliases">
`);
document.getElementById('addRow').addEventListener('click', addRow);
document.getElementById('submitAliases').addEventListener('click', submitAliases);
return false;
});