// ==UserScript==
// @name Greasy Fork - Analyze from posted scripts
// @namespace ScriptAnalyzer
// @version 13
// @description Shows the total amount for each rating, total/daily installs, and scripts posted on any user profile and search pages.
// @author hacker09
// @match https://greasyfork.org/*/users/*
// @match https://greasyfork.org/*/scripts?q=*
// @match https://greasyfork.org/*/scripts/by-site/*
// @icon https://t2.gstatic.com/faviconV2?client=SOCIAL&type=FAVICON&fallback_opts=TYPE,SIZE,URL&url=https://greasyfork.org/&size=64
// @grant none
// @downloadURL https://update.greasyfork.org/scripts/468184/Greasy%20Fork%20-%20Analyze%20from%20posted%20scripts.user.js
// @updateURL https://update.greasyfork.org/scripts/468184/Greasy%20Fork%20-%20Analyze%20from%20posted%20scripts.meta.js
// ==/UserScript==
(async function() {
'use strict';
const data = await (await fetch(document.querySelector('link[href$=".json"]').href)).json(); //Fetch and parse the response
var LatestCreated, LatestUpdated, ok, bad, good, DailyTotal, TotalInstalls, element, SignedIN, ScriptsArray = data.scripts; //Create new variables
SignedIN = document.querySelector('.user-profile-link a')?.href.match(/\d+/)[0] === String(data.id) ? ' + Deleted + Unlisted + Libraries' : ''; //If the user is on their own profile page
element = location.href.match(/org\/.*\/scripts/) ? ".width-constraint:nth-child(2)" : "#user-script-sets-section, #user-script-list-section"; //If the current page is a script search page
LatestCreated = new Date(Math.max(...ScriptsArray.map(obj => new Date(obj.created_at)))); //Get the latest created script
LatestUpdated = new Date(Math.max(...ScriptsArray.map(obj => new Date(obj.code_updated_at)))); //Get the latest updated script
ok = ScriptsArray.map(obj => obj.ok_ratings).reduce((acc, curr) => acc + curr, 0); //Sum the total amount of ok ratings
bad = ScriptsArray.map(obj => obj.bad_ratings).reduce((acc, curr) => acc + curr, 0); //Sum the total amount of bad ratings
good = ScriptsArray.map(obj => obj.good_ratings).reduce((acc, curr) => acc + curr, 0); //Sum the total amount of good ratings
DailyTotal = ScriptsArray.map(obj => obj.daily_installs).reduce((acc, curr) => acc + curr, 0); //Sum the total amount of daily total installs
TotalInstalls = ScriptsArray.map(obj => obj.total_installs).reduce((acc, curr) => acc + curr, 0); //Sum the total amount of total installs
document.querySelector(element).insertAdjacentHTML("afterbegin", ` - Script posts${SignedIN}: ${ScriptsArray.length}
- Daily installs: ${DailyTotal.toLocaleString()}
- Total installs: ${TotalInstalls.toLocaleString()}
- Total ok ratings: ${ok.toLocaleString()}
- Total bad ratings: ${bad.toLocaleString()}
- Total good ratings: ${good.toLocaleString()}
- Latest created: ${LatestCreated}
- Latest updated: ${LatestUpdated}
`); //Add the information on the page
document.querySelector(".list-option").innerHTML += `(${DailyTotal.toLocaleString()})`; //Add the Dailytotal number on the sidebar
document.querySelector(".list-option:nth-child(2)").innerHTML += `(${TotalInstalls.toLocaleString()})`; //Add the total number on the sidebar
document.querySelector(".list-option:nth-child(3)").innerHTML += `(${ok+bad+good.toLocaleString()})`; //Add the ratings number on the sidebar
})();