赞
踩
某次看b站的时候看了好长时间的视频
感觉要放弃 也不知道后面还有多少时长的视频等去看
就想知道自己看了多少 还剩下多少
于是我想搞一个玩意出来
(function getMsg(start=0) {
var timelist = document.querySelectorAll('#multi_page > div.cur-list > ul > li > a > div > div.duration')
var sum = 0
timelist.forEach((ele,index)=> {
if (index>start) {
min = ele.innerText.split(":")[0]
sum += Number(min)
}
})
//分钟
console.log("视频总分钟:"+sum)
//小时
console.log("视频总小时:"+Math.round(sum/60))
})()
参数空
输出如下
视频总分钟:1054
视频总小时:18
当传递参数 40
从参数 40开始到结束的视频长度
我觉得 这样还是有点 nb的样子 要是能直观的看到就好了
没问题
直接制造一个百分比的条子不就好了
开搞
(function getMsg(start=0) { var timelist = document.querySelectorAll('#multi_page > div.cur-list > ul > li > a > div > div.duration') var a=document.createElement("p") a.style.position="fixed" a.style.top="0" a.style.left="0" a.style.zIndex="99999999999" a.style.width="100%" a.style.height="14px" a.style.backgroundColor= '#4158D0'; a.style.backgroundImage='linear-gradient(43deg, #4158D0 0%, #C850C0 46%, #FFCC70 100%)'; var sp=document.createElement("span") a.appendChild(sp) sp.style.float="right" sp.style.width="50px" sp.style.height="14px" sp.style.color="#fff" sp.style.fontSize="10px" sp.style.lineHeight="14px" sp.style.borderRadius="2px" sp.style.backgroundImage='linear-gradient(160deg, #0093E9 0%, #80D0C7 100%)'; document.body.appendChild(a) var sum = 0 var current =0 timelist.forEach((ele,index)=> { if (index<=start) { min = ele.innerText.split(":")[0] current += Number(min) } min = ele.innerText.split(":")[0] sum += Number(min) var cur=current/sum a.style.width=100+"%" sp.style.width=(cur*100).toFixed(2)+"%" sp.innerText=(cur*100).toFixed(2)+"%" }) })(77)
77就是视频当前看的地方
效果
再度优化 现实更多信息
sp.innerText=(cur*100).toFixed(2)+"%"+"当前视频总时长"+sum+"当前已看"+current+"还剩时长"+(sum-current)
(function getMsg(start=0) { var timelist = document.querySelectorAll('#multi_page > div.cur-list > ul > li > a > div > div.duration') var a=document.createElement("p") a.style.position="fixed" a.style.top="0" a.style.left="0" a.style.zIndex="99999999999" a.style.width="100%" a.style.height="14px" a.style.backgroundColor= '#4158D0'; a.style.backgroundImage='linear-gradient(43deg, #4158D0 0%, #C850C0 46%, #FFCC70 100%)'; var sp=document.createElement("span") a.appendChild(sp) sp.style.float="right" sp.style.width="50px" sp.style.height="14px" sp.style.color="#fff" sp.style.fontSize="10px" sp.style.lineHeight="14px" sp.style.borderRadius="2px" sp.style.backgroundImage='linear-gradient(160deg, #0093E9 0%, #80D0C7 100%)'; document.body.appendChild(a) var sum = 0 var current =0 timelist.forEach((ele,index)=> { if (index<=start) { min = ele.innerText.split(":")[0] current += Number(min) } min = ele.innerText.split(":")[0] sum += Number(min) var cur=current/sum a.style.width=100+"%" sp.style.width=(cur*100).toFixed(2)+"%" sp.innerText=(cur*100).toFixed(2)+"%"+"当前视频总时长"+sum+"当前已看"+current+"还剩时长"+(sum-current) }) })(55)
这样子 感觉一依然还是不爽
怎么办呢
要不给每个按钮点击事件 点击就更新进度条
遇到问题 给原生li添加事件会报错
转换思路
获取其他数据 事件绑定到其他地方
var heads=document.querySelector(".head-con") undefined heads <div class="head-con">…</div>flex var curs=document.querySelector("li.on .page-num") undefined curs <span class="page-num">P4</span> var cursvalue = curs.innerText.split("P")[0] undefined cursvalue '' var cursvalue = curs.innerText.split("P")[1] undefined cursvalue '4'
于是就有了这样的事件函数
heads.onclick=function (params) { console.log("show") var sum = 0 var current =0 var curs=document.querySelector("li.on .page-num") var cursvalue = curs.innerText.split("P")[1] var sp=document.querySelector("body>p>span") var a=document.querySelector("body>p") timelist.forEach((ele,index)=> { if (index<=cursvalue ) { min = ele.innerText.split(":")[0] current += Number(min) } min = ele.innerText.split(":")[0] sum += Number(min) var cur=current/sum a.style.width=100+"%" sp.style.width=(cur*100).toFixed(2)+"%" sp.innerText=(cur*100).toFixed(2)+"%"+"当前视频总时长"+sum+"当前已看"+current+"还剩时长"+(sum-current) }) }
打开b站
找到视频资源 带有很长的那种集合视频
打开控制台
f12
开始操作了
站提如下代码
(function getMsg(start=0) { var timelist = document.querySelectorAll('#multi_page > div.cur-list > ul > li > a > div > div.duration') var a=document.createElement("p") a.style.position="fixed" a.style.top="0" a.style.left="0" a.style.zIndex="99999999999" a.style.width="100%" a.style.height="14px" a.style.backgroundColor= '#4158D0'; a.style.backgroundImage='linear-gradient(43deg, #4158D0 0%, #C850C0 46%, #FFCC70 100%)'; var sp=document.createElement("span") a.appendChild(sp) sp.style.float="right" sp.style.width="50px" sp.style.height="14px" sp.style.color="#fff" sp.style.fontSize="10px" sp.style.lineHeight="14px" sp.style.borderRadius="2px" sp.style.backgroundImage='linear-gradient(160deg, #0093E9 0%, #80D0C7 100%)'; document.body.appendChild(a) var sum = 0 var current =0 timelist.forEach((ele,index)=> { if (index<=start) { min = ele.innerText.split(":")[0] current += Number(min) } min = ele.innerText.split(":")[0] sum += Number(min) var cur=current/sum a.style.width=100+"%" sp.style.width=(cur*100).toFixed(2)+"%" sp.innerText=(cur*100).toFixed(2)+"%"+"当前视频总时长"+sum+"当前已看"+current+"还剩时长"+(sum-current) }) })(1)
将会显示一个比例条子
添加事件函数 如下代码
小提示
heads是事件绑定的地方
timelist 是视频每集的列表 从中获取数据
var heads=document.querySelector(".head-con") var timelist = document.querySelectorAll('#multi_page > div.cur-list > ul > li > a > div > div.duration') heads.onclick=function (params) { console.log("show") var sum = 0 var current =0 var curs=document.querySelector("li.on .page-num") var cursvalue = curs.innerText.split("P")[1] var sp=document.querySelector("body>p>span") var a=document.querySelector("body>p") timelist.forEach((ele,index)=> { if (index<=cursvalue-1 ) { min = ele.innerText.split(":")[0] current += Number(min) } min = ele.innerText.split(":")[0] sum += Number(min) var cur=current/sum a.style.width=100+"%" sp.style.width=(cur*100).toFixed(2)+"%" sp.innerText=(cur*100).toFixed(2)+"%"+"当前视频总时长"+sum+"当前已看"+current+"还剩时长"+(sum-current) }) }
改变一个集数试试
ok 目前基本可以完了
(function getMsg(start=0) { var timelist = document.querySelectorAll('#multi_page > div.cur-list > ul > li > a > div > div.duration') var a=document.createElement("p") a.style.position="fixed" a.style.top="0" a.style.left="0" a.style.zIndex="99999999999" a.style.width="100%" a.style.height="14px" a.style.backgroundColor= '#4158D0'; a.style.backgroundImage='linear-gradient(43deg, #4158D0 0%, #C850C0 46%, #FFCC70 100%)'; var sp=document.createElement("span") a.appendChild(sp) sp.style.float="right" sp.style.width="50px" sp.style.height="14px" sp.style.color="#fff" sp.style.fontSize="10px" sp.style.lineHeight="14px" sp.style.borderRadius="2px" sp.style.backgroundImage='linear-gradient(160deg, #0093E9 0%, #80D0C7 100%)'; document.body.appendChild(a) var sum = 0 var current =0 timelist.forEach((ele,index)=> { if (index<=start) { min = ele.innerText.split(":")[0] current += Number(min) } min = ele.innerText.split(":")[0] sum += Number(min) var cur=current/sum a.style.width=100+"%" sp.style.width=(cur*100).toFixed(2)+"%" sp.innerText=(cur*100).toFixed(2)+"%"+"当前视频总时长"+sum+"当前已看"+current+"还剩时长"+(sum-current) }) })(1) var heads=document.querySelector(".head-con") var timelist = document.querySelectorAll('#multi_page > div.cur-list > ul > li > a > div > div.duration') heads.onclick=function (params) { console.log("show") var sum = 0 var current =0 var curs=document.querySelector("li.on .page-num") var cursvalue = curs.innerText.split("P")[1] var sp=document.querySelector("body>p>span") var a=document.querySelector("body>p") timelist.forEach((ele,index)=> { if (index<=cursvalue-1 ) { min = ele.innerText.split(":")[0] current += Number(min) } min = ele.innerText.split(":")[0] sum += Number(min) var cur=current/sum a.style.width=100+"%" sp.style.width=(cur*100).toFixed(2)+"%" sp.innerText=(cur*100).toFixed(2)+"%"+"当前视频总时长"+sum+"当前已看"+current+"还剩时长"+(sum-current) }) }
实验测试
试用阶段
问题
经过某些此时发现 存在 需要优化的 代码
if (index<=cursvalue-1 ) { min = ele.innerText.split(":")[0] current += Number(min) } min = ele.innerText.split(":")[0] sum += Number(min) ele.innerText 是时间的值 可能是 1:05:12 小时 可能是 50:20 存在小时和分钟的情况 ele.innerText.split(":")[0] 只会截取第一个 1:05:12 取出来的是1 50:20 取出的是50 就需要加上判断了 后期如果想在精确比如到 秒 需要再度操作了
if (index<=cursvalue-1 ) { //解决 小时的问题 把小时变为 60分钟 //01 : 50: 20 == [ "01" , "50" , "20"] if(ele.innerText.split(":").length==3){ min = ele.innerText.split(":")[0]*60 current += Number(min) min1 = ele.innerText.split(":")[1] current += Number(min1 ) }else{ //取出分钟 min = ele.innerText.split(":")[0] current += Number(min) } } //同理 if(ele.innerText.split(":").length==3){ min = ele.innerText.split(":")[0]*60 sum += Number(min) min1 = ele.innerText.split(":")[1] sum += Number(min1 ) }else{ //取出分钟 min = ele.innerText.split(":")[0] sum += Number(min) }
(function getMsg(start=0) { var timelist = document.querySelectorAll('#multi_page > div.cur-list > ul > li > a > div > div.duration') var a=document.createElement("p") a.style.position="fixed" a.style.top="0" a.style.left="0" a.style.zIndex="99999999999" a.style.width="100%" a.style.height="14px" a.style.backgroundColor= '#4158D0'; a.style.backgroundImage='linear-gradient(43deg, #4158D0 0%, #C850C0 46%, #FFCC70 100%)'; var sp=document.createElement("span") a.appendChild(sp) sp.style.float="right" sp.style.width="50px" sp.style.height="14px" sp.style.color="#fff" sp.style.fontSize="10px" sp.style.lineHeight="14px" sp.style.borderRadius="2px" sp.style.backgroundImage='linear-gradient(160deg, #0093E9 0%, #80D0C7 100%)'; document.body.appendChild(a) var sum = 0 var current =0 timelist.forEach((ele,index)=> { if (index<=start) { min = ele.innerText.split(":")[0] current += Number(min) } min = ele.innerText.split(":")[0] sum += Number(min) var cur=current/sum a.style.width=100+"%" sp.style.width=(cur*100).toFixed(2)+"%" sp.innerText=(cur*100).toFixed(2)+"%"+"当前视频总时长"+sum+"当前已看"+current+"还剩时长"+(sum-current) }) })(1) var heads=document.querySelector(".head-con") var timelist = document.querySelectorAll('#multi_page > div.cur-list > ul > li > a > div > div.duration') heads.onclick=function (params) { console.log("show") var sum = 0 var current =0 var curs=document.querySelector("li.on .page-num") var cursvalue = curs.innerText.split("P")[1] var sp=document.querySelector("body>p>span") var a=document.querySelector("body>p") timelist.forEach((ele,index)=> { if (index<=cursvalue-1 ) { //解决 小时的问题 把小时变为 60分钟 //01 : 50: 20 == [ "01" , "50" , "20"] if(ele.innerText.split(":").length==3){ min = ele.innerText.split(":")[0]*60 current += Number(min) min1 = ele.innerText.split(":")[1] current += Number(min1 ) }else{ //取出分钟 min = ele.innerText.split(":")[0] current += Number(min) } } //同理 if(ele.innerText.split(":").length==3){ min = ele.innerText.split(":")[0]*60 sum += Number(min) min1 = ele.innerText.split(":")[1] sum += Number(min1 ) }else{ //取出分钟 min = ele.innerText.split(":")[0] sum += Number(min) } var cur=current/sum a.style.width=100+"%" sp.style.width=(cur*100).toFixed(2)+"%" sp.innerText=(cur*100).toFixed(2)+"%"+"当前视频总时长"+sum+"当前已看"+current+"还剩时长"+(sum-current) }) }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。