forked from jesus2099/konami-command
-
Notifications
You must be signed in to change notification settings - Fork 2
/
mb_HYPER-MOULINETTE.user.js
240 lines (240 loc) · 13 KB
/
mb_HYPER-MOULINETTE.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
(function(){"use strict";var meta={rawmdb:function(){
// ==UserScript==
// @name mb. HYPER MOULINETTE
// @version 2014.11.24.1451
// @description musicbrainz.org: Mass PUT or DELETE releases in a collection from an edit search or an other collection
// @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_HYPER-MOULINETTE.user.js
// @updateURL https://raw.githubusercontent.com/jesus2099/konami-command/master/mb_HYPER-MOULINETTE.user.js
// @author PATATE12 aka. jesus2099/shamo
// @licence CC BY-NC-SA 3.0 (https://creativecommons.org/licenses/by-nc-sa/3.0/)
// @since 2014-09-19
// @icon data:image/gif;base64,R0lGODlhEAAQAKEDAP+/3/9/vwAAAP///yH/C05FVFNDQVBFMi4wAwEAAAAh/glqZXN1czIwOTkAIfkEAQACAwAsAAAAABAAEAAAAkCcL5nHlgFiWE3AiMFkNnvBed42CCJgmlsnplhyonIEZ8ElQY8U66X+oZF2ogkIYcFpKI6b4uls3pyKqfGJzRYAACH5BAEIAAMALAgABQAFAAMAAAIFhI8ioAUAIfkEAQgAAwAsCAAGAAUAAgAAAgSEDHgFADs=
// @grant none
// @include http*://*musicbrainz.org/user/*/collections
// @include http://*.mbsandbox.org/user/*/collections
// @exclude *//*/*mbsandbox.org/*
// @exclude *//*/*musicbrainz.org/*
// @run-at document-end
// ==/UserScript==
}};
if (meta.rawmdb && meta.rawmdb.toString && (meta.rawmdb = meta.rawmdb.toString())) {
var kv/*key,val*/, row = /\/\/\s+@(\S+)\s+(.+)/g;
while ((kv = row.exec(meta.rawmdb)) !== null) {
if (meta[kv[1]]) {
if (typeof meta[kv[1]] == "string") meta[kv[1]] = [meta[kv[1]]];
meta[kv[1]].push(kv[2]);
} else meta[kv[1]] = kv[2];
}
}
var DEBUG = false;
meta.key = "jesus2099"+meta.name.replace(/^mb\. /, "").replace(/ /, "-");
var MBS = self.location.protocol+"//"+self.location.host;
var stre_GUID = "[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}";
var re_GUID = new RegExp(stre_GUID);
var account = document.querySelector("div#header-menu li.account");
var target, method, source, client, loaders = [];
var crawlType = {
"^/collection/": "div#content table.tbl a[href*='/release/']",
"^/search/edits": "div.edit-details a[href*='/release/']",
};
var genuineTitle = document.title;
/*==========================================================================
## MENU ITEM ##
find this script in MB "Editing" menu
==========================================================================*/
if (self.location.href.match(/\/collections/) && document.querySelector("h1").textContent == account.querySelector("a").textContent) {
var hdr = document.querySelector("table.tbl > thead > tr").appendChild(createTag("th", {a:{"class":"hypermouli-header"}}, [meta.name.replace(/.+\. /, "")+(self.opera?" (does not work in Opera!)":""),createTag("br"),meta.description.replace(/.+:/, "")]));
var colls = document.querySelectorAll("table.tbl > tbody > tr");
for (var c=0; c<colls.length; c++) {
colls[c].appendChild(createTag("td", {}, [createTag("a", {e:{click:mouli}}, "Put"), " | ", createTag("a", {e:{click:mouli}}, "Delete")]));
}
}
/*==========================================================================
## THE BIG ONE ## goes here
==========================================================================*/
function mouli() {
target = this.parentNode.parentNode.querySelector("a[href*='/collection/']").getAttribute("href").match(re_GUID);
method = this.textContent.toLowerCase();
source = prompt("Please paste your release edit search or other collection URL here.\nIt will parse these pages to modify the previously mentionned collection.", localStorage.getItem(meta.key+"_"+method+"-source")||"");
client = meta.name.replace(/^mb\. /, "").replace(/ /g, ".").toLowerCase()+"-"+meta.version;
if (target && method.match(/^(put|delete)$/i) && source) {
localStorage.setItem(meta.key+"_"+method+"-source", source);
source = source.replace(/^(https?:\/\/[^\/]+)?(\/.+)/, "$2");
modal(createTag("h3", {}, [createTag("a", {a:{href:meta.namespace, target:"_blank"}}, meta.name), " ", createTag("b", {}, meta.version), " (reload this page to abort)"]));
if (source.match(/^\/search\/edits/)) {
modal(createTag("p", {}, "(loading first page of an edit search is always long)"));
}
loadForExtract(source.replace(/([\?&])page=\d+&*/g, "$1")+(source.match(/\?/)?"&":"?")+"page=1");
} else {
alert("syntax error\ntarget: "+target+"\nmethod: "+method+" (should be either “put” or “delete”)\nsource: "+source);
}
}
function loadForExtract(page) {
var xhr = new XMLHttpRequest();
loaders[xhr.getID()] = {maxRetry:5, url:page};
xhr.addEventListener("load", function(e) {
var sPage = loaders[this.getID()].url.match(/page=(\d+)$/)[1];
var iPage = parseInt(sPage, 10);
var suffix = {1:"st",2:"nd",3:"rd",digit:sPage.match(/(?:^|[02-9])([123])$/)};
sPage = sPage+(suffix.digit?suffix[suffix.digit[1]]:"th")+" page";
var ploaded = modal(createTag("h4", {}, [createTag("a", {a:{href:loaders[this.getID()].url, target:"_blank"}}, sPage), " loaded"]));
document.title = sPage+" loaded (HYPER MOULINETTE) "+genuineTitle;
var res = document.createElement("html"); res.innerHTML = this.responseText;
var releases;
for (var type in crawlType) if (crawlType.hasOwnProperty(type) && loaders[this.getID()].url.match(new RegExp(type))) {
releases = res.querySelectorAll(crawlType[type]);
ploaded.appendChild(document.createTextNode(" ("+releases.length+" release"+(releases.length==1?"":"s")+"):"));
if (releases.length > 0) {
var url = "/ws/2/collection/"+target+"/releases/";
var cont = modal(createTag("table"));
for (var r=0; r < releases.length; r++) {
var guid = releases[r].getAttribute("href").match(re_GUID);
cont.appendChild(createTag("tr", {}, [createTag("th", {s:{"padding-right":"6px"}}, (r+1)+"."), createTag("td", {s:{padding:"0px"}}, createTag("img", {a:{src:"http://coverartarchive.org/release/"+guid+"/front-250",alt:""},s:{margin:"0px","max-height":"16px","max-width":"16px","box-shadow":"1px 1px 2px black"},e:{error:function(){this.parentNode.removeChild(this);}}})), createTag("td", {s:{"padding-left":"6px"}}, createTag("a", {a:{href:releases[r].getAttribute("href"),target:"_blank"}}, releases[r].textContent))]));
url += (r==0?"":";")+guid;
}
requestForAction(method, url+"?client="+client);
}
break;
}
var lastPage;
if ((lastPage = res.querySelector("p.pageselector > a:last-of-type")) && (lastPage = lastPage.getAttribute("href").match(/page=(\d+)/))) {
lastPage = parseInt(lastPage[1]);
}
if (iPage < lastPage) {
loadForExtract(page.replace(/(page=)\d+$/, "$1"+(iPage+1)));
} else {
document.title = genuineTitle;
modal(createTag("h3", {}, ["Last page processed (", createTag("a",{e:{click:function(){location.reload()}}},"RELOAD"), " page to quit this crap)."]));
}
});
xhr.addEventListener("error", function(e) {
if (--loaders[this.getID()].maxRetry > 0) {
modal(createTag("fragment", {}, ["Error loading ", createTag("a", {a:{href:loaders[this.getID()].url, target:"_blank"}}, "page"), ", retrying…"]));
loadForExtract(loaders[this.getID()].url);
} else {
alert("XHR-"+this.getID()+" ERROR "+this.status+"\nStopped retrying.\n"+loaders[this.getID].url+"\n\n"+this.responseText);
}
});
xhr.openDebug("get", page);
xhr.sendDebug(null);
}
function requestForAction(method, url) {
if (self.opera) { modal(createTag("p", {}, ["Will not perform ",createTag("a", {a:{href:url,target:"_blank"}}, method)," (auth-digest does not work in Opera)."])); }
else {
var xhr = new XMLHttpRequest();
loaders[xhr.getID()] = {method:method, url:url, maxRetry:5};
xhr.addEventListener("load", function(e) {
var node, res = this.responseXML.documentElement;
var msg = createTag("fragment", {}, ["Releases “", createTag("a", {a:{title:loaders[this.getID()].method, href:loaders[this.getID()].url, target:"_blank"}}, loaders[this.getID()].method), "” on collection "]);
if ((node = res.querySelector("message text")) && node.textContent.match(/ok/i)) {
modal(createTag("p", {}, [msg, "OK."]));
} else {
modal(createTag("p", {}, [msg, "failed.\n\n"+res.textContent]));
}
});
xhr.addEventListener("error", function(e) {
if (--loaders[this.getID()].maxRetry > 0) {
modal(createTag("fragment", {}, ["Error performing ", createTag("a", {a:{title:loaders[this.getID()].method, href:loaders[this.getID()].url, target:"_blank"}}, "“"+loaders[this.getID()].method+"” method"), ", retrying…"]));
loadForAction(loaders[this.getID()].method, loaders[this.getID()].url);
} else {
alert("XHR-"+this.getID()+" ERROR "+this.status+"\nStopped retrying.\n"+loaders[this.getID].url+"\n\n"+this.responseText);
}
});
xhr.openDebug(method, url);
xhr.overrideMimeType("text/xml");
xhr.sendDebug(null);
}
}
/*======================================
## hacked from COLLECTION HIGHLIGHTER ##
======================================*/
function modal(txt) {
var obj = document.getElementById(meta.key+"modal");
if (txt && !obj) {
coolstuff("div", "50", "100%", "100%", "black", ".6");
obj = coolstuff("div", "55", "800px", "600px", "white");
obj.setAttribute("id", meta.key+"modal");
obj.style.padding = "4px";
obj.style.overflow = "auto";
obj.style.whiteSpace = "nowrap";
obj.style.border = "4px solid black";
obj.addEventListener("mouseover", function(e) { this.style.borderColor = "silver"; }, false);
obj.addEventListener("mouseout", function(e) { this.style.borderColor = "black"; }, false);
}
if (txt && obj) {
var ret = obj.appendChild(typeof txt=="string"?document.createTextNode(txt):txt);
if (obj.style.borderColor == "black") { obj.scrollTop = obj.scrollHeight; }
return ret;
}
if (!txt && obj) {
obj.parentNode.removeChild(obj.previousSibling);
obj.parentNode.removeChild(obj);
}
function coolstuff(t,z,x,y,b,o,a) {
var truc = document.getElementsByTagName("body")[0].appendChild(document.createElement(t));
truc.style.position = "fixed";
truc.style.zIndex = z;
truc.style.width = x;
var xx = x.match(/^([0-9]+)(px|%)$/);
if (xx) {
truc.style.left = ((xx[2]=="%"?100:self.innerWidth)-xx[1])/2+xx[2];
}
truc.style.height = y;
var yy = y.match(/^([0-9]+)(px|%)$/);
if (yy) {
truc.style.top = ((yy[2]=="%"?100:self.innerHeight)-yy[1])/2+yy[2];
}
if (b) { truc.style.background = b; }
if (o) { truc.style.opacity = o; }
return truc;
}
}
/*================================================================================
## adaptation of JULIEN COUVREUR inspired code at http://oreilly.com/pub/h/4133 ##
================================================================================*/
XMLHttpRequest.prototype.getID = function() {
if (!this.id) {
this.id = Math.floor(Math.random() * 1000);
}
return this.id;
};
XMLHttpRequest.prototype.openDebug = function(method, url) {
if (DEBUG) { console.log("XHR-"+this.getID()+" open "+method.toUpperCase()+" "+url); }
this.open(method, url);
};
XMLHttpRequest.prototype.sendDebug = function(params) {
if (DEBUG) {
console.log("XHR-"+this.getID()+" send("+params+")");
var loaded = function(e) {
console.log("XHR-"+this.getID()+" "+e.type+" "+this.status);
};
this.addEventListener("load", loaded, false);
this.addEventListener("error", loaded, false);
}
this.send(params);
};
/*==========================================================================
## MY COMMON CRAP : https://github.com/jesus2099/javascript-patate12chips ##
==========================================================================*/
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 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;
}
})();