kemono.party links for ppixiv

Add Kemono Party buttons on ppixiv user dropdown

目前為 2023-09-25 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         kemono.party links for ppixiv
// @namespace    https://www.pixiv.net/
// @version      1.3.1
// @description  Add Kemono Party buttons on ppixiv user dropdown
// @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
// @run-at       document-end
// ==/UserScript==

(function() {
    'use strict';

    const cachedPatreonUsers = {};
    const patreonIdRegex = /"id":\s*"(\d+)",[\n\s]*"type":\s*"user"/sm;
    const linkRegex = /((?:https?:\/\/)?(?:www)*\.?.+?\..+)/g;

    function normalizeUrl(url) {
        url = url.trim();
        if(!url.startsWith("http")) url = `https://${url}`;

        return url;
    }



    function getLinksFromDescription(extraLinks) {
        return removeDuplicates(
            preprocessMatches(
                Array.from(document.body.querySelector(".description").innerText.matchAll(linkRegex)).map(e => e[1])
            ),
            extraLinks
        );
    }

    function removeDuplicates(links, extraLinks) {
        const labels = extraLinks.map(e => e.label);
        return links.filter(e => !labels.includes(e.label));
    }

    const labelMatchingMap = {
        "patreon": "patreon.com",
        "fanbox": "Fanbox",
        "fantia": "fantia.jp",
    };
    function preprocessMatches(matches) {
        return matches.map(e => {
            try {
                const url = new URL(normalizeUrl(e));
                return {
                    label: labelMatchingMap[Object.keys(labelMatchingMap).filter(e => url.host.includes(e))[0]],
                    url
                };
            } catch {
                return {label: null, url: null};
            }
        });
    }



    function normalizePatreonLink({ link }) {
        if(typeof(link.url) === "string") link.url = new URL(normalizeUrl(link.url));

        link.url.protocol = "https";
        if(!link.url.host.startsWith("www.")) link.url.host = `www.${link.url.host}`;
    }

    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 patreon({link, extraLinks, userInfo}) {
        normalizePatreonLink({ link });
        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 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 fantia({link, extraLinks}) {
        const id = link.url.toString().split("/").pop();
        extraLinks.push({
            url: new URL(`https://kemono.party/fantia/user/${id}`),
            icon: "mat:money_off",
            type: "kemono_fantia",
            label: "Kemono fantia"
        });
    }



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

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