derpi_only_show_upvotes

25/06/2023 Replaces images' score with only upvotes, instead of the usual combined upvotes+downvotes. Also hides downvotes. Fetches the scores via API calls to https://derpibooru.org/api/v1/json/search/images?filter_id=56027&q=id:IMAGEID1 || id:IMAGEID2 max 15 per query

Stan na 25-06-2023. Zobacz najnowsza wersja.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

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

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

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.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==UserScript==
// @name       derpi_only_show_upvotes
// @namespace   Violentmonkey Scripts
// @match       https://derpibooru.org/*
// @grant       none
// @version     1.2
// @license     MIT
// @author      Saphkey
// @description 25/06/2023 Replaces images' score with only upvotes, instead of the usual combined upvotes+downvotes. Also hides downvotes. Fetches the scores via API calls to https://derpibooru.org/api/v1/json/search/images?filter_id=56027&q=id:IMAGEID1 || id:IMAGEID2   max 15 per query
// ==/UserScript==


(function() {
	function gatherImageIds() {
		let images = document.getElementsByClassName('score');
		let imageIds = [];
		for (let i = 0; i < images.length; i++) {
			imageIds.push(images[i].getAttribute('data-image-id'));
		}
		return imageIds;
	}

	function hideScores(){
		let scoreEls = document.getElementsByClassName('score');
		for (let i = 0; i < scoreEls.length; i++) {
			scoreEls[i].innerHTML = '?';
		}
	}

	function hideDownvotes() {
		let downvoteEls = document.getElementsByClassName('downvotes');
		for (let i = 0; i < downvoteEls.length; i++) {
			downvoteEls[i].innerHTML = '?';
		}
	}

	async function getUpvotesAndCallDisplaySingleUpvote(imageIds, maxImagesPerReq) {

		function prepareRequestUrls(imagedIds) {
			let requestUrlBase = `https://derpibooru.org/api/v1/json/search/images?filter_id=56027&q=id:${imagedIds[0]}`;
			let requestUrlsArray = [];

			let tempUrl = requestUrlBase;
			for (let i=1; i < imagedIds.length; i++) {
				tempUrl += ` || id:${imagedIds[i]}`;

				if (i / maxImagesPerReq === 1) {
					requestUrlsArray.push(tempUrl);
					tempUrl = requestUrlBase;
				}
			}

			if (tempUrl.length > 0) {
				requestUrlsArray.push(tempUrl);
			}
			return requestUrlsArray;
		}

		async function processRequest(res) {try{
			res = await res.json();
			res.images.forEach((image,i)=>{
				console.log(`imageID: ${image.id} upvotes: ${image.upvotes}`)
				displaySingleUpvote(image.upvotes, image.id);
			});
		}catch(err){
			console.log(err);
		}}

		let requestUrlsArray = prepareRequestUrls(imageIds);
		for (let i = 0; i < requestUrlsArray.length; i++) {
			await fetch(requestUrlsArray[i]).then((response) => {
				processRequest(response);
			})
		}
	}

	//displayAllUpvotes is unused
	function displayAllUpvotes(upvotesObj) {
		let scoreEls = document.getElementsByClassName('score');

		for (let i = 0; i < scoreEls.length; i++) {
			let scoreEl = scoreEls[i];
			let imageId = scoreEl.getAttribute('data-image-id');
			let upvotes = upvotesObj[imageId];
			if (upvotes !== undefined) {
				scoreEl.innerText = upvotes;
			}
			else {
				scoreEl.innerText = '?';
			}
		}
	}


	function displaySingleUpvote(upvotes, imageId) {
		let scoreEls = document.getElementsByClassName('score');

		for (let i = 0; i < scoreEls.length; i++) {
			let scoreEl = scoreEls.item(i);

			if (scoreEl.getAttribute(`data-image-id`) == imageId){
				scoreEl.innerHTML = upvotes;
			}
		}
	}


	window.onload = async function() {
		hideScores();
		hideDownvotes();

		let imageIds = gatherImageIds();
		await getUpvotesAndCallDisplaySingleUpvote(imageIds, 15);
	}
})();