EroMe - Sort by Views Link (Inside Page Container)

Adds a visible "Sort by Views" link just inside the #page container on EroMe profile pages to sort albums by view count descendingly.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         EroMe - Sort by Views Link (Inside Page Container)
// @namespace    http://tampermonkey.net/
// @version      1.6
// @description  Adds a visible "Sort by Views" link just inside the #page container on EroMe profile pages to sort albums by view count descendingly.
// @author       ChatGPT
// @match        https://www.erome.com/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function () {
    'use strict';

    function parseViews(text) {
        if (!text) return 0;
        let cleaned = text.replace(/[^0-9,.KM]/g, '').replace(',', '.');
        if (cleaned.includes('K')) return parseFloat(cleaned) * 1000;
        if (cleaned.includes('M')) return parseFloat(cleaned) * 1000000;
        return parseFloat(cleaned);
    }

    function sortAlbumsByViews() {
        const container = document.querySelector('#albums');
        if (!container) return;

        const albumNodes = Array.from(document.querySelectorAll('.album'));
        const sorted = albumNodes.sort((a, b) => {
            const viewsA = parseViews(a.querySelector('.album-bottom-views')?.innerText);
            const viewsB = parseViews(b.querySelector('.album-bottom-views')?.innerText);
            return viewsB - viewsA;
        });

        sorted.forEach(album => container.appendChild(album));
        console.log('Albums sorted by most views');
    }

    function restoreOriginalOrder() {
        location.reload();
    }

    function insertSortLink() {
        const pageContainer = document.querySelector('#page');
        if (!pageContainer || document.querySelector('#sortViewsLink')) return;

        const sortLink = document.createElement('a');
        sortLink.href = '#';
        sortLink.textContent = '🔽 Sort by Views';
        sortLink.id = 'sortViewsLink';
        sortLink.style.display = 'inline-block';
        sortLink.style.margin = '20px 0';
        sortLink.style.fontSize = '16px';
        sortLink.style.fontWeight = 'bold';
        sortLink.style.color = '#e91e63';

        let sorted = false;

        sortLink.addEventListener('click', (e) => {
            e.preventDefault();
            if (!sorted) {
                sortAlbumsByViews();
                sortLink.textContent = '↩ Unsort';
            } else {
                restoreOriginalOrder();
            }
            sorted = !sorted;
        });

        pageContainer.insertBefore(sortLink, pageContainer.firstChild);
    }

    const waitForElement = (selector, callback, timeout = 10000) => {
        const start = Date.now();
        const check = () => {
            const el = document.querySelector(selector);
            if (el) callback(el);
            else if (Date.now() - start < timeout) requestAnimationFrame(check);
        };
        check();
    };

    waitForElement('#page', insertSortLink);
})();