MB. artist / label pending edits

By jesus2099 Last update Dec 15, 2011 — Installed 605 times.

There are 25 previous versions of this script.

// ==UserScript==
// @name           mb: Artist / label pending edits
// @description    Adds a link to view artist/label pending edits, if any in all env. (classic.|test.)musicbrainz.org
// @version        2011-12-15_1228
// @author         Tristan DANIEL (jesus2099)
// @contact        http://miaou.ions.fr
// @licence        GPL (http://www.gnu.org/copyleft/gpl.html)
// @namespace      http://userscripts.org/scripts/show/42102
//
// @include        http://*musicbrainz.org/artist/*
// @include        http://*musicbrainz.org/show/artist/*
// @include        http://*musicbrainz.org/release-group/*
// @include        http://*musicbrainz.org/show/release-group/*
// @include        http://*musicbrainz.org/release/*
// @include        http://*musicbrainz.org/show/release/*
// @include        http://*musicbrainz.org/album/*
// @include        http://*musicbrainz.org/show/album/*
// @include        http://*musicbrainz.org/recording/*
// @include        http://*musicbrainz.org/work/*
// @include        http://*musicbrainz.org/label/*
// @include        http://*musicbrainz.org/url/*/relationships
//
// @exclude        http://*musicbrainz.org/*edit*
// ==/UserScript==

