// ==UserScript== // @name 为B站直播间添加分区跳转按钮 // @namespace http://tampermonkey.net/ // @version 1.1 // @description 在直播间播放器右上角补充当前直播的分区信息和跳转按钮 // @author byhgz // @icon https://static.hdslb.com/images/favicon.ico // @license Apache-2.0 // @run-at document-end // @match *://live.bilibili.com/* // @exclude https://live.bilibili.com/p* // @grant GM_registerMenuCommand // @grant GM_openInTab // ==/UserScript== (function(){'use strict';if (unsafeWindow.mk_wins) { unsafeWindow.mk_wins["mkBiliLivePartitionJumper"] = window; } else { window.mk_wins = { mkBiliLivePartitionJumper: window }; } var globalValue = { group_url: "http://qm.qq.com/cgi-bin/qm/qr?_wv=1027&k=tFU0xLt1uO5u5CXI2ktQRLh_XGAHBl7C&authKey=KAf4rICQYjfYUi66WelJAGhYtbJLILVWumOm%2BO9nM5fNaaVuF9Iiw3dJoPsVRUak&noverify=0&group_code=876295632", scriptCat_js_url: "https://scriptcat.org/zh-CN/script-show-page/6248", greasyFork_js_url: "https://greasyfork.org/zh-CN/scripts/577801", update_log_url: "https://docs.qq.com/doc/DSnhjSVZmRkpCd0Nj" };GM_registerMenuCommand("脚本猫脚本更新页", () => { GM_openInTab(globalValue.scriptCat_js_url); }, "E"); GM_registerMenuCommand("gf脚本更新页", () => { GM_openInTab(globalValue.greasyFork_js_url); }, "W"); GM_registerMenuCommand("加入or反馈", () => { GM_openInTab(globalValue.group_url); }, "T"); GM_registerMenuCommand("更新日志", () => { GM_openInTab(globalValue.update_log_url); }, "U");var elUtil = { elFindCss(selector, doc = document, timeout = 1e3) { return new Promise((resolve) => { const i = setInterval(() => { const el = doc.querySelector(selector); if (el === null) return; clearInterval(i); resolve(el); }, timeout); }); } };var gzStyleCss = `button[gz_type] { display: inline-block; line-height: 1; white-space: nowrap; cursor: pointer; border: 1px solid #dcdfe6; color: #F07775; -webkit-appearance: none; text-align: center; box-sizing: border-box; outline: none; margin: 0; transition: .1s; font-weight: 500; -moz-user-select: none; -webkit-user-select: none; -ms-user-select: none; padding: 10px 20px; font-size: 14px; border-radius: 8px; } button[gz_type="info"] { color: #fff; background-color: #909399; border-color: #909399; } button:hover { border-color: #646cff; } button[gz_type]:focus, button[gz_type]:focus-visible { outline: 4px auto -webkit-focus-ring-color; } `;var defUtl = { addGzStyle(el, insertionPosition = document.head) { const styleEl = el.querySelector("style[gz_style]"); if (styleEl !== null) { console.log("已有gz_style样式,故不再插入该样式内容"); return; } const style = document.createElement("style"); style.setAttribute("gz_style", ""); style.textContent = gzStyleCss; insertionPosition.appendChild(style); } };const isLiveRoom = (url = location.href) => { return url.search("/live.bilibili.com/\\d+") !== -1 || url.search("https://live.bilibili.com/blanc/\\d+") !== -1 || url.includes("live.bilibili.com/blackboard/era"); }; const isLiveRoomActivity = () => { return isLiveRoom() && !document.title.endsWith("哔哩哔哩直播,二次元弹幕直播平台"); }; function installLiveSectionButton(body = document.body) { const p = new Promise((resolve) => { const i = setInterval(() => { const headInfoVmDivEl = body.querySelector("#head-info-vm"); if (headInfoVmDivEl === null) return; const vueData = headInfoVmDivEl["__vue__"]; if (vueData === void 0) return; clearInterval(i); resolve(vueData); }, 1e3); }); p.then((vueData) => { const { childAreaUri, liveAreaName } = vueData; elUtil.elFindCss(".normal-row-ctnr", body).then((el) => { const butAEl = document.createElement("a"); butAEl.target = "_blank"; butAEl.href = childAreaUri; const but = document.createElement("button"); but.setAttribute("gz_type", "info"); but.textContent = liveAreaName; but.title = `当前直播的分区:${liveAreaName}`; butAEl.appendChild(but); el.appendChild(butAEl); }); }); } var router = { staticRoute(url, title) { if (!isLiveRoom(url)) return; const isLiveRoomActivityVal = isLiveRoomActivity(); if (isLiveRoomActivityVal) { console.log("直播活动页面"); elUtil.elFindCss("#player-ctnr iframe").then((el) => { const iframeEl = el; const contentDocument = iframeEl.contentDocument; defUtl.addGzStyle(contentDocument.head, contentDocument.head); installLiveSectionButton(contentDocument); }); return; } else { installLiveSectionButton(); console.log("常规直播间", url, title); } } };window.addEventListener("DOMContentLoaded", () => { defUtl.addGzStyle(document.body); router.staticRoute(window.location.href, document.title); });})();