Greasyfork/Sleazyfork Enhanced Stats Display

Displays total number of scripts, installs, and version numbers for users on Greasyfork/Sleazyfork

Fra og med 24.11.2024. Se den nyeste version.

// ==UserScript==
// @name         Greasyfork/Sleazyfork Enhanced Stats Display
// @description  Displays total number of scripts, installs, and version numbers for users on Greasyfork/Sleazyfork
// @icon         https://greasyfork.org/vite/assets/blacklogo96-CxYTSM_T.png
// @version      1.1
// @author       afkarxyz
// @namespace    https://github.com/afkarxyz/misc-scripts/
// @supportURL   https://github.com/afkarxyz/misc-scripts/issues
// @license      MIT
// @match        https://greasyfork.org/*/users/*
// @match        https://greasyfork.org/users/*
// @match        https://sleazyfork.org/*/users/*
// @match        https://sleazyfork.org/users/*
// @match        https://greasyfork.org/*/scripts*
// @match        https://greasyfork.org/scripts*
// @match        https://sleazyfork.org/*/scripts*
// @match        https://sleazyfork.org/scripts*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    
    function appendVersionNumbers() {
        const listItems = document.querySelectorAll('li[data-script-id]');
        if (!listItems || listItems.length === 0) return;
        
        listItems.forEach(listItem => {
            const version = listItem.getAttribute('data-script-version');
            if (!version) return;
            
            const separator = listItem.querySelector('.name-description-separator');
            if (!separator) return;
            
            const existingVersion = separator.previousElementSibling;
            if (existingVersion && existingVersion.classList.contains('script-version')) return;
            
            const versionSpan = document.createElement('span');
            versionSpan.textContent = ` v${version} `;
            versionSpan.style.color = '#000000';
            versionSpan.style.fontWeight = 'bold';
            versionSpan.classList.add('script-version');
            
            separator.parentNode.insertBefore(versionSpan, separator);
        });
    }

    function displayVersionNumbers() {
        const headings = document.querySelectorAll('h2 a.script-link');
        headings.forEach(heading => {
            const version = heading.closest('li').getAttribute('data-script-version');
            const versionSpan = document.createElement('span');
            versionSpan.textContent = ` v${version}`;
            versionSpan.style.color = '#000';
            heading.insertAdjacentElement('afterend', versionSpan);
        });
    }

    function displayUserStats() {
        const parseInstallCount = text => parseInt(text.replace(/,/g, '')) || 0;
        
        const scripts = [...document.querySelectorAll('.script-list-total-installs span')]
            .filter(element => !element.textContent.includes('Total installs'));
        
        if (scripts.length === 0) return;
        
        const totalInstalls = scripts.reduce((sum, element) => 
            sum + parseInstallCount(element.textContent), 0);
        
        const statsData = {
            scriptsCount: scripts.length,
            totalInstalls: totalInstalls
        };
        
        const statsElement = document.createElement('section');
        statsElement.id = 'user-stats';
        
        const userDiscussionsElement = document.getElementById('user-discussions');
        if (!userDiscussionsElement) return;
        
        const stylesToCopy = [
            'padding',
            'border',
            'borderRadius',
            'backgroundColor',
            'color',
            'fontSize',
            'fontFamily',
            'lineHeight'
        ];
        
        const computedStyle = window.getComputedStyle(userDiscussionsElement);
        stylesToCopy.forEach(property => {
            statsElement.style[property] = computedStyle.getPropertyValue(property);
        });
        
        const header = document.createElement('header');
        const headerStyle = window.getComputedStyle(userDiscussionsElement.querySelector('header'));
        header.style.padding = headerStyle.padding;
        header.style.borderBottom = headerStyle.borderBottom;
        
        const h3 = document.createElement('h3');
        h3.textContent = 'Stats';
        const originalH3Style = window.getComputedStyle(userDiscussionsElement.querySelector('h3'));
        h3.style.margin = originalH3Style.margin;
        h3.style.fontSize = originalH3Style.fontSize;
        header.appendChild(h3);
        
        const contentSection = document.createElement('section');
        contentSection.className = 'text-content';
        const originalContentStyle = window.getComputedStyle(userDiscussionsElement.querySelector('.text-content'));
        contentSection.style.padding = originalContentStyle.padding;
        
        const p = document.createElement('p');
        p.innerHTML = `This user has <strong>${statsData.scriptsCount}</strong> script${statsData.scriptsCount !== 1 ? 's' : ''} with <strong>${statsData.totalInstalls.toLocaleString()}</strong> total install${statsData.totalInstalls !== 1 ? 's' : ''}.`;
        contentSection.appendChild(p);
        
        statsElement.appendChild(header);
        statsElement.appendChild(contentSection);
        
        userDiscussionsElement.parentNode.insertBefore(statsElement, userDiscussionsElement.nextSibling);
    }

    function init() {
        const currentPath = window.location.pathname;
        if (currentPath.includes('/scripts')) {
            appendVersionNumbers();
        } else if (currentPath.includes('/users/')) {
            displayUserStats();
            displayVersionNumbers();
        }
    }

    window.addEventListener('load', init);
    
    let lastUrl = location.href;
    const scriptListObserver = new MutationObserver((mutations) => {
        const url = location.href;
        if (url !== lastUrl) {
            lastUrl = url;
        }
        
        const hasRelevantChanges = mutations.some(mutation => 
            [...mutation.addedNodes].some(node => 
                node.nodeType === 1 &&
                (node.matches?.('li[data-script-id]') || node.querySelector?.('li[data-script-id]'))
            )
        );
        
        if (hasRelevantChanges) {
            init();
        }
    });

    function startObserver() {
        const observeTarget = document.querySelector('#browse-script-list, .script-list');
        if (observeTarget) {
            scriptListObserver.observe(observeTarget, {
                childList: true,
                subtree: true
            });
        } else {
            requestAnimationFrame(startObserver);
        }
    }

    startObserver();
})();