Danbooru Post Link Rating

Finds links to posts on Danbooru and appends that post's rating to the end

As of 17.08.2016. See апошняя версія.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name          Danbooru Post Link Rating
// @namespace     DoomTay
// @description   Finds links to posts on Danbooru and appends that post's rating to the end
// @include       http://danbooru.donmai.us/*
// @include       https://danbooru.donmai.us/*
// @version       1.2.0

// ==/UserScript==

var links = document.links;

for(var l = 0; l < links.length; l++)
{
	if(links[l].firstChild.nodeName == "IMG") continue;
	if(document.getElementById("pool-nav") && document.getElementById("pool-nav").contains(links[l])) continue;
	if(links[l].href.includes("/posts/") && /\/posts\/(\d+)/.test(links[l].href))
	{
		var postID = /\/posts\/(\d+)/.exec(links[l].href)[1];
	}
	else if(links[l].href.includes("/post/show/") && /\/post\/show\/(\d+)(?:\/)?/.test(links[l].href))
	{
		var postID = /\/post\/show\/(\d+)(?:\/)?/.exec(links[l].href)[1];
	}
	else if(links[l].href.includes("/pools/") && /\/pools\/(\d+)/.test(links[l].href))
	{
		var poolID = /\/pools\/(\d+)/.exec(links[l].href)[1];
		if(links[l].href.includes("?page=")) continue;
		if(links[l].href.includes("/edit")) continue;
		collectPool(links[l], poolID);
		continue;
	}
	else continue;
	if(window.location.href.includes(postID)) continue;
	if(!isNaN(postID)) appendPostRating(links[l],postID);
}

function getJSON(url)
{
	var promise = new Promise(function(resolve, reject) {
		var request = new XMLHttpRequest();
		request.onload = function () {
			if(this.status == 200) resolve(this.response);
			else if(this.status == 421) reject("User Throttled");
			else reject(this.statusText);
		}
		request.onerror = function () {
			reject("Connection Failed");
		}
		request.open("GET", url, true);
		request.responseType = "json";
		request.send();
	});
	return promise;
}

function appendPostRating(link,id)
{
	getJSON("/posts/" + id + ".json").then(data =>
	{
		var rating = data.rating;
		var ratingReading = document.createElement("span");
		ratingReading.innerHTML = " (rating: " + rating + ")";
		link.parentNode.insertBefore(ratingReading, link.nextSibling);
	})
}

function collectPool(link, id)
{
	var ratingReading = document.createElement("span");
	ratingReading.innerHTML = " (loading...)";
	link.parentNode.insertBefore(ratingReading, link.nextSibling);
	
	getJSON("/pools/" + id + ".json").then(data =>
	{
		var pages = Math.ceil((data.post_count)/100);
		var searchCount = [];
		for(var c = 0; c < pages; c++)
		{
			searchCount[c] = getJSON("/posts.json?tags=pool:" + id + "&page=" + (c + 1) + "&limit=100");
		}
		Promise.all(searchCount).then((data) => {
			var ratingCount = [0,0,0];
			for(var d = 0; d < data.length; d++)
			{
				for(var p = 0; p < data[d].length; p++)
				{
					switch (data[d][p].rating)
					{
						case "s":
							ratingCount[0]++;
							break;
						case "q":
							ratingCount[1]++;
							break;
						case "e":
							ratingCount[2]++;
							break;
					}
				}
			}
			var total = ratingCount[0] + ratingCount[1] + ratingCount[2];
			ratingReading.innerHTML = " (" + FractionToPercentage(ratingCount[0],total) + " s, " + FractionToPercentage(ratingCount[1],total) + " q, " + FractionToPercentage(ratingCount[2],total) + " e)";
		},reason => {
			ratingReading.innerHTML = " (" + reason + ")";
		});
	},reason => {
		ratingReading.innerHTML = " (" + reason + ")";
	});
}

function FractionToPercentage(count, total)
{
	return Math.round((count / total) * 100) + "%";
}