EN
/video/44251798.html

网页端B站屏蔽视频推荐的脚本

2025-06-24 12:25:54 来源: 新华社
字号:默认 超大 | 打印 |

脚本使用:

脚本已上传至油猴:https://greasyfork.org/zh-CN/scripts/466760-b站学习脚本

功能:

  • 屏蔽播放页以及首页的视频推荐(t键切换)
  • 扩大选集框快捷键l
  • 字幕快捷键z
  • 页面全屏快捷键y
  • 宽屏快捷键k

效果展示:

首页推荐屏蔽:
首页推荐被屏蔽

视频侧边栏推荐屏蔽:
在这里插入图片描述

搜索框以及热搜榜屏蔽:
在这里插入图片描述
扩大视频选框,更加方便的查看选框名称以及选集播放:
在这里插入图片描述

代码实现:

屏蔽推荐的核心是通过MutationObserver监听页面变化,及时把推荐信息的display属性变为none
而本地通过localstorage记录了用户是否屏蔽视频的标志,通过快捷键t可以切换。

// ==UserScript==// @name         b站学习工具// @version      2.0// @description  屏蔽视频推荐,扩大选集框,字幕快捷键z、y、z。// @author       your_hamster// @match        https://www.bilibili.com/*// @match        https://space.bilibili.com/*// @icon         https://www.bilibili.com/favicon.ico// @grant        none// @namespace https://greasyfork.org/users/1080975// @downloadURL https://update.greasyfork.org/scripts/466760/b%E7%AB%99%E5%AD%A6%E4%B9%A0%E5%B7%A5%E5%85%B7.user.js// @updateURL https://update.greasyfork.org/scripts/466760/b%E7%AB%99%E5%AD%A6%E4%B9%A0%E5%B7%A5%E5%85%B7.meta.js// ==/UserScript==(function(){ 'use strict';//查看本地是否开启屏蔽varenableBlock =localStorage.getItem('enableBlock');if(enableBlock ==null){ enableBlock ='true';localStorage.setItem('enableBlock',enableBlock);}//绑定节点varnode1;varnode2;varnode3;varnode4;varnode5;varsearchWord;bindNode();//判断是否开启屏蔽varobserver =newMutationObserver((mutations)=>{ bindNode();blockRecommend();});;if(enableBlock =='true'){ observer.observe(document.body,{ childList:true,subtree:true});}//绑定快捷键:打开字幕“z”、宽屏播放“k”、页面全屏播放“y”、扩大/缩小选集框“l”、开启/关闭推荐屏蔽“t”varright_container_flag =0;document.addEventListener('keydown',function(event){ constsubtitleBtn =document.querySelector('.bpx-player-ctrl-subtitle .bpx-player-ctrl-btn-icon .bpx-common-svg-icon');constwideScreenBtn =document.querySelector('.bpx-player-ctrl-wide .bpx-player-ctrl-btn-icon .bpx-common-svg-icon');constfullScreenBtn =document.querySelector('.bpx-player-ctrl-web .bpx-player-ctrl-btn-icon .bpx-common-svg-icon');if(event.key ==='z'&&subtitleBtn){ subtitleBtn.click();}if(event.key ==='k'&&wideScreenBtn){ wideScreenBtn.click();}if(event.key ==='y'&&fullScreenBtn){ fullScreenBtn.click();}if(event.key ==='l'){ try{ if(right_container_flag ==0){ document.querySelector(".right-container").setAttribute("style","width: 600px;");try{ document.querySelector(".video-pod__body").setAttribute("style","max-height: 350px;");}catch{ }right_container_flag =1;}else{ document.querySelector(".right-container").setAttribute("style","width: 350px;");try{ document.querySelector(".video-pod__body").setAttribute("style","max-height: 250px;");}catch{ }right_container_flag =0;}}catch(error){ }}if(event.key ==='t'){ enableBlock =(enableBlock=='false')?'true':'false';localStorage.setItem('enableBlock',enableBlock);observer.disconnect();if(enableBlock =='true'){ blockRecommend();}else{ undoBlockRecommend();}}});//函数区===============================函数区//绑定节点functionbindNode(){ try{ node1 =document.querySelector('.bpx-player-ending-panel').querySelector('.bpx-player-ending-related');//视频结束后的视频推荐}catch(e){ }try{ node2 =document.querySelector('.recommend-list-v1');//侧边视频推荐栏}catch(e){ }try{ node3 =document.querySelector('#biliMainHeader');//顶部栏}catch(e){ }try{ node4 =document.querySelector('.pop-live-small-mode');//侧边直播框}catch(e){ }try{ node5 =document.querySelector(".feed2");//首页推荐}catch(e){ }}//清除所有推荐functionblockRecommend(){ try{ node1.style.display ="none";}catch(error){ }try{ node2.style.display ="none";}catch(error){ }try{ node4.style.display ="none";}catch(error){ }try{ varwords =node3.querySelector('.nav-search-input').getAttribute("placeholder");if(words !='')searchWord =words;node3.querySelector('.nav-search-input').setAttribute("placeholder",'');}catch(error){ }try{ node3.querySelector('.trending').style.display ="none";}catch(error){ }try{ node5.style.display ="none";}catch(error){ }}//添加所有推荐functionundoBlockRecommend(){ try{ node1.style.display ="block";}catch(error){ }try{ node2.style.display ="block";}catch(error){ }try{ node4.style.display ="none";}catch(error){ }try{ node3.querySelector('.nav-search-input').setAttribute("placeholder",searchWord);}catch(error){ }try{ node3.querySelector('.trending').style.display ="block";}catch(error){ }try{ node5.style.display ="block";}catch(error){ }}})();

提示:

自己使用其实有一年了,但由于电脑坏了脚本丢失,遂重写并上传。
首页搜索框没屏蔽,懒得加了。。。
然后其他地方的屏蔽可能出现失效的情况,这可能是页面加载速度影响。

【我要纠错】责任编辑:新华社