// ==UserScript==
// @name 哔哩哔哩视频页面常驻显示AV/BV号[已完全重构,支持显示分P标题]
// @namespace ckylin-bilibili-display-video-id
// @version 1.9
// @description 始终在哔哩哔哩视频页面标题下方显示当前视频号,默认显示AV号,右键切换为BV号,单击弹窗可复制链接
// @author CKylinMC
// @match https://www.bilibili.com/video*
// @match https://www.bilibili.com/medialist/play/*
// @resource cktools https://greasyfork.org/scripts/429720-cktools/code/CKTools.js?version=967994
// @resource popjs https://cdn.jsdelivr.net/gh/CKylinMC/PopNotify.js@master/PopNotify.js
// @resource popcss https://cdn.jsdelivr.net/gh/CKylinMC/PopNotify.js@master/PopNotify.css
// @grant unsafeWindow
// @grant GM_setValue
// @grant GM_getValue
// @grant GM_getResourceText
// @grant GM_registerMenuCommand
// @grant GM_unregisterMenuCommand
// @license GPL-3.0-only
// @downloadURL none
// ==/UserScript==
(function () {
//======[Apply all resources]
const resourceList = [
{name:'cktools',type:'js'},
{name:'popjs',type:'js'},
{name:'popcss',type:'css'},
{name:'popcsspatch',type:'rawcss',content:"div.popNotifyUnitFrame{z-index:110000!important;}.CKTOOLS-modal-content{color: #616161!important;}"},
]
function applyResource() {
resloop:for(let res of resourceList){
if(!document.querySelector("#"+res.name)){
let el;
switch (res.type) {
case 'js':
case 'rawjs':
el = document.createElement("script");
break;
case 'css':
case 'rawcss':
el = document.createElement("style");
break;
default:
console.log('Err:unknown type', res);
continue resloop;
}
el.id = res.name;
el.innerHTML = res.type.startsWith('raw')?res.content:GM_getResourceText(res.name);
document.head.appendChild(el);
}
}
}
applyResource();
//======
const wait = (t) => new Promise(r => setTimeout(r, t));
const waitForPageVisible = async () => {
return document.hidden && new Promise(r => document.addEventListener("visibilitychange", r))
}
const log = (...m) => console.log('[ShowAV]', ...m);
const getAPI = (bvid) => fetch('https://api.bilibili.com/x/web-interface/view?bvid=' + bvid).then(raw => raw.json());
const getAidAPI = (aid) => fetch('https://api.bilibili.com/x/web-interface/view?aid=' + aid).then(raw => raw.json());
const config = {
defaultAv: true,
firstTimeLoad: true,
showInNewLine: false,
pnmaxlength: 18,
orders: ['openGUI','showAv','showPn'],
all: ['showAv','showPn','showCid','showCate','openGUI'],
vduration: 0
};
const menuId = {
showAv: -1,
defaultAv: -1,
showInNewLine:-1,
showPn: -1,
showCate:-1,
showCid: -1,
};
const txtCn = {
showAv: "视频编号和高级复制",
showPn: "视频分P名",
showCid: "视频CID编号",
showCate: "视频所在分区",
openGUI: "设置选项"
};
let infos = {};
async function saveAllConfig(){
for(let configKey of Object.keys(config)){
if([
"all","vduration","firstTimeLoad"
].includes(configKey)) continue;
await GM_setValue(configKey, config[configKey]);
}
popNotify.success("配置保存成功");
}
async function initScript(flag = false) {
for(let menuitem of Object.keys(menuId)){
if(menuId[menuitem]!=-1) GM_unregisterMenuCommand(menuId[menuitem]);
}
for(let configKey of Object.keys(config)){
if([
"all","vduration","firstTimeLoad"
].includes(configKey)) continue;
if(typeof(await GM_getValue(configKey))==='undefined'){
await GM_setValue(configKey, config[configKey]);
}else{
config[configKey] = await GM_getValue(configKey);
}
}
if ((await GM_getValue("defaultAv"))) {
config.defaultAv = true;
menuId.defaultAv = GM_registerMenuCommand("默认显示BV号[当前显示av号]", async () => {
await GM_setValue("defaultAv", false);
initScript(true);
});
} else {
config.defaultAv = false;
menuId.defaultAv = GM_registerMenuCommand("默认显示av号[当前显示BV号]", async () => {
await GM_setValue("defaultAv", true);
initScript(true);
});
}
if ((await GM_getValue("showInNewLine"))) {
config.showInNewLine = true;
menuId.showInNewLine = GM_registerMenuCommand("显示模式: 换行 [点击切换]", async () => {
await GM_setValue("showInNewLine", false);
let old = document.querySelector("#bilibiliShowInfos")
if(old)old.remove();
initScript(true);
});
} else {
config.showInNewLine = false;
menuId.showInNewLine = GM_registerMenuCommand("显示模式: 附加 [点击切换]", async () => {
await GM_setValue("showInNewLine", true);
let old = document.querySelector("#bilibiliShowInfos")
if(old)old.remove();
initScript(true);
});
}
GM_registerMenuCommand("打开设置", async () => {
await GUISettings();
});
CKTools.addStyle(`
#bilibiliShowPN{
max-width: ${config.pnmaxlength}em!important;
}
`,"showav_pnlen","update",document.head);
tryInject(flag);
}
function atleastOne(){
let k = 0;
[...arguments].map(v=>k+=v);
return k>0;
}
async function playerReady() {
let i = 150;
while (--i > 0) {
await wait(100);
if (!('player' in unsafeWindow)) continue;
if (!('isInitialized' in unsafeWindow.player)) continue;
if (!unsafeWindow.player.isInitialized()) continue;
break;
}
if (i < 0) return false;
await waitForPageVisible();
while (1) {
await wait(200);
if (document.querySelector(".bilibili-player-video-control-wrap")) return true;
}
}
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 marginDirection = config.showInNewLine ? "Right" : "Left";
let target = document.querySelector("#" + id);
if (!target) {
target = document.createElement("span");
target.id = id;
target.style['margin'+marginDirection] = "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 registerVideoChangeHandler() {
const video = await waitForDom(".bilibili-player-video video");
if (!video) return;
const observer = new MutationObserver(async e => {
if (e[0].target.src) {
tryInject(true);
}
});
observer.observe(video, {attribute: true, attributes: true, childList: false});
}
function getPageFromCid(cid, infos) {
if (!cid || !infos || !infos.pages) return 1;
let pages = infos.pages;
if (pages.length == 1) return 1;
let page;
for (page of pages) {
if (!page) continue;
if (page.cid == cid) return page.page;
}
return 1;
}
async function feat_showCate(){
const {av_root,infos} = this;
const cate_span = getOrNew("bilibiliShowCate", av_root);
//if (config.showCate) {
cate_span.style.textOverflow = "ellipsis";
cate_span.style.whiteSpace = "nowarp";
cate_span.style.overflow = "hidden";
cate_span.title = "分区:"+infos.tname;
cate_span.innerText = "分区:"+infos.tname;
//} else cate_span.remove();
}
async function feat_showAv(){
const {av_root,infos} = this;
const av_span = getOrNew("bilibiliShowAV", av_root);
//if (config.showAv) {
if (config.defaultAv)
av_span.innerText = 'av' + infos.aid;
else
av_span.innerText = infos.bvid;
av_span.style.overflow = "hidden";
av_span.oncontextmenu = e => {
if (e.target.innerText.startsWith('av')) e.target.innerText = infos.bvid;
else av_span.innerText = 'av' + infos.aid;
e.preventDefault();
}
const video = await waitForDom("video");
if (video) {
config.vduration = Math.floor(video.duration);
}
const avspanHC = new CKTools.HoldClick(av_span);
avspanHC.onclick(async e=>{
let url = new URL(location.protocol + "//" + location.hostname + "/video/" + e.target.innerText);
infos.p == 1 || url.searchParams.append("p", infos.p);
let t = await getPlayerSeeks();
if (t && t != "0" && t != ("" + config.vduration)) url.searchParams.append("t", t);
copy(url);
popNotify.success("完整地址复制成功", url);
});
avspanHC.onhold(async e=>{
let url = new URL(location.protocol + "//" + location.hostname + "/video/" + e.target.innerText);
infos.p == 1 || url.searchParams.append("p", infos.p);
let vidurl = new URL(url);
let t = await getPlayerSeeks();
if (t && t != "0" && t != ("" + config.vduration)) url.searchParams.append("t", t);
CKTools.modal.alertModal("高级复制",`
点击输入框可以快速复制
当前地址
含视频进度地址(仅在播放时提供)
快速分享
快速分享(含视频进度)
MarkDown格式
BBCode格式