学习利器,借助Tampermonkey写一个B站视频加速器脚本

借助Tampermonkey写一个B站视频加速器脚本

如果你会JavaScript,你可以借助Tampermonkey实现对网页的控制。在播放B站视频时,作为学习党可能你会需要3倍速或者更高的倍速来观看视频,有这个脚本点击一次你能增加0.5倍速。(在firefox浏览器下进行演示,Chrome浏览器基本一致)以后的文章我会讲解一下这个脚本主要都做了写什么,为什么这么做,有兴趣的你可以关注一下。
脚本解析文章链接:一份B站视频加速器脚本原理请查收

下载Tampermonkey插件

下载地址:https://addons.mozilla.org/zh-CN/firefox/addon/tampermonkey/

编辑加速器脚本

选择插件,找到添加新脚本到插件就可以粘贴代码
在这里插入图片描述

效果

在这里插入图片描述

完整代码

// ==UserScript==
// @name         B站视频加速器
// @namespace    http://dragon.net/
// @version      0.1
// @description  提升b站视频播放速度
// @author       Dragon
// @match        *://www.bilibili.com/*
// @match        *://pan.baidu.com/*
// @require      https://cdn.jsdelivr.net/npm/jquery@3.2.1/dist/jquery.min.js
// @grant        unsafeWindow
// @grant        GM_addStyle(css)
// ==/UserScript==

(function() {
    'use strict';
    var speed = document.createElement("div");//创建一个控制的div标签
    speed.innerText = "1X";
    speed.style.position = "fixed";
    speed.style.backgroundColor = "rgba(253, 102, 1, 0.5)";
    speed.style.width = "60px";
    speed.style.height = "100px";
    speed.style.top = "250px";
    speed.style.right = "0px";
    speed.style.borderRadius = "15px 0px 0px 15px";
    speed.style.fontSize = "24px";
    speed.style.display = "flex";
    speed.style.alignItems = "center";
    speed.style.justifyContent = "center";
    speed.style.userSelect = "none";
    speed.style.zindex = 99999999;
    document.body.appendChild(speed);
    $(speed).css({"z-index":99999999});
    play(1);
    setInterval(function(){
        var str = sessionStorage.getItem("bilibili_player_settings");
        var j = JSON.parse(str);
        var s =j.video_status.videospeed;
        speed.innerText = s+"X";
    },1000);
    speed.onmouseover = function(){
        speed.style.backgroundColor = "rgba(253, 102, 1, 1)";
        speed.onclick = function () {//设置播放速度
            var vd = document.getElementsByTagName("video");
            var str = sessionStorage.getItem("bilibili_player_settings");
            var j = JSON.parse(str);
            var s =j.video_status.videospeed;
            if(s >5){
                s = 1;
            }else{
                s +=0.5;
            }
            vd[0].playbackRate =s;
            speed.innerText = s+"X";
            play(s);
        }
    }

    speed.onmouseout = function(){
        speed.style.backgroundColor = "rgba(253, 102, 1, 0.5)";
    }
    function play(s){
        var str = sessionStorage.getItem("bilibili_player_settings");
        var j = JSON.parse(str);
        console.log(str);
        j.video_status.videospeed = s;
        var nstr = JSON.stringify(j);
        console.log(nstr);
        sessionStorage.setItem("bilibili_player_settings",nstr);
    }

})();
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值