Danbooru Post Link Rating

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

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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.2.3
// @license       GPL-3.0

// ==/UserScript==

var ratePools = false;

var links = document.links;

$(links).each(function(l)
{
	var postID;

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

function getJSON(url)
{
	var promise = new Promise(function(resolve, reject) {
		$.getJSON(url,resolve).fail(function(response)
		{
			if(response.status == 421) reject("User Throttled");
			else if(response.status == 429) reject("Too Many Requests");
			else if(response.status == 502) setTimeout(function(){resolve(getJSON(url))},2000);
			else reject(response.statusText);
		});
	});
	return promise;
}

function appendPostRating(link,id)
{
	getJSON("/posts/" + id + ".json").then(data =>
	{
		link.after(" (rating: " + data.rating + ")");
	}).catch(reason => {
		link.after(" (" + reason + ")");
	});
}

function collectPool(link, id)
{

	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");
		}
		return Promise.all(searchCount).then(data => [].concat.apply([], data));
	}).then(allPosts => {
		var gCount = allPosts.filter(post => post.rating == "g").length;
		var sCount = allPosts.filter(post => post.rating == "s").length;
		var qCount = allPosts.filter(post => post.rating == "q").length;
		var eCount = allPosts.filter(post => post.rating == "e").length;

		var total = allPosts.length;

		link.after(" (" + FractionToPercentage(gCount,total) + " g, " + FractionToPercentage(sCount,total) + " s, " + FractionToPercentage(qCount,total) + " q, " + FractionToPercentage(eCount,total) + " e)");
	}).catch(reason => {
		link.after(" (" + reason + ")");
	});
}

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