blive_room_info_api

简单封装一些信息

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.gf.qytechs.cn/scripts/439903/1037039/blive_room_info_api.js

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name            简单封装一些信息
// @version         1.0.6
// @description     无
// @match           bilibili
// @author          Pronax
// ==/UserScript==

var ROOM_INFO_API = {
    ROOM_PLAY_INFO: undefined,
    ROOM_USER_INFO: undefined,
    FANS_MEDAL_INFO: undefined,
    getRid: async function () {
        if (!this.ROOM_PLAY_INFO) {
            await this.fetchPlayInfo(this.getTempRid());
        }
        return this.ROOM_PLAY_INFO.room_id;
    },
    getUid: async function () {
        if (!this.ROOM_PLAY_INFO) {
            await this.fetchPlayInfo(this.getTempRid());
        }
        return this.ROOM_PLAY_INFO.uid;
    },
    getDanmuLength: async function () {
        if (!this.ROOM_USER_INFO) {
            await this.fetchUserInfo(this.getTempRid());
        }
        return this.ROOM_USER_INFO.property.danmu.length;
    },
    getFansMedalInfo: async function () {
        if (!this.ROOM_USER_INFO) {
            let uid = await ROOM_INFO_API.getUid();
            await this.fetchMedalInfo(uid);
        }
        return this.ROOM_USER_INFO.my_fans_medal;
    },
    getTempRid: function () {
        switch (true) {
            // 真实roomid
            case typeof (__NEPTUNE_IS_MY_WAIFU__) != 'undefined':
                return __NEPTUNE_IS_MY_WAIFU__.roomInitRes.data.room_id;
            case document.querySelector("#iframe-popup-area>iframe") != null:
                return document.querySelector("#iframe-popup-area>iframe").src.match(/roomid=(\d+)/)[1];
            // 下面的都是短位rid
            case location.href.match(/live.bilibili.com(\/blanc)?\/(\d+)/) != null:
                return location.href.match(/live.bilibili.com(\/blanc)?\/(\d+)/)[2];
            case document.querySelector("#player-ctnr iframe"):
                return document.querySelector("#player-ctnr iframe").src.match(/blanc\/(\d+)/)[1];
            case typeof (__initialState) != 'undefined' && __initialState["live-non-revenue-player"].length == 1:
                return __initialState["live-non-revenue-player"][0].defaultRoomId;
            default:
                alert("无法获得RID,请反馈给插件开发者");
        }
    },
    fetchPlayInfo: async function (rid) {
        return new Promise((resolve, reject) => {
            fetch(`https://api.live.bilibili.com/xlive/web-room/v2/index/getRoomPlayInfo?room_id=${rid}`, {
                credentials: 'include',
            })
                .then(res => res.json())
                .then(json => {
                    this.ROOM_PLAY_INFO = json.data;
                    resolve();
                });
        });
    },
    fetchUserInfo: async function (rid) {
        return new Promise((resolve, reject) => {
            fetch(`https://api.live.bilibili.com/xlive/web-room/v1/index/getInfoByUser?room_id=${rid}`, {
                credentials: 'include',
            })
                .then(res => res.json())
                .then(json => {
                    this.ROOM_USER_INFO = json.data;
                    resolve();
                });
        });
    },
    fetchMedalInfo: async function (uid) {
        return new Promise((resolve, reject) => {
            fetch(`https://api.live.bilibili.com/xlive/app-ucenter/v1/fansMedal/fans_medal_info?target_id=${uid}`, {
                credentials: 'include',
            })
                .then(res => res.json())
                .then(json => {
                    this.ROOM_USER_INFO = json.data;
                    resolve();
                });
        });
    }
};