Bilibili MediaSession Helper

Dock metadata to MediaSession API for bilibili.com (Chrome 73+)

当前为 2020-01-31 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name		 Bilibili MediaSession Helper
// @namespace	http://tampermonkey.net/
// @version	  0.5
// @description  Dock metadata to MediaSession API for bilibili.com (Chrome 73+)
// @author	   nondanee
// @match		https://*.bilibili.com/*
// @grant		none
// ==/UserScript==

(() => {
	if (!('mediaSession' in navigator)) return
	if (self != top) return

	const updateMetadata = () =>
		Promise.resolve()
		.then(() => {
			const url = window.location.href
			let id, title, artist, cover
			if (id = (url.match(/live\.bilibili\.com\/(\d+)/) || [])[1]) {
				return fetch(`//api.live.bilibili.com/xlive/web-room/v1/index/getInfoByRoom?room_id=${id}`)
				.then(response => response.json())
				.then(body => body.data)
				.then(data => Object.assign(data.room_info, {artist: data.anchor_info.base_info.uname}))
			}
			else if (id = (url.match(/bilibili\.com\/video\/av(\d+)/) || [])[1]) {
				const {videoData} = window.__INITIAL_STATE__
				return {
					title: videoData.title,
					artist: videoData.owner.name,
					cover: videoData.pic.replace('https:', '').replace('http:', '')
				}
			}
			else if (id = (url.match(/bilibili\.com\/bangumi\/play\/(\w+)/) || [])[1]) {
				const {progress = {}} = window.__PGC_USERSTATE__
				const {epInfo, epList, mediaInfo, sections = []} = window.__INITIAL_STATE__
				if (id.startsWith('ss'))
					id = epInfo.id === -1 ? progress.last_ep_id : epInfo.id
				else if (id.startsWith('ep'))
					id = id.slice(2)
				const epListFromSections = sections.reduce((result, section) => result.concat(section.epList), [])
				const targetEpInfo = Array.prototype.concat(epList, epListFromSections).find(item => item.id.toString() === id.toString())
				return {
					title: mediaInfo.title,
					artist: (targetEpInfo.titleFormat + ' ' + targetEpInfo.longTitle).trim(),
					cover: targetEpInfo.cover
				}
			}
		})
		.then(({title, artist, cover}) => {
			const image = new Image()
			image.onload = () => {
				navigator.mediaSession.metadata = new window.MediaMetadata({
					title,
					artist,
					artwork: [{src: cover, sizes: `${image.naturalWidth}x${image.naturalHeight}`, type: 'image/jpeg'}]
				})
			}
			image.src = cover
		})

	updateMetadata()

	const _pushState = history.pushState
	history.pushState = function (...props) {
		_pushState.apply(this, props)
		updateMetadata()
	}

	const _replaceState = history.replaceState
	history.replaceState = function (...props) {
		_replaceState.apply(this, props)
		updateMetadata()
	}
})()