视频倍速播放(追剧学习神器)

看视频播太慢,这能忍?直接倍速播放,【食用方法】①调节右上角加速框右侧上下按钮即可调节倍率 ②在右上角的加速框内输入加速倍率,如2、4、8、16等。【快捷键】:①单手快捷键:“x”,“c” 恢复正常播放:“t” ②双手快捷键:ctrl + 左右箭头

目前為 2021-12-23 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         视频倍速播放(追剧学习神器)
// @namespace    http://tampermonkey.net/
// @icon         https://img-blog.csdnimg.cn/20181221195058594.gif
// @version      1.1.7
// @description  看视频播太慢,这能忍?直接倍速播放,【食用方法】①调节右上角加速框右侧上下按钮即可调节倍率 ②在右上角的加速框内输入加速倍率,如2、4、8、16等。【快捷键】:①单手快捷键:“x”,“c” 恢复正常播放:“t”  ②双手快捷键:ctrl + 左右箭头 
// @author       wll
// @require      https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js
// @grant        GM_getValue
// @grant        GM_setValue
// @include      *:*
// @note         增加支持网站:	依照规则增加@match所在标签即可
// @note         郑重声明:	本脚本只做学习交流使用,未经作者允许,禁止转载,不得使用与非法用途,一经发现,追责到底
// @note         授权联系:	[email protected]
// @note         版本更新	20-12-26 1.0.0	初版发布视频倍速播放
// @note         版本更新	21-02-04 1.0.1 	优化用户体验
// @note         版本更新	21-02-04 1.0.2 	优化标题,优化简介
// @note         版本更新	21-06-18 1.0.3 	增加新的倍速网址,ehuixue.cn/index/study,ehuixue.cn/index/study,chaoxing.com
// @note         版本更新	21-06-25 1.0.4 	增加新的倍速网址,douyin.com
// @note         版本更新	21-06-26 1.0.5 	增加新的倍速网址,pan.baidu.com,youku.com
// @note         版本更新	21-07-09 1.0.6 	修正哔哩哔哩网站无法暂停问题
// @note         版本更新	21-10-11 1.0.7 	由于百度云视频倍速播放收费,一时无法解决,暂时停用百度相关加速*://*.pan.baidu.com/*
// @note         版本更新	21-12-11 1.0.8  感谢用户“何佳林”,提供建议,增加快捷键控制倍速 ctrl + ->  ctrl + <-
// @note         版本更新	21-12-13 1.0.9  增加cctv支持,增加倍速控件悬浮不跟随滑动
// @note         版本更新	21-12-14 1.1.0  增加倍率记忆功能,防止页面刷新倍率重新计算
// @note         版本更新	21-12-19 1.1.1  1、增加单手快捷键: “x” 、“c”, 2、增加寄存器倍率存储,浏览器全局使用 3、增加倍速框自动聚焦
// @note         版本更新	21-12-20 1.1.2  代码脚本优化
// @note         版本更新	21-12-20 1.1.3  增加全网倍速支持,让倍速不再有障碍
// @note         版本更新	21-12-21 1.1.4  增加快捷键d,用于恢复正常播放速度
// @note         版本更新	21-12-22 1.1.5  更改快捷键t,用于恢复正常播放速度
// @note         版本更新	21-12-23 1.1.6  修正倍速无法回到正常播放问题,感谢大佬“我不想上班”提供技术支持
// @note         版本更新	21-12-24 1.1.7  修正插件自带寄存器存储赋值失效问题

// ==/UserScript==

(function() {
	'use strict';

    document.onkeydown = function() {
		if (window.event.ctrlKey && (window.event.keyCode == 37)) {
			console.log("ctrl---");
			$("#rangeId").val(parseFloat(parseFloat($("#rangeId").val()) - 0.1 < 0.1 ? 0.1 : parseFloat($("#rangeId").val()) - 0.1).toFixed(1));
		}
		if (window.event.ctrlKey && (window.event.keyCode == 39)) {
		    console.log("ctrl+++");
			$("#rangeId").val(parseFloat(parseFloat($("#rangeId").val()) + 0.1 >  20 ?  20 : parseFloat($("#rangeId").val()) + 0.1).toFixed(1));
		}
	}

	document.addEventListener("keypress", function(e) {
	    console.log("--->e.key:"+e.key);
		switch (e.key) {
			case "x":
			    console.log("x---");
				$("#rangeId").val(parseFloat(parseFloat($("#rangeId").val()) - 0.1 < 0.1 ? 0.1 : parseFloat($("#rangeId").val()) - 0.1).toFixed(1));
				break;
			case "c":
			    console.log("c+++");
				$("#rangeId").val(parseFloat(parseFloat($("#rangeId").val()) + 0.1 >  20 ?  20 : parseFloat($("#rangeId").val()) + 0.1).toFixed(1));
				break;
			case "t":
			    console.log("t+++");
                $("#rangeId").val(1.0);
				localUtil.setSValue("speedStepKey", null);
				break;
		}
	});

	var localUtil = {
		getSValue(name) {
			return window.localStorage.getItem(name);
		},
		setSValue(name, value) {
			window.localStorage.setItem(name, value);
		},
		getGValue(name) {
			return window.GM_getValue(name);
		},
		setGValue(name, value) {
			window.GM_setValue(name, value);
		}
	}

    var main = {
		init() {
			$("body").prepend('<input id="rangeId" type="number" step="0.1" min="0.1" max="20" value="" autofocus="autofocus" style="z-index:999999;position:fixed;top:100px;right:100px;border:solid 1px;background-color:#E3EDCD;" />');
		},
		run() {
			var step = document.getElementById("rangeId").value;
			var htmlVideo = $("video").length;
			if (htmlVideo > 0) {
				console.log("倍速播放方法启动,当前倍率为....." + step);
				if (location.href.indexOf('bilibili.com/video/') > 0) {
					if (stopFlag) {
						//play video is auto
						document.querySelector('video').play();
						stopFlag = false;
					}
				}
                var speedStepKey = localUtil.getSValue("speedStepKey");
                if((step == null || step ==  '') && speedStepKey == null){
                    $("#rangeId").val(1);
                    document.querySelector('video').playbackRate = 1;
                    return;
                }
                if((step == null || step ==  '') && speedStepKey != null){
					localUtil.setSValue("speedStepKey", speedStepKey);
                    $("#rangeId").val(speedStepKey);
                    document.querySelector('video').playbackRate = speedStepKey;
                    return;
                }
                if(step != null && step !=  ''&& step != speedStepKey){
                    localUtil.setSValue("speedStepKey", step);
                    $("#rangeId").val(step);
                    document.querySelector('video').playbackRate = step;
                    return;
                }
			} else {
				console.log("当前视频不支持倍速播放..... o(╥﹏╥)o");
			}
		}
	}

	var stopFlag = true;
	var startStamp = new Date().getTime();
	window.initTimer = setInterval(() => {
		var videos = document.querySelectorAll("video").length;
		var nowStamp = new Date().getTime();
		if (videos > 0) {
			clearInterval(initTimer);
			main.init();
			window.setInterval(function() {main.run();}, 1000);
		} else if ((nowStamp - startStamp) > 15 * 1000) {
			clearInterval(initTimer);
		} else {
			console.log('search video waiting...');
		}
	}, 1000);

})();