Go to kemono.party

Go to kemono.party from an artist's page. Supports Patreon, Pixiv, Fanbox and Fantia

As of 2023-07-14. See the latest version.

// ==UserScript==
// @name         Go to kemono.party
// @name:ja      kemono.partyへ移動
// @namespace    https://greasyfork.org/ja/users/1126644-s-k-script
// @version      0.1.2
// @description  Go to kemono.party from an artist's page. Supports Patreon, Pixiv, Fanbox and Fantia
// @description:ja  アーティストのページから対応するkemono.partyのページへ移動します。Pixiv, Fanbox, Fantia, Patreonをサポートしています。
// @author       S.K.Script
// @homepage     https://greasyfork.org/ja/users/1126644-s-k-script
// @license      GPL-3.0-only
// @match        https://fantia.jp/*
// @match        https://*.fanbox.cc/*
// @match        https://www.pixiv.net/*
// @match        https://www.patreon.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=kemono.party
// @grant        none
// @run-at context-menu
// ==/UserScript==

(function() {
    'use strict';

    class FantiaUrl {
        static check(href) {
            return !!href.match(/^https:\/\/fantia.jp/i);
        }

        constructor(href) {
            this.href = href;
        }

        extract_user_id() {
            const re = /https:\/\/fantia.jp\/fanclubs\/(?<userid>\d+)/i;
            const match = this.href.match(re);
            if(match) {
                return {ok: match.groups.userid};
            }
            else {
                return {err: 'Try again in the top page of this fanclub.\nファンクラブのトップページに移動してからもう一度実行してください'};
            }
        }

        generate_kemono_url(user_id) {
            return 'https://kemono.party/fantia/user/' + user_id;
        }
    }

    class FanboxUrl {
        static check(href) {
            return !!href.match(/^https:\/\/\w+.fanbox.cc/i);
        }

        constructor(href) {
            this.href = href;
        }

        extract_user_id() {
            return {err: 'Try again in the Pixiv user page of this person. Note: Pixiv and Fanbox are run by the same company.\nこの人のPixiv(Fanboxではなく)のユーザーページへ移動してからもう一度実行してください'};
        }

        generate_kemono_url(user_id) {
            return 'https://kemono.party/fanbox/user/' + user_id;
        }
    }

    class PixivUrl {
        static check(href) {
            return !!href.match(/https:\/\/www.pixiv.net/i);
        }

        constructor(href) {
            this.href = href;
        }

        extract_user_id() {
            const re = /https:\/\/www.pixiv.net\/users\/(?<userid>\d+)/i;
            const match = href.match(re);
            if(match) {
                return {ok: match.groups.userid};
            }
            else {
                return {err: 'Try again in the Pixiv user page of this person.\nPixivのユーザーページに移動してからもう一度実行してください'};
            }
        }

        generate_kemono_url(user_id) {
            return 'https://kemono.party/fanbox/user/' + user_id;
        }
    }

    class PatreonUrl {
        static check(href) {
            return !!href.match(/https:\/\/www.patreon.com/i);
        }

        constructor(href) {
            this.href = href;
        }

        extract_user_id() {
            // https://www.patreon.com/user?u=35870453
            const re = /https:\/\/www.patreon.com\/user\?u=(?<userid>\d+)/i;
            const match = href.match(re);
            if(match) {
                return {ok: match.groups.userid};
            }
            else {
                return {err: 'Try again in the Patreon user page of this person.\nPatreonのユーザーページに移動してからもう一度実行してください'};
            }
        }

        generate_kemono_url(user_id) {
            return 'https://kemono.party/patreon/user/' + user_id;
        }
    }

    const href = location.href;

    let url;
    if(FantiaUrl.check(href)) {
        url = new FantiaUrl(href);
    }
    else if(FanboxUrl.check(href)) {
        url = new FanboxUrl(href);
    }
    else if(PixivUrl.check(href)) {
        url = new PixivUrl(href);
    }
    else if(PatreonUrl.check(href)) {
        url = new PatreonUrl(href);
    }
    else {
        url = undefined;
    }

    if(!url) {
        window.alert('Not supported / 未対応/非対応です');
        return;
    }

    const result = url.extract_user_id();
    if(result.ok) {
        const user_id = result.ok;
        const kemono_url = url.generate_kemono_url(user_id);
        window.open(kemono_url, '_blank');
    }
    else {
        const err_message = result.err ? result.err : 'Error / なんかエラーだって';
        window.alert(err_message);
    }

})();