ppixiv-kemono

Add Kemono Party buttons for ppixiv

目前为 2023-09-15 提交的版本。查看 最新版本

// ==UserScript==
// @name         ppixiv-kemono
// @namespace    https://www.pixiv.net/
// @version      1.1.2
// @description  Add Kemono Party buttons for ppixiv
// @author       EnergoStalin
// @match        https://*.pixiv.net/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=pixiv.net
// @license      GPL3
// @grant        GM_xmlhttpRequest
// @connect      www.patreon.com
// ==/UserScript==

(function() {
    'use strict';

    const cachedPatreonUsers = {};
    const patreonIdRegex = /"id":\s*"(\d+)",\n\s+"type":\s*"user"/sm;

    function ripPatreonId(link) {
        return new Promise((res,rej) => {
            GM_xmlhttpRequest({
                method: "GET",
                url: link,
                onload: function(response) {
                    res(response.responseText.match(patreonIdRegex)[1])
                },
                onerror: rej,
                onabort: rej,
            });
        });
    }

    function fanbox({extraLinks, userInfo}) {
        extraLinks.push({
            url: new URL(`https://kemono.party/fanbox/user/${userInfo.userId}`),
            icon: "mat:money_off",
            type: "kemono_fanbox",
            label: "Kemono fanbox"
        });
    }

    function patreon({link, extraLinks, userInfo}) {
        const url = link.url.toString();
        const cachedId = cachedPatreonUsers[url];
        if(!cachedId) {
            ripPatreonId(url).then(id => {
                cachedPatreonUsers[url] = id;
                unsafeWindow.ppixiv.userCache.callUserModifiedCallbacks(userInfo.userId);
            }).catch(console.log)
        } else {
            extraLinks.push({
                url: new URL(`https://kemono.party/patreon/user/${cachedId}`),
                icon: "mat:money_off",
                type: "kemono_patreon",
                label: "Kemono patreon"
            });
        }
    }

    function addUserLinks({ extraLinks, userInfo }) {
        for(const link of extraLinks) {
            switch(link.label) {
                case "Fanbox": fanbox({extraLinks, userInfo}); break;
                case "patreon.com": patreon({link, extraLinks, userInfo}); break;
            }
        }
    }

    unsafeWindow.vviewHooks = {
        addUserLinks: addUserLinks
    };
})();