当前位置: 首页 > news >正文

b站PC网页版视频播放页油猴小插件制作

文章目录

  • 前言
  • 需求分析
  • 实施
    • 观察页面起始渲染
    • 编码
    • 效果展示
  • 总结


前言

新手上路,欢迎指导

需求分析

想要一个简约干净的界面,需要去除推荐栏和广告部分.
想要自由调节视频播放速率,需要在视频控制栏加一个输入框控制视频倍速

实施

观察页面起始渲染

因为要使用MutationObserver监控元素,所以需要确认页面开始渲染了哪些东西

  • 打个断点:
    在这里插入图片描述
  • 刷新:

在这里插入图片描述

经观察(需要动手看结果)发现,页面基本框架已经渲染完成了,侧边的推荐和广告,顶部左侧的导航栏都有了.
缺少中间的搜索框,视频控件,底部评论

这里怎么清理全靠喜好:
左上导航栏不喜欢,留一个首页,
右侧广告和推荐都不喜欢,去除不要

编码

清理的基本思路就是找到不要的元素的类名或id,创建个样式直接display: none !important,有要保留的就父元素display: none再把个别要保留的部分换回原来的显示属性,不行就不要的一一display: none !important
插入搜索框拿到值改下video的playbackRate即可

  1. 准备一个插入css的函数.
function addNewStyle(newStyle) {let styleElement = document.getElementById('mystyles');if (!styleElement) {styleElement = document.createElement('style');styleElement.type = 'text/css';styleElement.id = 'mystyles';document.getElementsByTagName('head')[0].appendChild(styleElement);}styleElement.appendChild(document.createTextNode(newStyle));
}
  1. 预定义全局样式
const cssVars = {hide_force: 'display: none !important',hide: 'display: none',show_as_item: 'display: list-item'
};
  1. 在window加载时加入逻辑监听body插入元素时去掉左上导航除图标首页以外的部分,就完成了.
    不太放心又加了个定时器来去除之前遇到的其他广告.
    评论区是一个单独的组件,封装在shadow DOM里,所以直接用定时器解决了.
//二次初始化页面
const observerInto = new MutationObserver(function () {const cssConfig = {hiddenList: {selectors: ['.ad-report', '.recommend-list-v1', '.slide-ad-exp'],props: cssVars.hide_force},headerCleanup: {selectors: ['#biliMainHeader .left-entry li'],props: cssVars.hide},headerRetain: {selectors: ['#biliMainHeader .left-entry li:first-child'],props: cssVars.show_as_item,}};addNewStyle(compileCSS(cssConfig))observerInto.disconnect()
})
observerInto.observe(document.body, {childList: true, subtree: true})
//去除其他广告
setTimeout(function () {const cssConfig = {hiddenAds: {selectors: ['.vcd', '.video-card-ad-small', '.activity-m-v1', '.act-end'],props: cssVars.hide_force}};addNewStyle(compileCSS(cssConfig));}, 2000);
//去除评论区notice公告
setTimeout(function () {const shadowHost = document.querySelector('bili-comments').shadowRoot.querySelector('bili-comments-header-renderer');// 获取 Shadow Rootconst shadowRoot = shadowHost.shadowRoot;// 如果 Shadow DOM 是开放的(open),可以直接访问if (shadowRoot) {// 在 Shadow DOM 中查找元素const noticeElement = shadowRoot.querySelector('#notice');console.log(noticeElement)if (noticeElement) {noticeElement.setAttribute('style', 'display: none !important');}} else {console.log('Shadow DOM 是封闭的(closed),无法访问');}
}, 6000);
  1. 监听搜索框值的变化,改成喜欢的值(需要注意,点击搜索还是搜索原来的内容)
const observerSearchBar = new MutationObserver(function () {const input = document.querySelector('#nav-searchform input')if (input && input.placeholder && input.placeholder !== '好好学习,天天向上') {input.placeholder = '好好学习,天天向上'input.title = '好好学习,天天向上'}
})
const centerSearchContainer = document.querySelector('#biliMainHeader');
observerSearchBar.observe(centerSearchContainer, {subtree: true,attributes: true,attributeFilter: ['placeholder']
})
  1. 监听视频控件渲染,插入自定义输入框
