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 individual API calls to https://derpibooru.org/api/v1/json/images/imageNumber

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला 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       derpi_only_show_upvotes
// @namespace   Violentmonkey Scripts
// @match       https://derpibooru.org/*
// @grant       none
// @version     1.0
// @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 individual API calls to https://derpibooru.org/api/v1/json/images/imageNumber
// ==/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 getUpvotesThenCallDisplayUpvotes(imageIds, maxParalelRequests) {
		let requestQ = [];
		let imageUpvotesObj = {};

		async function processRequest(res) {try{
			res = await res.json();
			imageUpvotesObj[res.image.id] = res.image.upvotes;
			console.log(`imageID: ${res.image.id} upvotes: ${res.image.upvotes}`)

			//if (Object.keys(imageUpvotesObj).length === imageIds.length){
			//	displayAllUpvotes(imageUpvotesObj);
			//}
			displaySingleUpvote(res.image.upvotes, res.image.id);
		}catch(err){
			console.log(err);
		}}

		for (let i = 0; i < imageIds.length; i++) {

			if (requestQ.length >= maxParalelRequests) {

				let nextRequest = requestQ.shift()

				await nextRequest.then((response) => {
					processRequest(response);
				})

			}

			requestQ.push(fetch(`https://derpibooru.org/api/v1/json/images/${imageIds[i]}`))
		}

		let nextRequest = requestQ[0];
		while(nextRequest = requestQ.shift()) {

			await nextRequest.then((response) => {
				processRequest(response);
			})
		}
	}


	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 getUpvotesThenCallDisplayUpvotes(imageIds, 8);
	}
})();