Greasy Fork is available in English.

哔哩哔哩轻量化

哔哩哔哩播放器轻量化工具

  1. // ==UserScript==
  2. // @name 哔哩哔哩轻量化
  3. // @namespace https://ez118.github.io/
  4. // @version 3.8
  5. // @description 哔哩哔哩播放器轻量化工具
  6. // @author ZZY_WISU
  7. // @match *://*.bilibili.com/*
  8. // @icon https://static.hdslb.com/images/favicon.ico
  9. // @license GPLv3
  10. // @grant GM_xmlhttpRequest
  11. // @grant GM_download
  12. // @grant GM_registerMenuCommand
  13. // @grant GM_addStyle
  14. // @grant GM_setValue
  15. // @grant GM_getValue
  16. // @run-at document-start
  17. // ==/UserScript==
  18.  
  19. /* 存储初始化 */
  20. if(GM_getValue('IfNewPlayer') == null || GM_getValue('IfNewPlayer') == undefined){ GM_setValue('IfNewPlayer', true); }
  21.  
  22. /* 配置开始 */
  23.  
  24. var EmbedPlayerUrl = GM_getValue('IfNewPlayer') ? "https://www.bilibili.com/blackboard/html5mobileplayer.html?" : "https://www.bilibili.com/blackboard/webplayer/activity-embed-other-old.html?";
  25.  
  26. var VideoDetail = ""; /* 用于记录当前视频简介的变量 */
  27. var VideoTitle = ""; /* 用于记录当前视频标题的变量 */
  28. var VideoAid = ""; /* 用于记录当前视频aid的变量(bvid不记录,api使用aid的较多) */
  29. /* 配置结束 */
  30.  
  31.  
  32. function GetAjax(url,func) {
  33. GM_xmlhttpRequest({
  34. method: "GET", url: url, data:"",
  35. headers: {"Content-Type": "application/x-www-form-urlencoded;charset=utf-8"},
  36. onload: function(response){func(response.responseText);}, onerror: function(response){alert("[错误]\n请求失败");}
  37. });
  38. }
  39.  
  40. function $GET(name){
  41. var reg = new RegExp('(^|&)'+name+'=([^&]*)(&|$)', 'i');
  42. var r = window.location.search.substr(1).match(reg);
  43. if (r != null) { return unescape(r[2]); }
  44. return "";
  45. }
  46.  
  47. function GetRedirect(){
  48. /* 通过普通播放页链接获取aid或bvid */
  49. var str = window.location.href;
  50. str = str.replace("/s/", "/");
  51. if(str.indexOf("?") !== -1){}else{ str += "?";}
  52. str = str.split("/")[4].replace(str.substring(str.lastIndexOf("?")), "").replace("/", "");
  53. if(str[1] == "v"){
  54. return {"type":"aid", "id":str.replace("av", "")};
  55. } else {
  56. return {"type":"bvid", "id":str};
  57. }
  58. }
  59.  
  60. function ShowVideoReplies(content){
  61. /* 向网页插入html元素,实现展示评论内容 */
  62.  
  63. content = content.replace(/\[.*?\]/g,"").replace(/\r?\n/g,'<br/>').replace(/\【/g, "<b>【").replace(/\】/g, "】</b>");
  64.  
  65. GM_addStyle(`.userscript-bilibili-replies-close { width:100%; border-radius:12px; background:#f25d8e; color:#FFF; padding:8px; border:none; margin:5px 0 5px 0; }
  66. .userscript-bilibili-replies-hide { word-wrap:break-word; width:25%; height:100%; overflow-x:auto; background:#FFFFFFCC; color:#000; padding:10px; border:1px solid #CCC; line-height:20px; font-size:15px; position:fixed; top:0px; right:calc(-25% + 5px); bottom:0px; z-index:999; transition:all 0.3s; }
  67. .userscript-bilibili-replies-hide:hover { right:0%; }
  68. .userscript-bilibili-replies-show { word-wrap:break-word; width:25%; height:100%; overflow-x:auto; background:#FFFFFFCC; color:#000; padding:10px; border:1px solid #CCC; line-height:20px; font-size:15px; position:fixed; top:0px; right:0%; bottom:0px; z-index:999; }
  69. .userscript-bilibili-reptitle { font-size:20px; font-weight:bold; margin-bottom:10px; color:#FFFFFF; background-color:#000000;}`);
  70.  
  71. let MDiv = document.createElement('div');
  72. document.body.appendChild(MDiv);
  73. MDiv.setAttribute("class", "userscript-bilibili-replies-show");
  74. MDiv.setAttribute("id", "userscript-bilibili-replies");
  75.  
  76. MDiv.innerHTML = `<b class="userscript-bilibili-reptitle">&nbsp;视频详情&nbsp;</b><br/>` + VideoDetail.replace(/\r?\n/g,'<br/>') + `<br/><hr/>
  77. <b class="userscript-bilibili-reptitle">&nbsp;视频评论&nbsp;</b><br/>` + content + `<br/>
  78. <button onclick='document.getElementById("userscript-bilibili-replies").remove()' class="userscript-bilibili-replies-close">关闭浏览栏</button><br/><br/>`;
  79.  
  80. setTimeout(function(){
  81. document.getElementById('userscript-bilibili-replies').setAttribute('class','userscript-bilibili-replies-hide');
  82. }, 3000);
  83. }
  84.  
  85. function GetVideoReplies(aid){
  86. /* 获取评论内容 */
  87. GetAjax("https://api.bilibili.com/x/v2/reply?jsonp=jsonp&pn=1&type=1&sort=2&oid=" + aid, function(result){
  88. result = JSON.parse(result);
  89. if(result.code == 0){
  90. let UserShow = "";
  91.  
  92. (result.data.replies).forEach((item, index) => {
  93. UserShow += "【" + item.member.uname + "】 " + item.content.message + "\n";
  94. })
  95.  
  96. ShowVideoReplies(UserShow);
  97. } else {
  98. alert("【提示】评论获取失败");
  99. }
  100. });
  101. }
  102.  
  103.  
  104.  
  105. let menu1 = GM_registerMenuCommand('查看视频详情&评论', function () {
  106. /* 查看视频详情&评论菜单 */
  107.  
  108. let url = window.location.href;
  109.  
  110. if(url.includes("bilibili.com/blackboard/webplayer/") || url.includes("bilibili.com/blackboard/html5mobileplayer")){
  111. /* 如果是播放页,那么执行获取评论的函数 */
  112. GetVideoReplies(VideoAid);
  113. } else {
  114. /* 如果不是,则向用户提示: */
  115. alert("【提示】不支持的页面(若误判请反馈)");
  116. }
  117. }, 'R');
  118.  
  119.  
  120. let menu2 = GM_registerMenuCommand('在PC客户端中查看', function () {
  121. /* 在BiliBili客户端中查看视频菜单功能 */
  122.  
  123. let url = window.location.href;
  124.  
  125. if(url.includes("bilibili.com/video/")){
  126. /* 如果不是目标页面 */
  127. alert("【提示】不支持的页面(若误判请反馈)");
  128. } else if(url.includes("bilibili.com/blackboard/webplayer/") || url.includes("bilibili.com/blackboard/html5mobileplayer")) {
  129. /* 如果是轻量播放器,则直接使用页面加载时存储的aid创建链接 */
  130. location.href = "bilibili://video/" + VideoAid;
  131. }
  132. }, 'O');
  133.  
  134. let menu3 = GM_registerMenuCommand('更改视频播放器', function () {
  135. /* 让用户决定是否使用新版播放器 */
  136.  
  137. var test_value = confirm("【请选择】\n请选择是否使用新版播放器\n(“确定”使用新版(推荐),“取消”使用旧版)");
  138. if(test_value) { GM_setValue('IfNewPlayer', true); }
  139. else { GM_setValue('IfNewPlayer', false); }
  140. }, 'S');
  141.  
  142.  
  143.  
  144. (function() {
  145. 'use strict';
  146.  
  147. var url = window.location.href;
  148.  
  149. if(url.includes("bilibili.com/video/")){
  150. /* 如果当前页面为普通播放页 */
  151. let cb = GetRedirect();
  152. top.location.href = EmbedPlayerUrl + cb.type + "=" + cb.id + "&page=1&danmaku=0";
  153. } else if(url.includes("bilibili.com/blackboard/webplayer/") || url.includes("bilibili.com/blackboard/html5mobileplayer")) {
  154. /* 推荐的播放器 */
  155.  
  156. /* 获取视频标题和详情文本,方便后续获取,减少后续请求所用时长 */
  157. let DataStr = "";
  158. if($GET("bvid") != ""){ DataStr = "bvid=" + $GET("bvid"); }
  159. else { DataStr = "aid=" + $GET("aid"); }
  160. GetAjax("https://api.bilibili.com/x/web-interface/view?" + DataStr, function(aid){
  161. aid = JSON.parse(aid);
  162. try{
  163. VideoDetail = aid.data.desc;
  164. VideoTitle = aid.data.title;
  165. VideoAid = aid.data.aid;
  166. } catch {
  167. console.log("[USERSCRIPT/哔哩哔哩轻量化] 视频信息获取失败");
  168. }
  169. });
  170.  
  171. /* 删除播放器多余控件,更改视频标题 */
  172. setTimeout(function() {
  173. try{
  174. document.getElementsByClassName("bilibili-player-video-sendjumpbar")[0].remove();
  175. document.getElementsByClassName("bilibili-player-video-pause-panel-container-qrcode")[0].remove();
  176. document.getElementsByTagName("title")[0].innerText = VideoTitle + "_哔哩哔哩_bilibili";
  177. } catch { }
  178. }, 3000);
  179. } else if(url.includes("player.bilibili.com/player")) {
  180. /* 烦人的新播放器 */
  181.  
  182. /* 获取视频标题和详情文本,方便后续获取,减少后续请求所用时长 */
  183. let DataStr = "";
  184. if($GET("bvid") != ""){ DataStr = "bvid=" + $GET("bvid"); }
  185. else { DataStr = "aid=" + $GET("aid"); }
  186. GetAjax("https://api.bilibili.com/x/web-interface/view?" + DataStr, function(aid){
  187. aid = JSON.parse(aid);
  188. try{
  189. VideoDetail = aid.data.desc;
  190. VideoTitle = aid.data.title;
  191. VideoAid = aid.data.aid;
  192. } catch {
  193. console.log("[USERSCRIPT/哔哩哔哩轻量化] 视频信息获取失败");
  194. }
  195. });
  196.  
  197. /* 删除播放器多余控件,更改视频标题 */
  198. setTimeout(function() {
  199. document.getElementsByClassName("bpx-player-relation-button")[0].remove();
  200. document.getElementsByClassName("bpx-player-inputbar-mask")[0].remove();
  201. document.getElementsByClassName("bpx-player-enter-button")[0].remove();
  202. GM_addStyle(".bpx-player-video-wrap video{pointer-events: none!important;}");
  203. //document.getElementsByClassName("bpx-player-dialog-area")[0].remove();
  204. document.getElementsByTagName("title")[0].innerText = VideoTitle + "_哔哩哔哩_bilibili";
  205. //document.querySelectorAll("*")
  206. document
  207. .getElementsByClassName("bpx-player-video-perch")
  208. .forEach(node =>
  209. node.addEventListener(
  210. "click",
  211. event => event.stopImmediatePropagation(),
  212. true
  213. )
  214. );
  215. }, 3000);
  216. }
  217. })();