const playerContralBottom = document.querySelector(".bpx-player-control-bottom-right");
const observerContralBottom = new MutationObserver(function () {if (playerContralBottom.children.length >= 2) {const input = document.createElement('input');Object.assign(input, {type: 'text',maxLength: 4,placeholder: '✍️ 播放倍率',style: `
width: 72px;
height: 24px;
padding: 2px 0 2px 4px;
font-size: 12px;
font-weight: bold;
background: linear-gradient(45deg, #f3ec78, #af4261);
border: 1px solid #fff;
border-radius: 15px;
box-shadow: 0 4px 15px rgba(175,66,97,0.3);
color: #fff;
text-shadow: 1px 1px 2px rgba(0,0,0,0.3);
transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
`});input.addEventListener('focus', () => {input.style.transform = 'scale(1.05)';input.style.boxShadow = '0 8px 25px rgba(175,66,97,0.4)';input.style.borderColor = '#ffd700';});input.addEventListener('blur', () => {input.style.transform = 'scale(1)';input.style.boxShadow = '0 4px 15px rgba(175,66,97,0.3)';input.style.borderColor = '#fff';});
//终止监听,避免插入监听死循环observerContralBottom.disconnect()playerContralBottom.insertBefore(input, playerContralBottom.children[1]);input.addEventListener('input', function () {// 输入警告效果if (this.value.length === 4) {this.style.background = 'linear-gradient(45deg, #af4261, #f3ec78)';setTimeout(() => {this.style.transform = 'translateX(5px)';setTimeout(() => {this.style.transform = 'translateX(-5px)'}, 50);}, 0);} else {this.style.transform = 'scale(1.05)';this.style.background = 'linear-gradient(45deg, #f3ec78, #af4261)';}});//核心逻辑在这里input.addEventListener('keydown', function (event) {if (event.key === 'Enter') {//console.log('你按下了回车键,当前输入的值是:', this.value);const regex = /\d+(\.\d+)?/if (!regex.test(this.value)) {alert("输入值非法");this.value = ''return;}const video = document.querySelector("video");let rate = parseFloat(this.value).toFixed(2);rate = rate < 0.1 ? (() => {alert("不能低于0.1倍速!已调整为0.1")return 0.1})() : rate > 16 ? (() => {alert("不能高于16倍速!已调整为16")return 16})() : ratevideo.playbackRate = ratethis.value = ''}});}
})
observerContralBottom.observe(playerContralBottom, {childList: true, subtree: true})

效果展示

在这里插入图片描述

总结

主要就用到MutationObserver和定时器,以及video.playbackRate

输入框那一大段基本是deepseek生成的,加了些样式和动画.

{childList: true, subtree: true,attributes: true, attributeFilter: ['placeholder']}
用到了四个选项

  • 监听自身或子节点插入
  • 监听整个子树
  • 监听属性变化
  • 要监听的属性值(默认不加全监听)

相关文章:

  • 钧瓷收藏防坑指南:如何科学评估与理性收藏
  • Flink介绍——实时计算核心论文之Kafka论文详解
  • MCP认证难题破解
  • 文件上传Ⅰ
  • pgsql中使用jsonb的mybatis-plus和jps的配置
  • 微信小程序调用yolo目标检测模型
  • 仿腾讯会议项目开发——网络嵌入
  • AWS Elastic Beanstalk的部署Python Flask后端服务(Hello,World)
  • Hadoop的三大结构及其作用?
  • 计算机基础 | 常见进制与单位简介 / 表示 / 描述
  • 医疗行业如何构建合成数据平台?——技术、合规与实践全景
  • 数据结构-Map和Set
  • 第 8 期:条件生成 DDPM:让模型“听话”地画图!
  • 元宇宙概念兴起,B 端数字孪生迎来哪些新机遇?
  • 考研408第一章计算机系统概述——1.1-1.2操作系统的基本概念与发展历程
  • java基础从入门到上手(九):Java - List、Set、Map
  • Java并发编程高频面试题(已整理Java面试宝典PDF完整版)
  • 笔记整理五
  • Scrapeless Scraping Browser: A high-concurrency automation solution for AI
  • 【C++深入系列】:模版详解(上)
  • 印控克什米尔恐袭引爆印巴新一轮外交摩擦,地区安全风险骤增
  • 上海开展2025年“人民城市 文明风采”群众性主题活动
  • 世卫发布预防少女怀孕新指南,呼吁终止童婚、延长女孩受教育时间
  • 阻燃材料点火就着引发一场火灾,河北一企业的产品被指不达标且涉嫌欺诈
  • 福特中国CFO:依然坚信中国市场,上海帮助公司吸引到人才
  • 明日出征!航天员详细信息来啦