forked from jesus2099/konami-command
-
Notifications
You must be signed in to change notification settings - Fork 2
/
mb_ELEPHANT-EDITOR.user.js
359 lines (359 loc) · 16.3 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
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
// ==UserScript==
// @name mb. ELEPHANT EDITOR
// @version 2015.2.18.12.0
// @description musicbrainz.org + acoustid.org: Remember last edit notes and dates
// @homepage http://userscripts-mirror.org/scripts/show/94629
// @supportURL https://github.com/jesus2099/konami-command/issues
// @namespace https://github.com/jesus2099/konami-command
// @downloadURL https://raw.githubusercontent.com/jesus2099/konami-command/master/mb_ELEPHANT-EDITOR.user.js
// @updateURL https://raw.githubusercontent.com/jesus2099/konami-command/master/mb_ELEPHANT-EDITOR.user.js
// @author PATATE12
// @licence CC BY-NC-SA 3.0 (https://creativecommons.org/licenses/by-nc-sa/3.0/)
// @since 2011-01-13
// @icon data:image/gif;base64,R0lGODlhEAAQAKEDAP+/3/9/vwAAAP///yH/C05FVFNDQVBFMi4wAwEAAAAh/glqZXN1czIwOTkAIfkEAQACAwAsAAAAABAAEAAAAkCcL5nHlgFiWE3AiMFkNnvBed42CCJgmlsnplhyonIEZ8ElQY8U66X+oZF2ogkIYcFpKI6b4uls3pyKqfGJzRYAACH5BAEIAAMALAgABQAFAAMAAAIFhI8ioAUAIfkEAQgAAwAsCAAGAAUAAgAAAgSEDHgFADs=
// @grant none
// @include http*://*musicbrainz.org/*/add-alias
// @include http*://*musicbrainz.org/*/change-quality
// @include http*://*musicbrainz.org/*/create*
// @include http*://*musicbrainz.org/*/delete
// @include http*://*musicbrainz.org/*/edit
// @include http*://*musicbrainz.org/*/edit_annotation
// @include http*://*musicbrainz.org/*/merge
// @include http*://*musicbrainz.org/*/merge?*
// @include http*://*musicbrainz.org/*/move?dest=*
// @include http*://*musicbrainz.org/*/remove*
// @include http*://*musicbrainz.org/*edits*
// @include http*://*musicbrainz.org/artist/*/split
// @include http*://*musicbrainz.org/cdtoc/*
// @include http*://*musicbrainz.org/edit/*
// @include http*://*musicbrainz.org/edit/artist/add.html?*
// @include http*://*musicbrainz.org/edit/subscribed*
// @include http*://*musicbrainz.org/isrc/delete*
// @include http*://*musicbrainz.org/mod/*
// @include http*://*musicbrainz.org/recording/*/add-isrc
// @include http*://*musicbrainz.org/release*/*/*-cover-art*
// @include http*://*musicbrainz.org/release/*/edit-relationships
// @include http*://*musicbrainz.org/release/add*
// @include http*://*musicbrainz.org/work/*/add-iswc
// @include http*://acoustid.org/edit/*
// @exclude http*://classic.musicbrainz.org*
// @run-at document-end
// ==/UserScript==
(function(){
/* - --- - --- - --- - START OF CONFIGURATION - --- - --- - --- - */
var textLabels = ["n-1", "n-2", "n-3", "n-4", "n-5", "n-6", "n-7", "n-8", "n-9", "n-10"]; /* maximum 10 labels empty [] will skip buttons */
var delLabel = "×";
var copyDateLabels = ["↓", "↑", "copy date"];
var setPrevNoteOnLoad = true; /* "true" will restore last edit note on load (user can choose text with buttons in either case) */
var setPrevNoteOnEditPageLoad = false; /* "true" can be troublesome if you just want to vote, believe me */
var setPrevDateOnLoad = false; /* "true" can be troublesome when you don’t need date any more and you forget to clear it */
/*funky colours*/
var cOK = "greenyellow";
var cNG = "pink";
var cWARN = "gold";
/* - --- - --- - --- - END OF CONFIGURATION - --- - --- - --- - */
var userjs = "jesus2099userjs94629";
var notetextStorage = "jesus2099userjs::last_editnotetext";
var acoustid = location.href.match(/acoustid\.org\/edit\//);
var mb = !acoustid;
var editpage = (mb && location.href.match(/musicbrainz\.org\/edit\/\d+($|[?#&])/));
var editsearchpage = (mb && location.href.match(/musicbrainz\.org\/.+(?:edits|subscribed)/));
var re = (mb && document.querySelector("div#release-editor"));
var save = editpage||editsearchpage?false:true;
var content = document.getElementById(mb?"page":"content");
var savedHeight = localStorage.getItem(userjs+"_savedHeight");
if (content) {
var notetext = content.querySelectorAll("textarea"+(acoustid?"":".edit-note, textarea#edit-note-text"));
var reldates = [];
if (notetext.length == 1) {
notetext = notetext[0];
if (acoustid) {
notetext.style.setProperty("height", "8em");
notetext.style.setProperty("width", "100%");
} else {
reldates = content.querySelectorAll("span.partial-date");
}
/*saving edit note size / 86px and 100% defaults for release editor*/
notetext.setAttribute("init-height", (notetext.offsetHeight?notetext.offsetHeight:86)+"px");
notetext.setAttribute("init-width", notetext.offsetWidth?notetext.offsetWidth+"px":"100%");
if (savedHeight) {
notetext.style.setProperty("height", savedHeight+"px");
}
notetext.addEventListener("mouseup", function(e) {
if (this.offsetHeight != savedHeight) {
localStorage.setItem(userjs+"_savedHeight", this.offsetHeight);
}
});
notetext.setAttribute("title", (notetext.getAttribute("title")?"\n":"")+"double‑click to reset size");
notetext.addEventListener("dblclick", function(e) {
localStorage.removeItem(userjs+"_savedHeight");
this.style.setProperty("height", this.getAttribute("init-height"));
this.style.setProperty("width", this.getAttribute("init-width"));
});
} else { notetext = false; }
var xdate = [];
var submitbtn = content.querySelector(mb?"form div.buttons button[type='submit'].submit.positive":"input[type='submit']");
if (re) submitbtn = document.querySelector("button.positive[type='button'][data-click='submitEdits']");
if (reldates.length == 2) {
xdate = [
[
"jesus2099userjs::last_editbegindate",
reldates[0].querySelector("input[placeholder='YYYY']"),
reldates[0].querySelector("input[placeholder='MM']"),
reldates[0].querySelector("input[placeholder='DD']")
],
[
"jesus2099userjs::last_editenddate",
reldates[1].querySelector("input[placeholder='YYYY']"),
reldates[1].querySelector("input[placeholder='MM']"),
reldates[1].querySelector("input[placeholder='DD']")
]
];
}
if (notetext) {
if (mb) {
if (carcan = getParent(notetext, "div", "half-width")) {
if (re) carcan.style.setProperty("width", "inherit");
else notetext.parentNode.style.setProperty("width", carcan.parentNode.offsetWidth+"px");
if (xdate[0] && (fs = getParent(xdate[0][1], "fieldset"))) { fs.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++) if (label = document.getElementById(removeLabels[l])) label.parentNode.removeChild(label);
}
var buttons = createTag("div", {a:{"class":"buttons"}});
var savecb = buttons.appendChild(createTag("label", {a:{title:"saves edit note on page unload"}, s:{"background-color":(save?cOK:cWARN),"min-width":"0",margin:"0"}, e:{"click":function(e){if(e.shiftKey){sendEvent(submitbtn, "click");}}}}));
savecb = savecb.appendChild(createTag("input", {a:{type:"checkbox","class":"jesus2099remember",tabindex:"-1"}, s:{display:"inline"}, e:{"change":function(e){save=this.checked;this.parentNode.style.setProperty("background-color",save?cOK:cWARN);}}}));
savecb.checked = save;
savecb.parentNode.appendChild(document.createTextNode(" remember "));
for (var ni = 0; ni < textLabels.length; ni++) {
var butt = createButtor(textLabels[ni], "50px");
var buttid = notetextStorage+"0"+ni;
butt.setAttribute("id", buttid);
var lastnotetext = localStorage.getItem(buttid);
if (!lastnotetext) {
butt.setAttribute("disabled", "true");
butt.style.setProperty("opacity", ".5");
}
else {
butt.setAttribute("title", lastnotetext);
butt.setAttribute("value", lastnotetext.replace(/(http:\/\/|https:\/\/|www\.|[\n\r])/gi, "").substr(0, 6));
butt.addEventListener("click", function(e) {
notetext.value = this.getAttribute("title");
sendEvent(notetext, "change");
notetext.focus();
if (e.shiftKey) { sendEvent(submitbtn, "click"); }
}, false);/*onclick*/
}
buttons.appendChild(butt);
}
buttons.appendChild(createClearButtor("notetext"));
buttons.appendChild(document.createTextNode(" ← shift+click to submit right away"));
notetext.parentNode.insertBefore(buttons, notetext);
var lastnotetext = localStorage.getItem(notetextStorage+"00");
if (!editsearchpage && (!editpage && setPrevNoteOnLoad || editpage && setPrevNoteOnEditPageLoad) && lastnotetext && notetext.value == "") {
notetext.value = lastnotetext;
sendEvent(notetext, "change");
}
if (reldates.length == 2) {
createClearButtor("dates");
/*date memories*/
for (var ixd=0; ixd<xdate.length; ixd++) {
var lastdatey = localStorage.getItem(xdate[ixd][0]+"_y");
var lastdatem = localStorage.getItem(xdate[ixd][0]+"_m");
var lastdated = localStorage.getItem(xdate[ixd][0]+"_d");
var butt = createButtor(textLabels[0]);
butt.setAttribute("id", xdate[ixd][0]);
if (!lastdatey) {
butt.setAttribute("disabled", "true");
butt.style.setProperty("opacity", ".5");
}
else {
butt.setAttribute("title", lastdatey+"-"+lastdatem+"-"+lastdated);
butt.setAttribute("value", butt.getAttribute("title").replace(/-null/g, ""));
butt.addEventListener("click", function(e) {
var xdymd = this.getAttribute("title").match(/^(.*)-(.*)-(.*)$/);
for (var iixd=0, acts=["YYYY","MM","DD"]; iixd<3; iixd++) {
var input = this.parentNode.querySelector("input[placeholder='"+acts[iixd]+"']");
input.value = xdymd[iixd+1].match(/^\d+$/);
sendEvent(input, "change");
if (iixd == 0) focusYYYY(input);
}
if (e.shiftKey) {
sendEvent(this.parentNode.getElementsByTagName("input")[3], "click");
}
}, false);/*onclick*/
}
xdate[ixd][1].parentNode.setAttribute("title", "shift+click to change both dates at once");
addAfter(butt, xdate[ixd][3]);
addAfter(document.createTextNode(" "), xdate[ixd][3]);
if (setPrevDateOnLoad && localStorage.getItem(xdate[ixd][0]) == "1") {
sendEvent(document.getElementById(xdate[ixd][0]), "click");
}
}
/*copy dates*/
for (var icd=0; icd < 2; icd++) {
var buttcd = createButtor(copyDateLabels[icd]);
buttcd.setAttribute("title", copyDateLabels[2]);
buttcd.addEventListener("click", function(e) {
var src = copyDateLabels.indexOf(this.getAttribute("value"));
for (var icdymd=1; icdymd<4; icdymd++) {
xdate[src==1?0:1][icdymd].value = xdate[src][icdymd].value;
sendEvent(xdate[src==1?0:1][icdymd], "change");
}
focusYYYY(xdate[src][1]);
}, false);/*onclick*/
addAfter(buttcd, xdate[icd][3]);
addAfter(document.createTextNode(" "), xdate[icd][3]);
}
}
}
if (location.href.match(/edit-relationships$/) && (sub = document.querySelector("div#content.rel-editor > form > div.row.no-label.buttons > button.submit.positive[type='submit']"))) {
sub.addEventListener("click", saveNote, false);
}
self.addEventListener("unload", saveNote, false);
}
function saveNote() {
if (notetext) {
if (textLabels.length > 0) {
var thisnotetext = notetext.value.replace(/(^[\n\r\s\t]+|[\n\r\s\t]+$)/gi, "");
var ls00 = localStorage.getItem(notetextStorage+"00");
if (save && thisnotetext != ls00) {
if (ls00 != "") {
for (var isav=textLabels.length-1; isav > 0 ; isav--) {
var prev = localStorage.getItem(notetextStorage+"0"+(isav-1));
if (prev) {
localStorage.setItem(notetextStorage+"0"+isav, localStorage.getItem(notetextStorage+"0"+(isav-1)));
}
}
}
localStorage.setItem(notetextStorage+"00", thisnotetext);
}
}
if (reldates.length == 2) {
for (var ixd=0; ixd<xdate.length; ixd++) {
var ndy = xdate[ixd][1].value;
var ndm = xdate[ixd][2].value;
var ndd = xdate[ixd][3].value;
if (ndy.match(/^\d{4}$/)) {
localStorage.setItem(xdate[ixd][0], "1");
localStorage.setItem(xdate[ixd][0]+"_y", ndy);
if (ndm.match(/^\d{1,2}$/)) {
localStorage.setItem(xdate[ixd][0]+"_m", ndm);
if (ndd.match(/^\d{1,2}$/)) {
localStorage.setItem(xdate[ixd][0]+"_d", ndd);
}
else {
localStorage.removeItem(xdate[ixd][0]+"_d");
}
}
else {
localStorage.removeItem(xdate[ixd][0]+"_m");
localStorage.removeItem(xdate[ixd][0]+"_d");
}
}
else if (ndy.trim() == "") {
localStorage.setItem(xdate[ixd][0], "0");
}
}
}
}
}
function createTag(tag, gadgets, children) {
var t = (tag=="fragment"?document.createDocumentFragment():document.createElement(tag));
if(t.tagName) {
if (gadgets) {
for (var attri in gadgets.a) if (gadgets.a.hasOwnProperty(attri)) { t.setAttribute(attri, gadgets.a[attri]); }
for (var style in gadgets.s) if (gadgets.s.hasOwnProperty(style)) { t.style.setProperty(style.replace(/!/g,""), gadgets.s[style].replace(/!/g,""), style.match(/!/)||gadgets.s[style].match(/!/)?"important":""); }
for (var event in gadgets.e) if (gadgets.e.hasOwnProperty(event)) { t.addEventListener(event, gadgets.e[event], false); }
}
if (t.tagName == "A" && !t.getAttribute("href") && !t.style.getPropertyValue("cursor")) t.style.setProperty("cursor", "pointer");
}
if (children) { var chldrn = children; if (typeof chldrn == "string" || chldrn.tagName) { chldrn = [chldrn]; } for(var child=0; child<chldrn.length; child++) { t.appendChild(typeof chldrn[child]=="string"?document.createTextNode(chldrn[child]):chldrn[child]); } t.normalize(); }
return t;
}
function createButtor(label, width) {
var butt = createTag("input", {a:{type:"button",value:label,tabindex:"-1"}, s:{display:"inline",padding:"2px","float":"none"}});
if (width) { butt.style.setProperty("width", width); }
return butt;
}
function createClearButtor(input) {
switch (input) {
case "notetext":
var butt = createButtor(delLabel, "25px");
butt.addEventListener("click", function(e) {
notetext.value = "";
sendEvent(notetext, "change");
notetext.focus();
if (e.shiftKey) { sendEvent(submitbtn, "click"); }
}, false);/*onclick*/
butt.style.setProperty("color", "red");
butt.style.setProperty("background-color", cWARN);
return butt;
break;
case "dates":
for (var i=0; i<2; i++) {
var butt = createButtor(delLabel, "25px");
butt.setAttribute("id", userjs+"deldate"+i);
butt.addEventListener("click", function(e) {
var id = this.getAttribute("id").charAt((userjs+"deldate").length);
for (var nii=1; nii<4; nii++) {
xdate[id][nii].value = "";
sendEvent(xdate[id][nii], "change");
}
if (e.shiftKey) { sendEvent(document.getElementById(userjs+"deldate"+(id==0?1:0)), "click"); }
focusYYYY(xdate[id][1]);
e.cancelBubble = true;
if (e.stopPropagation) e.stopPropagation();
}, false);/*onclick*/
addAfter(butt, xdate[i][3]);
addAfter(document.createTextNode(" "), xdate[i][3]);
}
break;
}
}
function addAfter(n, e) {
if (n && e && e.parentNode) {
if (e.nextSibling) { return e.parentNode.insertBefore(n, e.nextSibling); }
else { return e.parentNode.appendChild(n); }
} else { return null; }
}
function getParent(obj, tag, cls) {
var cur = obj;
if (cur.parentNode) {
cur = cur.parentNode;
if (cur.tagName == tag.toUpperCase() && (!cls || cls && cur.className.match(new RegExp("\\W*"+cls+"\\W*")))) {
return cur;
} else {
return getParent(cur, tag, cls);
}
} else {
return null;
}
}
function sendEvent(n, _e){
var e = _e.toLowerCase();
var ev;
if (e.match(/click|mouse/)) {
var params = {};
params.mods = [];
if (e.match(/\+/)) {
params.mods = e.split("+");
e = params.mods.pop();
}
ev = document.createEvent("MouseEvents");
ev.initMouseEvent(e, true, true, self, 0, 0, 0, 0, 0, params.mods.indexOf("ctrl")>-1, params.mods.indexOf("alt")>-1, params.mods.indexOf("shift")>-1, params.mods.indexOf("meta")>-1, 0, null);
}
else {
ev = document.createEvent("HTMLEvents");
ev.initEvent(e, true, true);
}
n.dispatchEvent(ev);
}
function focusYYYY(input) {
if(input.style.getPropertyValue("display") == "none" && input.nextSibling.getAttribute("placeholder") == "YYY+")/*EASY_DATE*/
input.nextSibling.focus();
else
input.focus();
}
})();