// ==UserScript== // @name 哔哩哔哩视频页面常驻显示AV/BV号[已完全重构,支持显示分P标题] // @namespace ckylin-bilibili-display-video-id // @version 1.1 // @description 始终在哔哩哔哩视频页面标题下方显示当前视频号,默认显示AV号,右键切换为BV号,单击弹窗可复制链接 // @author CKylinMC // @match https://www.bilibili.com/video* // @grant unsafeWindow // @license GPLv3 License // @downloadURL none // ==/UserScript== (function () { const wait = (t) => { return new Promise(r => setTimeout(r, t)); } const log = (...m) => console.log('[ShowAV]', ...m); let infos = {}; function videoInitInfoHandler(e) { if (e.data.command && e.data.pageData && e.data.command == "sendInitData") { infos = e.data.pageData; tryInject(); } } async function playerReady() { let i = 50; while (--i >= 0) { await wait(100); if (!('player' in unsafeWindow)) continue; if (!('isInitialized' in unsafeWindow.player)) continue; if (!unsafeWindow.player.isInitialized()) continue; return true; } return false; } async function waitForDom(q) { let i = 50; let dom; while (--i >= 0) { if (dom = document.querySelector(q)) break; await wait(100); } return dom; } function getUrlParam(key) { return (new URL(location.href)).searchParams.get(key); } function getOrNew(id, parent) { let target = document.querySelector("#" + id); if (!target) { target = document.createElement("span"); target.id = id; target.style.marginLeft = "16px"; parent.appendChild(target); } return target; } async function getPlayerSeeks() { const video = await waitForDom(".bilibili-player-video video"); let seconds = 0; if (video) { seconds = Math.floor(video.currentTime); } if (seconds == 0) { let fromParam = getUrlParam("t") || 0; return fromParam; } else return seconds; } async function tryInject() { log("Been called"); if (!(await playerReady())) return log('Can not load player in time.'); const av_root = await waitForDom(".video-data"); if (!av_root) return log('Can not load info-bar in time.'); const av_span = getOrNew("bilibiliShowAV", av_root); av_span.innerText = 'av' + infos.aid; av_span.oncontextmenu = e => { if (e.target.innerText.startsWith('av')) e.target.innerText = infos.bvid; else av_span.innerText = 'av' + infos.aid; e.preventDefault(); } av_span.onclick = async e => { let url = new URL(location.protocol + "//" + location.hostname + location.pathname); infos.p == 1 || url.searchParams.append("p", infos.p); let t = await getPlayerSeeks(); if (t && t != "0") url.searchParams.append("t", t); prompt("当前视频地址:", url); } const videoData = infos.videoData; if (!videoData) return; let part = { part: 'P' + infos.p } try { part = videoData.pages[infos.p - 1]; } catch (e) { part = videoData.pages[0]; } const pn_span = getOrNew("bilibiliShowPN", av_root); const currentPageNum = `P ${infos.p}/${videoData.videos}`; const currentPageName = `《${part.part}》` pn_span.style.textOverflow = "ellipsis"; pn_span.style.whiteSpace = "nowarp"; pn_span.style.overflow = "hidden"; pn_span.title = currentPageNum + "\n" + currentPageName pn_span.innerText = currentPageNum + " " + currentPageName; } unsafeWindow.addEventListener("message", videoInitInfoHandler); })();