Greasy Fork Total Downloads

Shows a user's total downloads.

目前為 2024-07-11 提交的版本,檢視 最新版本

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

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

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name Greasy Fork Total Downloads
// @namespace -
// @version 1.1.0
// @description Shows a user's total downloads.
// @author NotYou
// @include *://greasyfork.org/*/users/*
// @include *://sleazyfork.org/*/users/*
// @match *://greasyfork.org/*/users/*
// @match *://sleazyfork.org/*/users/*
// @license GPL-3.0
// @run-at document-body
// @grant GM_xmlhttpRequest
// @require https://update.greasyfork.org/scripts/445697/1244619/Greasy%20Fork%20API.js
// ==/UserScript==

~function(GreasyFork) {
    class Installs {
        static async getData(userId) {
            const userData = await GreasyFork.getUserData(userId)

            return userData.all_listable_scripts.reduce((acc, curr) => {
                acc.total += curr.total_installs
                acc.daily += curr.daily_installs

                return acc
            }, {
                total: 0,
                daily: 0
            })
        }
    }

    class User {
        static getId(url) {
            const match = url.match(/https?:\/\/(greasyfork|sleazyfork)\.org\/[a-zA-Z]+(\-[a-zA-Z]+)?\/users\/(?<userId>\d+)/)

            if (match) {
                const { userId } = match.groups

                return userId
            } else {
                return '1'
            }
        }

        static getLocale(languageSelectorLocale) {
            return languageSelectorLocale ? languageSelectorLocale.value || 'en-US' : 'en-US'
        }
    }

    class Stat {
        static createItem(data, locale, text, color) {
            const node = document.createElement('span')
            node.style.fontSize = '15px'
            node.style.borderRadius = '3px'
            node.style.backgroundColor = 'rgb(45, 45, 45)'
            node.style.color = 'rgb(255, 255, 255)'
            node.style.margin = '0 4px'
            node.style.padding = '0 4px'
            node.style.display = 'inline-flex'
            node.style.alignItems = 'center'
            node.style.gap = '4px'
            node.style.outline = `2px solid ${color}`
            node.textContent = data.toLocaleString(locale) + ' ' + text

            const circle = document.createElement('span')
            circle.style.width = '8px'
            circle.style.height = '8px'
            circle.style.borderRadius = '50%'
            circle.style.background = color

            node.prepend(circle)

            return node
        }
    }

    class Main {
        static init() {
            window.addEventListener('DOMContentLoaded', async () => {
                const { href } = location
                const $languageSelectorLocale = document.querySelector('#language-selector-locale')
                const userId = User.getId(href)
                const userLocale = User.getLocale($languageSelectorLocale)
                const installsData = await Installs.getData(userId)
                const { total, daily } = installsData
                const $total = Stat.createItem(total, userLocale, 'Installs', 'rgb(123, 23, 23)')
                const $daily = Stat.createItem(daily, userLocale, 'Daily Installs', 'rgb(185, 32, 32)')
                const $header = document.querySelector('div.sidebarred-main-content h3:first-child')

                $header.appendChild($total)
                $header.appendChild($daily)
            })
        }
    }

    Main.init()
}(GreasyFork)