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