Danbooru Post Link Rating

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

2016-06-10 या दिनांकाला. सर्वात नवीन आवृत्ती पाहा.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==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.1.1

// ==/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 appendPostRating(link,id)
{
	var post = new XMLHttpRequest();
	post.onload = function () {
		if(post.status == 200)
		{
			var rating = post.response.rating;
			var ratingReading = document.createElement("span");
			ratingReading.innerHTML = " (rating: " + rating + ")";
			link.parentNode.insertBefore(ratingReading, link.nextSibling);
		}
	}
	post.open("GET", "/posts/" + id + ".json", true);
	post.responseType = "json";
	post.send();
}

function collectPool(link, id)
{
	var postCount = new XMLHttpRequest();
		postCount.onload = function () {
			if(postCount.status == 200)
			{
				var pages = Math.ceil((postCount.response.post_count)/100);
				var ratingReading = document.createElement("span");
				ratingReading.innerHTML = " (loading...)";
				link.parentNode.insertBefore(ratingReading, link.nextSibling);
				var ratingCount = [0,0,0];
				var searchCount = [];
				for(var c = 0; c < pages; c++)
				{
					searchCount[c] = collectPosts(id,c + 1).then(function(response) {
						for(var p = 0; p < response.length; p++)
						{
							switch (response[p].rating)
							{
								case "s":
									ratingCount[0]++;
									break;
								case "q":
									ratingCount[1]++;
									break;
								case "e":
									ratingCount[2]++;
									break;
							}
						}
					});
				}
				Promise.all(searchCount).then(function() {
					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)";
				},function(reason) {
					  ratingReading.innerHTML = " (" + reason + ")";
					});
			}
		}
		postCount.open("GET", "/pools/" + id + ".json", true);
		postCount.responseType = "json";
		postCount.send();
}

function collectPosts(pool,page)
{
	var promise = new Promise(function(resolve, reject) {
		var search = new XMLHttpRequest();
		search.onload = function () {
			if(search.status == 200) resolve(search.response);
			else if(search.status == 421) reject("User Throttled");
			else reject(search.statusText);
		}
		search.onerror = function () {
			reject("Connection failed");
		}
		search.open("GET", "/posts.json?tags=pool:" + pool + "&page=" + page + "&limit=100", true);
		search.responseType = "json";
		search.send();
	});
	return promise;
}

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