(function () {

/*  QUICKLY FIXED FOR NGS/TEST/CLASSIC basic compatibility
 - --- - --- - --- - START OF CONFIGURATION - --- - --- - --- -
orderby: either "asc"ending or "desc"ending edit display
alwaysdisplaylink: always display link, even if no pending edits are found, can reduce page jumps after delayed ajax pending edit count.
	txt: default link text (alwaysdisplaylink=true) to display before we know the edit count : "pending edits" / "P" / ...
	txt0: link text (alwaysdisplaylink=true) when no pending edits: "no pending edits" / "0P" / ... 
txt1: link text when 1 pending edit: "1 pending edit" / "1P" / ...
txtn: link text when 2 or more pending edits (%s will be replace by edit count): "%s pending edits" / "%sP" / ... 
releasetracklinks: activate release and tracks pending edit links
smalltxt: used for track and release open edits (no numbering) */
var orderby = "desc";
var alwaysdisplaylink = true;
	var txt = "pending edits \u231B";
	var txt0 = "no pending edits";
var txt1 = "1 pending edit";
var txtn = "%s pending edits";
var releasetracklinks = true;
	var smalltxt = "pending\u2026";
/* - --- - --- - --- - END OF CONFIGURATION - --- - --- - --- - */

var classic = self.location.href.match(/\/classic\./i);
var ngslabel = self.location.href.match(/\/label\//i);
var content = document.getElementById(classic?"content-td":"page");
var artistedits = null;
var entity;

if (!classic) {
	var sb = document.getElementById("sidebar");
	entity = getEntity();
	if (entity) {
		var aep = document.createElement("p");
		var aea = document.createElement("a");
		aea.setAttribute("href", (ngslabel?"/label/":"/artist/")+entity[0]+"/edits");
		aea.appendChild(document.createTextNode("View "+entity[1]+" edits"));
		aep.appendChild(aea);
		sb.insertBefore(aep, sb.firstChild);
		artistedits = aea;
	} else { artistedits = null; }
} else {
	var cont = getElementsByClassName(content, "td", "links")[0];
	artistedits = cont.getElementsByTagName("a");
	for (var iae=0; iae < artistedits.length; iae++) {
		if (artistedits[iae].firstChild.nodeValue.match(/View artist edits/)) {
			artistedits = artistedits[iae];
			break;
		}
	}
}

var RGurl = "/mod/search/results.html?orderby="+orderby+"&object_type=release_group&mod_status=1&mod_status=8&object_id=";
var Rurl = "/mod/search/results.html?orderby="+orderby+"&object_type=album&mod_status=1&mod_status=8&object_id=";
var Turl = "/mod/search/results.html?orderby="+orderby+"&object_type=track&mod_status=1&mod_status=8&object_id=";
var noeditsfound = new RegExp("no edits found","i");
var editcount = new RegExp("found (.+) edit","i");

if (artistedits != null && !artistedits.length) {
	var url = classic?"/mod/search/results.html?orderby="+orderby+"&artist_type=3&mod_status=1&mod_status=8&artist_id=" + getEntityId("artist"):(ngslabel?"/label/":"/artist/")+entity[0]+"/open_edits";
	var pelink = null;
	if (alwaysdisplaylink) { pelink = create_pelink(-1); }

	/* pending edit search */
	var xmlhttp = new XMLHttpRequest();
	xmlhttp.onreadystatechange = xmlhttpChange;
	xmlhttp.open("GET", url, true);
	xmlhttp.send(null);
}

if (releasetracklinks) {
	/*ReleaseGroup DISABLED: WOULD REQUIRE ANOTHER AJAX REQUEST
	var pRGfound = document.evaluate("//table[@class='releasebegin']//span[@class='modpending']", content, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
	for (var irgs=0; irgs < pRGfound.snapshotLength; irgs++) {
		var theRG = pRGfound.snapshotItem(irgs);
	}*/
	/*Release*/
	var pRfound = document.evaluate("//table[@class='releasebegin']//tr[@class='title mp']", content, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
	for (var irs=0; irs < pRfound.snapshotLength; irs++) {
		var theR = pRfound.snapshotItem(irs);
		var releaseid = theR.getElementsByTagName("a")[0].getAttribute("id").match(".+::([0-9]+)")[1];
		var fragm = document.createDocumentFragment();
		fragm.appendChild(document.createTextNode(" ("));
		fragm.appendChild(smallpelink(smalltxt.toLowerCase(), Rurl+releaseid));
		fragm.appendChild(document.createTextNode(") "));
		theR.getElementsByTagName("td")[1].insertBefore(fragm, theR.parentNode.getElementsByTagName("a")[0].nextSibling);
	}
	/*Tracks*/
	var pTfound = document.evaluate("//table[@class='releasetracks']//td[contains(@class,'title mp')]", content, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
	for (var its=0; its < pTfound.snapshotLength; its++) {
		var theT = pTfound.snapshotItem(its);
		var trackid = theT.parentNode.getElementsByTagName("a");
		trackid = trackid[trackid.length-1];
		trackid = trackid.getAttribute("href").match("trackid=([0-9]+)")[1];
		var trackLinksCell = getElementsByClassName(theT.parentNode, "td", "links")[0];
		var fragm = document.createDocumentFragment();
		fragm.appendChild(smallpelink(smalltxt.charAt(0).toUpperCase()+smalltxt.slice(1), Turl+trackid+"&trackid="+trackid));
		fragm.appendChild(document.createTextNode(" | "));
		trackLinksCell.insertBefore(fragm, trackLinksCell.firstChild);
	}
}

function xmlhttpChange() {
	if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
		var res = xmlhttp.responseText;
		var editc = res.match(editcount);
		if (!editc || res.match(noeditsfound)) {
			editc = 0;
		} else {
			editc = parseInt(editc[1]);
		}
		if (alwaysdisplaylink || editc > 0) {
			pelink = create_pelink(editc);
		}
	}
}

function smallpelink(ptxt, purl) {
	var cont = document.createElement("span");
	cont.className = "mp";
	var a = document.createElement("a");
	a.setAttribute("href", purl);
	a.appendChild(document.createTextNode(ptxt));
	cont.appendChild(a);
	return cont;
}

function create_pelink(pecount) {
	var lbl;
	switch (pecount) {
		case -1:
			lbl = txt;
			break;
		case 0:
			lbl = txt0;
			break;
		case 1:
			lbl = txt1;
			break;
		default:
			if (!classic && pecount == 500) {
				lbl = txtn.replace("%s", "500+");
			} else {
				lbl = txtn.replace("%s", pecount);
			}
	}

	var a = document.createElement("a");
	a.setAttribute("href", url);
	a.appendChild(document.createTextNode(lbl));

	var oedits = document.createElement("span");
	oedits.appendChild(a);
	if (pecount > 0) {
		oedits.className = "mp";
	}

	if (pelink) {
		pelink.parentNode.replaceChild(oedits, pelink);
	} else {
		var fragment= document.createDocumentFragment();
		if (!classic) { fragment.appendChild(document.createElement("br")); }
		fragment.appendChild(document.createTextNode(" ("));
		fragment.appendChild(oedits);
		fragment.appendChild(document.createTextNode(") "));
		artistedits.parentNode.insertBefore(fragment, artistedits.nextSibling);
	}
	return(oedits);
}

/* Obtain the artistId of the current page (murdos) */
function getEntityId(subscriptionType) {
	var artistbox = getElementsByClassName(content, "td", "links")[0];
	var link = artistbox.getElementsByTagName("a")[1];
	var re = new RegExp('details.html\\?' + subscriptionType + 'id=(.+)');
	return re.exec(link.href)[1];
}
function getEntity() {
	var as = content.getElementsByTagName("a");
	for (var ias=0; ias < as.length; ias++) {
		var ent = as[ias].getAttribute("href");
		if (ent) {
			if (ngslabel) { ent = ent.match(/\/label\/([a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12})$/i); }
			else { ent = ent.match(/\/artist\/([a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12})$/i); }
			if (ent) {
				return [ent[1], as[ias].firstChild.nodeValue];
			}
		}
	}
	return null;
}

/* Helper function for getting html element by class name
Written by Jonathan Snook, http://www.snook.ca/jonathan
Add-ons by Robert Nyman, http://www.robertnyman.com */
function getElementsByClassName(oElm, strTagName, strClassName){
    var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    strClassName = strClassName.replace(/\-/g, "\\-");
    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
    var oElement;
    for(var i=0; i<arrElements.length; i++){
        oElement = arrElements[i];      
        if(oRegExp.test(oElement.className)){
            arrReturnElements.push(oElement);
        }   
    }
    return (arrReturnElements);
}

})();