Skip to content

Instantly share code, notes, and snippets.

@jesus2099
Last active October 7, 2015 21:27
Show Gist options
  • Save jesus2099/3227572 to your computer and use it in GitHub Desktop.
Save jesus2099/3227572 to your computer and use it in GitHub Desktop.
Edit of bitmap’s version 2012-05-26 found on http://mbsandbox.org/~bitmap/set-recording-comments.user.js by jesus2099 : textarea for edit note and larger recording comment text field
// ==UserScript==
// @name MusicBrainz: Set recording comments for a release
// @version 2013-12-19~1_JESUS2099-2013.1221.2058
// @author Michael Wiencek
// @namespace https://gist.github.com/jesus2099/3227572
// @include *://musicbrainz.org/release/*
// @include *://test.musicbrainz.org/release/*
// @include *://beta.musicbrainz.org/release/*
// @include *://*.mbsandbox.org/release/*
// @match *://musicbrainz.org/release/*
// @match *://test.musicbrainz.org/release/*
// @match *://beta.musicbrainz.org/release/*
// @match *://*.mbsandbox.org/release/*
// ==/UserScript==
/* original from http://mbsandbox.org/~bitmap/ i just added a textarea for edit note (#148) and wider comment input fields (#38 #144 #148) */
var scr = document.createElement("script");
scr.textContent = "$(" + recording_comments + ");";
document.body.appendChild(scr);
function recording_comments() {
var $tracks, $inputs, nameColumn = 1;
function init() {
if ($("th.video").length > 0) {
nameColumn = 2;
}
$tracks = $("tr[rel=mo\\:track]").each(function () {
var $td = $(this).children("td").eq(nameColumn),
node = ($td.find(".mp")[0] ||
$td.find(".name-variation")[0] ||
$td.find("a[href^=\\/recording\\/]")[0]);
$('<input type="text" class="recording-comment">')
.insertAfter(node)
.css({
"background": "inherit",
"border": "1px #999 solid",
"width": "32em",
"margin-left": "0.5em"
})
.hide();
});
$inputs = $(".recording-comment");
var release = location.match(MBID_REGEX)[0];
$.get("/ws/2/release/" + release + "?inc=recordings&fmt=json",
function (data) {
var comments = _.pluck(_.pluck(_.flatten(_.pluck(
data.media, "tracks")), "recording"), "disambiguation");
for (var i = 0, len = comments.length; i < len; i++) {
var comment = comments[i];
$inputs.eq(i).val(comment).data("old", comment);
}
});
}
_.delay(init, 1000);
var location = window.location.pathname;
if (!location.match(/\/release\/[a-f\d]{8}-[a-f\d]{4}-[a-f\d]{4}-[a-f\d]{4}-[a-f\d]{12}$/)) {
return;
}
var MBID_REGEX = /[a-f\d]{8}-[a-f\d]{4}-[a-f\d]{4}-[a-f\d]{4}-[a-f\d]{12}/,
editing = false,
submitting = false;
function RequestManager(rate) {
this.queue = [];
this.last = 0;
this.next = function () {
var request = this.queue.shift();
if (request) {
request();
this.last = new Date().getTime();
if (this.queue.length > 0) {
setTimeout(_.bind(this.next, this), rate);
}
}
};
this.push = function (req) {
this.queue.push(req);
if (this.queue.length === 1) {
this.start_queue();
}
};
this.unshift = function (req) {
this.queue.unshift(req);
if (this.queue.length === 1) {
this.start_queue();
}
};
this.start_queue = function () {
var now = new Date().getTime();
if (now - this.last >= rate) {
this.next();
} else {
setTimeout(_.bind(this.next, this), rate - now + this.last);
}
};
}
function button(text) {
return $("<button>" + text + "</button>")
.css({"color": "#565656",
"background-color": "#F0F0F0",
"border": "1px solid #D0D0D0",
"border-top": "1px solid #EAEAEA",
"border-left": "1px solid #EAEAEA"});
}
$("body").on("input.rc", ".recording-comment", function () {
$(this).css("border-color",
this.value === $(this).data("old") ? "#999" : "red");
});
var $container = $("<div></div>")
.css({
background: "#F1F1F1",
border: "1px #999 dotted",
display: "inline-block"
})
.insertBefore("table.tbl");
var $edit_button = button("Edit recording comments")
.on("click", function () {
editing = !editing;
$("#rc-table").add($inputs).toggle(editing);
$(this).text((editing ? "Hide" : "Edit") + " recording comments");
})
.appendTo($container);
$container.append('\
<table id="rc-table">\
<tr>\
<td><label for="all-comments">Set all comments to:</label></td>\
<td><input type="text" id="all-comments" style="width: 32em;"></td>\
</tr>\
<tr>\
<td><label for="rc-edit-note">Edit note:</label></td>\
<td><textarea id="rc-edit-note" style="width: 32em;" rows="5"></textarea></td>\
</tr>\
<tr>\
<td></td><td></td>\
</tr>\
</table>');
$("#rc-table").hide();
$("#all-comments").on("input", function () {
$inputs.val(this.value).trigger("input.rc");
});
var edit_requests = new RequestManager(2000);
function parseEditPage(html, mbid, $input) {
$input.css("border-color", "blue");
var $html = $($.parseHTML(html)), comment = $input.val(), data = {};
data["edit-recording.name"] = $html.find("#id-edit-recording\\.name").val();
data["edit-recording.length"] = $html.find("#id-edit-recording\\.length").val();
data["edit-recording.edit_note"] = $("#rc-edit-note").val();
data["edit-recording.as_auto_editor"] = "1";
data["edit-recording.comment"] = comment;
edit_requests.unshift(function () {
$.post("/recording/" + mbid + "/edit", data, function () {
$input.data("old", comment).trigger("input.rc")
.prop("disabled", false);
if (edit_requests.queue.length === 0) {
$submit_button.prop("disabled", false)
.text("Submit changes (marked red)");
}
}).error(function () {
$input.css("border-color", "red").prop("disabled", false);
});
});
}
var $submit_button = button("Submit changes (marked red)")
.on("click", function () {
if (submitting) {
edit_requests.queue = [];
submitting = false;
$(this).text("Submit changes (marked red)");
$inputs.prop("disabled", false).trigger("input.rc");
return;
}
submitting = true;
$(this).text("Submitting...click to cancel!");
$inputs.prop("disabled", true);
var edit_count = 0;
$.each($tracks, function (i, track) {
var $input = $inputs.eq(i), comment = $input.val();
if (comment === $input.data("old")) {
$input.prop("disabled", false);
return;
}
edit_count += 1;
var link = $(track).children("td").eq(nameColumn).find("a[href^=\\/recording\\/]")[0],
mbid = link.href.match(MBID_REGEX)[0];
edit_requests.push(function () {
$.get("/recording/" + mbid + "/edit", function (html) {
parseEditPage(html, mbid, $input);
});
});
});
if (edit_count === 0) {
$(this).prop("disabled", false)
.text("Submit changes (marked red)");
}
})
.appendTo($container.find("td")[5]);
}
@jesus2099
Copy link
Author

THIS DOES NOT WORK ANY MORE since the AutoComplete rewrite
Please wait for bitmap’s fix. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment