Yuba Fix

可使处于被关闭状态的斗鱼鱼吧的大多数功能暂时恢复正常使用,需要借用状态正常的鱼吧的UI。

// ==UserScript==
// @name         Yuba Fix
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  可使处于被关闭状态的斗鱼鱼吧的大多数功能暂时恢复正常使用,需要借用状态正常的鱼吧的UI。
// @match        *://yuba.douyu.com/discussion/13062*
// @match        *://yuba.douyu.com/discussion/5496243*
// @grant        none
// @run-at       document-start
// @license      MIT
// ==/UserScript==

(async function () {
    'use strict';

    var css = '.banner__AyafQ{background-color:#FFFFFF !important;object-fit:contain;}';
    css += '.groupname__BUzOM{color:#515151 !important;text-shadow:0 0 0px #ffffff !important;}';
    css += '.groupdesc__b8-53{color:#515151 !important;text-shadow:0 0 0px #ffffff !important;}';

    const OLD_GROUP_ID = '13062';
    const FAKE_GROUP_ID = '5496243';
    const FAKE_API_URL = `https://yuba.douyu.com/wbapi/web/group/head?group_id=${FAKE_GROUP_ID}`;
    const PUBLISH_API_URL = 'https://yuba.douyu.com/wgapi/yubanc/api/feed/publish';
    const FAKE_RESPONSE = {
        "data": {
            "group_id": 13062,
            "group_name": "玩机器丶Machine",
            "avatar": "https://apic.douyucdn.cn/upload/avatar_v3/201905/badbf01f7ab943358bf78bcd9245305f_big.jpg",
            "describe": "玩机器丶Machine的个人鱼吧",
            "post_num": 6657,
            "fans_num": 6657,
            "anchor_id": 5448527,
            "unread": 0,
            "is_follow": 1,
            "group_level": 3,
            "group_exp": 20,
            "level_status": 0,
            "level_medal": "https://img.douyucdn.cn/data/yuba/admin/2018/10/16/201810161200503920739346639.png?i=2326458f9353bc988e1a27c68175065282",
            "level_medal_new": "https://img.douyucdn.cn/data/yuba/weibo/2018/09/20/201809201549507007262123703.png",
            "next_level_exp": 30,
            "banner": "https://c-yuba.douyucdn.cn/yubavod/b/peApOkzz5mdl/7ced121e3251a680dfdf402305fd349c.jpg", //鱼吧banner背景图
            "group_title": "鱼塘司机",
            "is_signed": 0,
            "fid": 525,
            "f_name": "PC游戏",
            "f_describe": "斗鱼PC游戏版块",
            "f_avatar": "https://apic.douyucdn.cn/upload/avatar_v3/201905/badbf01f7ab943358bf78bcd9245305f_big.jpg",
            "manager_type": 0,
            "group_type": 2,
            "group_new_type": 2,
            "has_starwall": 0,
            "rank": 137,
            "has_user_rank": 1,
            "has_user_rank_reward": 1,
            "safe_anchor_id": "W67QgJNpzd0O",
            "has_game_comment": 0,
            "has_information": 0,
            "digest_tags": [],
            "cate2_info": { "c_template": 0 },
            "news_sw": 0,
            "hot_sort": false,
            "hor_cover": "",
            "game_id": 0
        },
        "message": "",
        "status_code": 200
    };

    const redirectRegex = new RegExp(`^/discussion/${FAKE_GROUP_ID}/posts(?:[/?#]|$)`);
    const redirectRegex2 = new RegExp(`^/discussion/${FAKE_GROUP_ID}/highlight(?:[/?#]|$)`);

    if (redirectRegex.test(window.location.pathname + window.location.search)) {
        const query = window.location.search || '';
        const hash = window.location.hash || '';
        window.location.href = `/discussion/${OLD_GROUP_ID}/posts${query}${hash}`;
    }
    if (redirectRegex2.test(window.location.pathname + window.location.search)) {
        const query = window.location.search || '';
        const hash = window.location.hash || '';
        window.location.href = `/discussion/${OLD_GROUP_ID}/highlight${query}${hash}`;
    }

    function titlechange() {
        document.title = '玩机器丶Machine的鱼吧';
    }

    const originalFetch = window.fetch;
    window.fetch = async function (...args) {
        let [input, init = {}] = args;

        if (typeof input === 'string') {
            input = input.replace(`=${OLD_GROUP_ID}`, `=${FAKE_GROUP_ID}`);
            if (input.includes(FAKE_API_URL)) {
                return new Response(JSON.stringify(FAKE_RESPONSE), {
                    status: 200,
                    headers: { 'Content-Type': 'application/json' }
                });
            }
            titlechange();
            if (input.includes(PUBLISH_API_URL) &&
                init.method?.toUpperCase() === 'POST' &&
                init.headers?.['Content-Type']?.includes('application/x-www-form-urlencoded') &&
                typeof init.body === 'string') {
                init.body = init.body.replace(`group_id=${OLD_GROUP_ID}`, `group_id=${FAKE_GROUP_ID}`);
            }
        }

        return originalFetch.call(this, input, init);
    };

    const originalXHROpen = XMLHttpRequest.prototype.open;
    const originalXHRSend = XMLHttpRequest.prototype.send;

    XMLHttpRequest.prototype.open = function (method, url) {
        this._method = method;
        this._originalUrl = url;
        this._isTarget = url.includes(FAKE_API_URL);
        const newUrl = url.replace(`=${OLD_GROUP_ID}`, `=${FAKE_GROUP_ID}`);
        return originalXHROpen.call(this, method, newUrl);
    };

    XMLHttpRequest.prototype.send = async function (body) {
        if (this._isTarget) {
            this.addEventListener('readystatechange', async function () {
                if (this.readyState === 4) {
                    Object.defineProperty(this, 'responseText', {
                        get: () => JSON.stringify(FAKE_RESPONSE)
                    });
                    Object.defineProperty(this, 'response', {
                        get: () => JSON.stringify(FAKE_RESPONSE)
                    });
                }
            });
        if (this._isPublish && this._method.toUpperCase() === 'POST' && typeof body === 'string') {
            body = body.replace(`group_id=${OLD_GROUP_ID}`, `group_id=${FAKE_GROUP_ID}`);
        }
            await new Promise(resolve => setTimeout(resolve, 0));
        }
        return originalXHRSend.call(this, body);
    };

    // 修改banner
    function loadStyle(css) {
        const style = document.createElement('style');
        style.type = 'text/css';
        style.rel = 'stylesheet';
        style.appendChild(document.createTextNode(css));
        document.head.appendChild(style);
    }

    loadStyle(css);

//    window.addEventListener('load', () => {
//        setTimeout(() => {
//            titlechange();
//        }, 1500);
//    });

})();