-
Notifications
You must be signed in to change notification settings - Fork 26
/
mb_ELEPHANT-EDITOR.user.js
287 lines (287 loc) · 12.4 KB
/
mb_ELEPHANT-EDITOR.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
// ==UserScript==
// @name mb. ELEPHANT EDITOR
// @version 2024.11.8
// @description musicbrainz.org + acoustid.org: Remember last edit notes
// @namespace https://github.com/jesus2099/konami-command
// @supportURL https://github.com/jesus2099/konami-command/labels/mb_ELEPHANT-EDITOR
// @downloadURL https://github.com/jesus2099/konami-command/raw/master/mb_ELEPHANT-EDITOR.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-01-13; https://web.archive.org/web/20131103163403/userscripts.org/scripts/show/94629 / https://web.archive.org/web/20141011084017/userscripts-mirror.org/scripts/show/94629
// @icon data:image/gif;base64,R0lGODlhEAAQAMIDAAAAAIAAAP8AAP///////////////////yH5BAEKAAQALAAAAAAQABAAAAMuSLrc/jA+QBUFM2iqA2ZAMAiCNpafFZAs64Fr66aqjGbtC4WkHoU+SUVCLBohCQA7
// @require https://github.com/jesus2099/konami-command/raw/c5fb7fe162530fdbd7e017170f24169272a729a0/lib/CONTROL-POMME.js?version=2024.3.14.1822
// @require https://github.com/jesus2099/konami-command/raw/f1cbb2368209bc51a175062dc512f1a1eb7d25a7/lib/SUPER.js?version=2024.11.8
// @grant none
// @match *://*.musicbrainz.org/*/add-alias
// @match *://*.musicbrainz.org/*/change-quality
// @match *://*.musicbrainz.org/*/create*
// @match *://*.musicbrainz.org/*/delete
// @match *://*.musicbrainz.org/*/edit
// @match *://*.musicbrainz.org/*/edit/?*
// @match *://*.musicbrainz.org/*/edit_annotation
// @match *://*.musicbrainz.org/*/merge
// @match *://*.musicbrainz.org/*/merge?*
// @match *://*.musicbrainz.org/*/move?dest=*
// @match *://*.musicbrainz.org/*/remove*
// @match *://*.musicbrainz.org/*edits*
// @match *://*.musicbrainz.org/artist/*/split
// @match *://*.musicbrainz.org/cdtoc/*
// @match *://*.musicbrainz.org/dialog?path=%2F*%2Fcreate*
// @match *://*.musicbrainz.org/edit/*
// @match *://*.musicbrainz.org/edit/artist/add.html?*
// @match *://*.musicbrainz.org/edit/subscribed*
// @match *://*.musicbrainz.org/event/*/*-event-art*
// @match *://*.musicbrainz.org/isrc/delete*
// @match *://*.musicbrainz.org/mod/*
// @match *://*.musicbrainz.org/recording/*/add-isrc
// @match *://*.musicbrainz.org/release*/*/*-cover-art*
// @match *://*.musicbrainz.org/release/*/edit*
// @match *://*.musicbrainz.org/release/add*
// @match *://*.musicbrainz.org/work/*/add-iswc
// @match *://acoustid.org/edit/*
// @run-at document-idle
// ==/UserScript==
"use strict";
var IS_TOUCH_SCREEN = ("ontouchstart" in window) || (navigator.maxTouchPoints > 0);
var IS_MOBILE_DEVICE = /Mobile/i.test(navigator.userAgent);
var ON_MB = location.host.match(/^((beta|test)\.)?musicbrainz\.(eu|org)/);
var ON_EDIT_PAGE = (ON_MB && location.pathname.match(/^\/edit\/\d+$/));
var ON_EDIT_SEARCH_PAGE = (ON_MB && location.pathname.match(/^\/.+(?:edits|subscribed)/));
var ON_RELEASE_EDITOR_PAGE = (ON_MB && document.querySelector("div#release-editor"));
var userjs = "jesus2099userjs94629";
var memories = 10;
var colours = {
ok: "greenyellow",
warning: "gold"
};
var notetextStorage = "jesus2099userjs::last_editnotetext";
var save = !localStorage.getItem(userjs + "forget") && (ON_EDIT_PAGE || !ON_EDIT_SEARCH_PAGE);
var content = document.querySelector(ON_MB ? "#page" : "div.content");
var saved_height = localStorage.getItem(userjs + "_savedHeight");
var notetext;
var submit_button;
waitForElements((ON_MB ? "#page" : "div.content") + " textarea" + (ON_MB ? ".edit-note, textarea#edit-note-text" : ""), init);
function init(edit_notes) {
if (edit_notes.length === 1) {
notetext = edit_notes[0];
if (!ON_MB) {
notetext.style.setProperty("height", "8em");
notetext.style.setProperty("width", "100%");
}
if (saved_height) {
notetext.style.setProperty("height", saved_height + "px");
addAfter(createTag("div", {s: {textAlign: "right"}}, createTag("a", {e: {click: function(event) {
localStorage.removeItem(userjs + "_savedHeight");
this.parentNode.replaceChild(document.createTextNode("Size reset! It will take effect at next page load."), this);
}}}, "↖Reset size")), notetext);
}
notetext.addEventListener("mouseup", function(event) {
if (this.offsetHeight != saved_height) {
localStorage.setItem(userjs + "_savedHeight", this.offsetHeight);
}
});
} else {
notetext = false;
}
submit_button = content.querySelector(ON_MB ? "form div.buttons button[type='submit'].submit.positive" : "input[type='submit']");
if (submit_button === null && ON_RELEASE_EDITOR_PAGE) submit_button = document.querySelector("button.positive[type='button'][data-click='submitEdits']");
if (submit_button === null && ON_EDIT_PAGE) submit_button = document.querySelector("form[action='/edit/enter_votes'] > span.buttons > button[type='submit']");
if (submit_button === null && location.href.match(/edit-relationships$/)) submit_button = document.querySelector("div#content.rel-editor form > div.row.no-label.buttons > button.submit.positive[type='submit']");
if (notetext) {
if (ON_MB) {
var carcan = getParent(notetext, "div", "half-width");
if (carcan) {
if (ON_RELEASE_EDITOR_PAGE) carcan.style.setProperty("width", "inherit");
else notetext.parentNode.style.setProperty("width", carcan.parentNode.offsetWidth + "px");
}
notetext.style.setProperty("width", "98%");
var removeLabels = ["label-id-ar.edit_note", "label-id-edit_note", "label-id-edit-artist.edit_note", "label-id-edit-label.edit_note", "label-id-edit-recording.edit_note", "label-id-edit-release-group.edit_note", "label-id-edit-url.edit_note", "label-id-edit-work.edit_note"];
for (var l = 0; l < removeLabels.length; l++) {
var label = document.getElementById(removeLabels[l]);
if (label) label.parentNode.removeChild(label);
}
}
var buttons = createTag("div", {a: {class: "buttons"}});
var save_checkbox = buttons.appendChild(createTag("label", {
a: {title: "save edit note"},
s: {backgroundColor: (save ? colours.ok : colours.warning), minWidth: "0", margin: "0"},
e: {
click: function(event) {
if (event[CONTROL_POMME.shift.key]) {
sendEvent(submit_button, "click");
}
}
}
}));
save_checkbox = save_checkbox.appendChild(createTag("input", {
a: {type: "checkbox", class: "jesus2099remember", tabindex: "-1"},
s: {display: "inline"},
e: {
change: function(event) {
save = this.checked;
this.parentNode.style.setProperty("background-color", save ? colours.ok : colours.warning);
localStorage.setItem(userjs + "forget", save ? "" : "1");
}
}
}));
save_checkbox.checked = save;
save_checkbox.parentNode.appendChild(document.createTextNode(" remember "));
buttons.appendChild(createClearButton());
for (var m = 0; m < memories; m++) {
buttons.appendChild(document.createTextNode(" "));
let butt = createButton("n-" + (+m + 1), "50px");
let buttid = notetextStorage + "0" + m;
butt.setAttribute("id", buttid);
let lastnotetext = localStorage.getItem(buttid);
if (!lastnotetext) {
butt.setAttribute("disabled", "true");
butt.style.setProperty("opacity", ".5");
} else {
butt.setAttribute("title", lastnotetext);
butt.setAttribute("value", summarise(lastnotetext));
if (IS_TOUCH_SCREEN) {
onLongPress(butt, function(event) {
if (confirm("Do you want to remove this memory?\n\n" + event.target.getAttribute("title"))) {
forget(event.target.getAttribute("id").match(/(\d)$/)[1]);
}
});
}
butt.addEventListener("click", function(event) {
if (event[CONTROL_POMME.ctrl.key]) {
forget(event.target.getAttribute("id").match(/(\d)$/)[1]);
notetext.focus();
} else {
forceValue(notetext, this.getAttribute("title"));
if (!IS_TOUCH_SCREEN) {
notetext.focus();
}
if (event[CONTROL_POMME.shift.key]) {
sendEvent(submit_button, "click");
}
}
}); // onclick
}
buttons.appendChild(butt);
}
if (IS_TOUCH_SCREEN) {
buttons.appendChild(document.createElement("br"));
buttons.appendChild(document.createTextNode("long touch: remove ↗"));
}
if (!IS_MOBILE_DEVICE || !IS_TOUCH_SCREEN) {
buttons.appendChild(document.createTextNode(" ← " + CONTROL_POMME.shift.label + "click: submit / " + CONTROL_POMME.ctrl.label + "click: remove"));
}
notetext.parentNode.insertBefore(buttons, notetext);
let lastnotetext = localStorage.getItem(notetextStorage + "00");
if (save && !ON_EDIT_SEARCH_PAGE && !ON_EDIT_PAGE && lastnotetext && notetext.value == "") {
forceValue(notetext, lastnotetext);
}
}
if (submit_button !== null) {
submit_button.addEventListener("click", saveNote);
submit_button.insertBefore(document.createTextNode("🐘 "), submit_button.firstChild);
} else if (!ON_EDIT_SEARCH_PAGE) {
// alert("Error: ELEPHANT did not find submit button and cannot save edit note.");
}
}
function saveNote() {
if (notetext) {
var thisnotetext = notetext.value.replace(/\u00a0—\u00a0[\r\n]{1,2}Merging into oldest \[MBID\] \(['\d,\s←+]+\)\./g, "").trim(); // linked in mb_MERGE-HELPOR-2.user.js
var ls00 = localStorage.getItem(notetextStorage + "00");
if (save && thisnotetext !== ls00) {
if (ls00 !== "") {
// remove earlier (rightwards) duplicates
for (var idel = memories - 1; idel > 0; idel--) {
if (thisnotetext === localStorage.getItem(notetextStorage + "0" + idel)) {
forget(idel);
}
}
// insert new note at the left and shift everything rightwards
for (var isav = memories - 1; isav > 0; isav--) {
var prev = localStorage.getItem(notetextStorage + "0" + (isav - 1));
if (prev) {
localStorage.setItem(notetextStorage + "0" + isav, prev);
}
}
}
localStorage.setItem(notetextStorage + "00", thisnotetext);
}
}
}
function forget(memory_index) {
if (memory_index >= 0 && memory_index < memories) {
for (var mi = memory_index; mi < memories; mi++) {
var memory_button = document.querySelector("[id='" + notetextStorage + "0" + mi + "']");
var next_memory = localStorage.getItem(notetextStorage + "0" + (+mi + 1));
if (next_memory === null) {
next_memory = "n-" + (+mi + 1);
localStorage.removeItem(notetextStorage + "0" + mi);
memory_button.removeAttribute("title");
memory_button.setAttribute("disabled", "true");
memory_button.style.setProperty("opacity", ".5");
} else {
localStorage.setItem(notetextStorage + "0" + mi, next_memory);
memory_button.setAttribute("title", next_memory);
}
memory_button.setAttribute("value", summarise(next_memory));
}
} else {
alert("Error while asked to forget memory n-" + (+memory_index + 1));
}
}
function createButton(label, width) {
let butt = createTag("input", {a: {type: "button", value: label, tabindex: "-1", class: "styled-button"}, s: {display: "inline", padding: "2px", float: "none"}});
if (width) {
butt.style.setProperty("width", width);
}
return butt;
}
function createClearButton() {
let butt = createButton("×", "25px");
butt.addEventListener("click", function(event) {
forceValue(notetext, "");
if (event[CONTROL_POMME.shift.key]) {
sendEvent(submit_button, "click");
} else {
notetext.focus();
}
}); // onclick
if (IS_TOUCH_SCREEN) {
onLongPress(butt, function(event) {
forceValue(notetext, "");
sendEvent(submit_button, "click");
});
}
butt.style.setProperty("color", "red");
butt.style.setProperty("background-color", colours.warning);
butt.setAttribute("title", "clear edit note");
return butt;
}
function summarise(full_edit_note) {
return full_edit_note.replace(/(http:\/\/|https:\/\/|www\.|[\n\r])/gi, "").substr(0, 6);
}
function waitForElements(selector, callback) {
var waitForElements_interval_id = setInterval(function() {
var elements = document.querySelectorAll(selector);
if (elements.length > 0) {
clearInterval(waitForElements_interval_id);
callback(elements);
}
}, 123);
}
function onLongPress(element, callback) {
// https://stackoverflow.com/a/60207895/2236179
let timer;
element.addEventListener("touchstart", function(event) {
timer = setTimeout(function() {
timer = null;
callback(event);
}, 500);
});
function cancel() {
clearTimeout(timer);
}
element.addEventListener("touchend", cancel);
element.addEventListener("touchcancel", cancel);
element.addEventListener("touchmove", cancel);
}