Fav4Pawchive

Consistent Favorite Button for Pawchive Creator Pages

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

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

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

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

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

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

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

Advertisement:

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

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

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

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

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

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

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

Advertisement:

// ==UserScript==
// @name         Fav4Pawchive
// @version      1
// @match        *://pawchive.pw/*
// @grant        GM_addStyle
// @run-at       document-end
// @description  Consistent Favorite Button for Pawchive Creator Pages
// @namespace    c0032
// @license      ohforfuckssakeidontcare
// ==/UserScript==

(function () {
    'use strict';

    GM_addStyle(`
        .restored-fav-btn {
            display: none !important;
            align-items: center;
            gap: 3px;
            padding: 0;
            margin: 0;
            background: #ff4081;
            color: white;
            border: none;
            border-radius: 3px;
            font-size: 14px;
            font-weight: 500;
            cursor: pointer;
            transition: all 0.2s;
            position: absolute;
            left: 50%;
            top: 0;
            transform: translateX(-50%);
            z-index: 99999;
        }
        .restored-fav-btn.visible { display: inline-flex !important; }
        .restored-fav-btn:hover { background: #f50057; transform: translateX(-50%) scale(1.05); }
        .restored-fav-btn.faved { background: #4caf50; }
        .restored-fav-btn.faved:hover { background: #388e3c; }
    `);

    const state = {
        service: null,
        id: null,
        isFaved: false,
        isFetching: false
    };

    const btn = document.createElement('button');
    btn.className = 'restored-fav-btn';
    document.body.appendChild(btn);

    btn.addEventListener('click', async () => {
        if (!state.service || !state.id || state.isFetching) return;

        const method = state.isFaved ? 'DELETE' : 'POST';
        const url = `/api/v1/favorites/creator/${state.service}/${state.id}`;
        state.isFetching = true;

        try {
            const res = await fetch(url, {
                method,
                credentials: 'include',
                headers: { 'Content-Type': 'application/json' }
            });

            if (res.ok || res.status === 204) {
                state.isFaved = !state.isFaved;
                render();
            } else {
                alert(`Failed (${res.status}). Are you logged in?`);
            }
        } catch (e) {
            console.error('Toggle failed', e);
        } finally {
            state.isFetching = false;
        }
    });

    function render() {
        if (!state.service || !state.id) {
            btn.classList.remove('visible');
            return;
        }
        btn.classList.add('visible');
        btn.textContent = state.isFaved ? '★ Unfavorite' : '☆ Favorite';
        btn.classList.toggle('faved', state.isFaved);
    }

    async function checkStatus() {
        if (!state.service || !state.id) return;

        const currentKey = `${state.service}/${state.id}`;
        state.isFetching = true;

        try {
            const res = await fetch(`/api/v1/account/favorites?type=artist`, { credentials: 'include' });
            if (!res.ok) throw new Error();
            const data = await res.json();

            if (currentKey !== `${state.service}/${state.id}`) return;

            state.isFaved = data.some(item =>
                item.service === state.service &&
                (String(item.id) === state.id || String(item.relation_id) === state.id)
            );
            render();
        } catch (e) {
            console.warn('Check status failed', e);
        } finally {
            state.isFetching = false;
        }
    }

    function handleNavigation() {
        const match = window.location.pathname.match(/^\/([^/]+)\/user\/([^/]+)/);
        const newService = match ? match[1] : null;
        const newId = match ? match[2] : null;

        if (state.service !== newService || state.id !== newId) {
            state.service = newService;
            state.id = newId;
            state.isFaved = false;

            render();
            checkStatus();
        } else if (!document.body.contains(btn)) {
            document.body.appendChild(btn);
        }
    }

    const originalPushState = history.pushState;
    history.pushState = function() {
        originalPushState.apply(this, arguments);
        handleNavigation();
    };

    const originalReplaceState = history.replaceState;
    history.replaceState = function() {
        originalReplaceState.apply(this, arguments);
        handleNavigation();
    };

    window.addEventListener('popstate', handleNavigation);

    handleNavigation();
})();