B站直播间自动换牌子

BiliBili liveroom assistant

目前为 2022-01-19 提交的版本。查看 最新版本

// ==UserScript==
// @name         B站直播间自动换牌子
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  BiliBili liveroom assistant
// @author       残云cyun
// @match        https://live.bilibili.com/*
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';
	// 换牌子
	let wear = (medal_id, token) => {
		const xhr = new XMLHttpRequest()
		xhr.responseType = 'json'
		xhr.withCredentials=true
		xhr.open("POST","https://api.live.bilibili.com/xlive/web-room/v1/fansMedal/wear")
		xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
		xhr.send(`medal_id=${medal_id}&csrf_token=${token}&csrf=${token}`)
	}

	// 获取Cookies
	let get_cookies = () => {
		let cookies = {}
		let cookies_array = document.cookie.split('; ')
		for (const prop of cookies_array) {
			let [k, v] = prop.split('=')
			cookies[k] = v
		}
		return cookies;
	}

	// 获取直播间详细信息
	let get_info = (liveroom_id) => {
		return new Promise((res, rej) => {
			let xhr = new XMLHttpRequest()
			xhr.responseType = 'json'
			xhr.withCredentials = true
			xhr.onreadystatechange = () => {
				if(xhr.readyState === 4) {
					if(xhr.status === 200) {
						res(xhr.response)
					}else {
						rej('error')
					}
				}
			}
			xhr.open("GET", `https://api.live.bilibili.com/xlive/web-room/v1/index/getInfoByUser?room_id=${liveroom_id}&from=0`)
			xhr.send()
		})
	}

	// 获取牌子列表
	let get_medal_list = (uid) => {
		return new Promise((res, rej) => {
			const xhr = new XMLHttpRequest()
			xhr.responseType = 'json'
			xhr.withCredentials = true
			xhr.onreadystatechange = () => {
				if(xhr.readyState === 4) {
					if(xhr.status === 200) {
						res(xhr.response)
					}else {
						rej('error')
					}
				}
			}
			xhr.open("GET", `https://api.live.bilibili.com/xlive/web-ucenter/user/MedalWall?target_id=${uid}`)
			xhr.send()
		})
	}

	let cookies = get_cookies()
	let csrf_token = cookies['bili_jct']
	let liveroom_id = window.location.pathname.split('/').pop()

	let info = null
	get_info(liveroom_id).then(res => {
		info = res
		return get_medal_list(res['data']['info']['uid'])
	}).then(res => {
		let list = res['data']['list']
		let up_uid = info['data']['medal']['up_medal']['uid']

		let weared = false
		for (const prop of list) {
			let medal_id = prop['medal_info']['medal_id']
			let target_id = prop['medal_info']['target_id']
			if(target_id === up_uid) {
				wear(medal_id, csrf_token)
				weared = true
				break
			}
		}

		// 没有该主播的牌子
		if(!weared) {
			wear(185810, csrf_token)
		}
	})
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址