SHOW:
|
|
- or go back to the newest paste.
1 | // ==UserScript== | |
2 | // @name MusicBrainz: Set recording comments for a release | |
3 | - | // @version 2011-12-27 |
3 | + | // @version 2012-05-16 |
4 | // @author Michael Wiencek | |
5 | // @namespace http://userscripts.org/users/266906 | |
6 | // @include http://musicbrainz.org/release/* | |
7 | // @include http://test.musicbrainz.org/release/* | |
8 | // @include http://beta.musicbrainz.org/release/* | |
9 | // @match http://musicbrainz.org/release/* | |
10 | // @match http://test.musicbrainz.org/release/* | |
11 | // @match http://beta.musicbrainz.org/release/* | |
12 | // ==/UserScript== | |
13 | ||
14 | var scr = document.createElement("script"); | |
15 | scr.textContent = "(" + recording_comments + ")();"; | |
16 | document.body.appendChild(scr); | |
17 | ||
18 | function recording_comments() { | |
19 | var $tracks = $("tr").filter(function() {return $(this).attr("typeof") == "mo:Track";}); | |
20 | if ($tracks.length == 0) | |
21 | - | location = window.location.href; |
21 | + | |
22 | - | if (location.match(/\/edits$/) || location.match(/\/release\/add/)) |
22 | + | |
23 | var location = window.location.pathname; | |
24 | if (!location.match(/\/release\/[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/)) | |
25 | - | var release = location.match(MBID_REGEX)[0], |
25 | + | |
26 | ||
27 | var MBID_REGEX = /[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}/, | |
28 | release = location.match(MBID_REGEX)[0], | |
29 | editing = false, submitting = false; | |
30 | ||
31 | function RequestManager(rate) { | |
32 | this.queue = []; | |
33 | this.last = 0; | |
34 | ||
35 | this.next = function() { | |
36 | var request = this.queue.shift(); | |
37 | if (request) { | |
38 | request(); | |
39 | this.last = new Date().getTime(); | |
40 | if (this.queue.length > 0) { | |
41 | setTimeout(function(foo) {foo.next();}, rate, this); | |
42 | } | |
43 | } | |
44 | } | |
45 | ||
46 | this.push = function(req) { | |
47 | this.queue.push(req); | |
48 | if (this.queue.length == 1) | |
49 | this.start_queue(); | |
50 | } | |
51 | ||
52 | this.unshift = function(req) { | |
53 | this.queue.unshift(req); | |
54 | if (this.queue.length == 1) | |
55 | this.start_queue(); | |
56 | } | |
57 | ||
58 | this.start_queue = function() { | |
59 | var now = new Date().getTime(); | |
60 | if (now - this.last >= rate) { | |
61 | this.next(); | |
62 | } else { | |
63 | setTimeout(function(foo) {foo.next();}, | |
64 | rate - now + this.last, this); | |
65 | } | |
66 | } | |
67 | } | |
68 | ||
69 | function button(text) { | |
70 | return $("<button>" + text + "</button>") | |
71 | .css({"color": "#565656", | |
72 | "background-color": "#F0F0F0", | |
73 | "border": "1px solid #D0D0D0", | |
74 | "border-top": "1px solid #EAEAEA", | |
75 | "border-left": "1px solid #EAEAEA"}); | |
76 | } | |
77 | ||
78 | $.each($tracks, function(i, track) { | |
79 | var $td = $($(track).children("td")[1]), node = $td.children(".mp")[0]; | |
80 | if (node === undefined) { | |
81 | node = $td.children("a[rel=mo\\:publication_of]")[0]; | |
82 | } | |
83 | $('<input type="text" class="recording-comment">') | |
84 | .insertAfter(node) | |
85 | .css({"background": "inherit", "border": "1px #999 solid", "width": "18em", "margin-left": "0.5em"}) | |
86 | .hide(); | |
87 | }); | |
88 | ||
89 | var $inputs = $(".recording-comment") | |
90 | .bind("input.rc", function() { | |
91 | var comment = this.value, old = $(this).data("old"); | |
92 | if (comment == old) { | |
93 | $(this).css("border-color", "#999"); | |
94 | } else { | |
95 | $(this).css("border-color", "red"); | |
96 | } | |
97 | }); | |
98 | ||
99 | var $container = $("<div></div>") | |
100 | .css({"background": "#F1F1F1", "border": "1px #999 dotted", "display": "inline-block"}) | |
101 | .insertBefore("table.tbl"); | |
102 | ||
103 | var $edit_button = button("Edit recording comments") | |
104 | .bind("click", function() { | |
105 | if (editing) { | |
106 | $(this).text("Edit recording comments"); | |
107 | $("#rc-table").hide(); | |
108 | $inputs.hide(); | |
109 | } else { | |
110 | $(this).text("Hide recording comments"); | |
111 | $("#rc-table").show(); | |
112 | $inputs.show(); | |
113 | } | |
114 | editing = !editing; | |
115 | }) | |
116 | .appendTo($container); | |
117 | ||
118 | $container.append('\ | |
119 | <table id="rc-table">\ | |
120 | <tr>\ | |
121 | <td><label for="all-comments">Set all comments to:</label></td>\ | |
122 | <td><input type="text" id="all-comments" style="width: 18em;"></td>\ | |
123 | </tr>\ | |
124 | <tr>\ | |
125 | <td><label for="rc-edit-note">Edit note:</label></td>\ | |
126 | <td><input type="text" id="rc-edit-note" style="width: 18em;"></td>\ | |
127 | </tr>\ | |
128 | <tr>\ | |
129 | <td></td><td></td>\ | |
130 | </tr>\ | |
131 | </table>'); | |
132 | $("#rc-table").hide(); | |
133 | ||
134 | var $all_comments = $("#all-comments") | |
135 | .bind("input", function() { | |
136 | - | var edit_requests = new RequestManager(1000); |
136 | + | |
137 | }); | |
138 | ||
139 | var edit_requests = new RequestManager(2000); | |
140 | ||
141 | var $submit_button = button("Submit changes (marked red)") | |
142 | .bind("click", function() { | |
143 | if (submitting) { | |
144 | edit_requests.queue = []; | |
145 | submitting = false; | |
146 | $(this).text("Submit changes (marked red)"); | |
147 | $inputs.attr("disabled", false).trigger("input.rc"); | |
148 | return; | |
149 | } | |
150 | submitting = true; | |
151 | $(this).text("Submitting...click to cancel!"); | |
152 | $inputs.attr("disabled", true); | |
153 | var edit_count = 0; | |
154 | ||
155 | $.each($tracks, function(i, track) { | |
156 | var input = $inputs[i], comment = input.value; | |
157 | if (comment == $(input).data("old")) { | |
158 | input.disabled = false; | |
159 | return; | |
160 | } | |
161 | edit_count += 1; | |
162 | var link = $($(track).children("td")[1]).find("a[rel=mo\\:publication_of]")[0], | |
163 | - | $.get("/recording/" + mbid + "/edit", function(html, textStatus, jqXHR) { |
163 | + | |
164 | - | if (jqXHR.status != 200) { |
164 | + | |
165 | - | edit_requests.push(request); |
165 | + | |
166 | - | return; |
166 | + | $.get("/recording/" + mbid + "/edit", function(html) { |
167 | $(input).css("border-color", "blue"); | |
168 | var $html = $(html), data = {}; | |
169 | ||
170 | - | var xml = $.parseXML(html.replace(/ /g, "")), data = {}; |
170 | + | data["edit-recording.name"] = $html.find("#id-edit-recording\\.name").val(); |
171 | data["edit-recording.length"] = $html.find("#id-edit-recording\\.length").val(); | |
172 | - | data["edit-recording.name"] = xml.getElementById("id-edit-recording.name").value; |
172 | + | |
173 | - | data["edit-recording.length"] = xml.getElementById("id-edit-recording.length").value; |
173 | + | |
174 | var a = []; | |
175 | a[0] = "edit-recording.artist_credit.names." + i + ".artist.name"; | |
176 | a[1] = "edit-recording.artist_credit.names." + i + ".artist.id"; | |
177 | a[2] = "edit-recording.artist_credit.names." + i + ".name"; | |
178 | a[3] = "edit-recording.artist_credit.names." + i + ".join_phrase"; | |
179 | var $artist_name = $html.find("#id-" + a[0].replace(/\./g, "\\.")); | |
180 | if ($artist_name.length == 0) | |
181 | - | var artist_name = xml.getElementById("id-" + a[0]); |
181 | + | |
182 | - | if (artist_name == null) |
182 | + | data[a[0]] = $artist_name.val(); |
183 | data[a[1]] = $html.find("#id-" + a[1].replace(/\./g, "\\.")).val(); | |
184 | - | data[a[0]] = artist_name.value; |
184 | + | data[a[2]] = $html.find("#id-" + a[2].replace(/\./g, "\\.")).val(); |
185 | - | data[a[1]] = xml.getElementById("id-" + a[1]).value; |
185 | + | data[a[3]] = $html.find("#id-" + a[3].replace(/\./g, "\\.")).val(); |
186 | - | data[a[2]] = xml.getElementById("id-" + a[2]).value; |
186 | + | |
187 | - | data[a[3]] = xml.getElementById("id-" + a[3]).value; |
187 | + | |
188 | data["edit-recording.edit_note"] = $("#rc-edit-note").val(); | |
189 | data["edit-recording.as_auto_editor"] = "1"; | |
190 | data["edit-recording.comment"] = comment; | |
191 | ||
192 | var post_edit = function() { | |
193 | $.post("/recording/" + mbid + "/edit", data, | |
194 | function(data) { | |
195 | $(input) | |
196 | .data("old", comment) | |
197 | .trigger("input.rc") | |
198 | .attr("disabled", false); | |
199 | if (edit_requests.queue.length == 0) | |
200 | $submit_button.attr("disabled", false).text("Submit changes (marked red)"); | |
201 | }).error(function() { | |
202 | $(input) | |
203 | .css("border-color", "red") | |
204 | .attr("disabled", false); | |
205 | }); | |
206 | }; | |
207 | - | edit_requests.unshift(post_edit); |
207 | + | |
208 | ||
209 | }) | |
210 | }; | |
211 | edit_requests.push(request); | |
212 | - | }).error(function() { |
212 | + | |
213 | - | edit_requests.push(request); |
213 | + | |
214 | - | }); |
214 | + | |
215 | $(this).attr("disabled", false).text("Submit changes (marked red)"); | |
216 | }) | |
217 | .appendTo($container.find("td")[5]); | |
218 | ||
219 | $.get("/ws/2/release/" + release + "?inc=recordings", function(data) { | |
220 | var $recordings = $(data).find("recording"); | |
221 | for (var i = 0; i < $recordings.length; i++) { | |
222 | var recording = $recordings[i], input = $inputs[i], | |
223 | disambiguation = $(recording).children("disambiguation")[0]; | |
224 | if (disambiguation === undefined) { | |
225 | $(input).data("old", ""); | |
226 | continue; | |
227 | } | |
228 | var comment = $(disambiguation).text(); | |
229 | $(input).val(comment).data("old", comment); | |
230 | } | |
231 | }); | |
232 | } |