B站视频时长计算器

计算B站视频列表中特定页码后的总时长

目前為 2025-01-25 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name B站视频时长计算器
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1
  5. // @description 计算B站视频列表中特定页码后的总时长
  6. // @author Your name
  7. // @match https://www.bilibili.com/video/*
  8. // @license MIT
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. // 创建一个浮动的结果显示框
  16. function createResultBox() {
  17. const box = document.createElement('div');
  18. box.style.cssText = `
  19. position: fixed;
  20. top: 20px;
  21. right: 20px;
  22. padding: 10px;
  23. background: rgba(0, 0, 0, 0.8);
  24. color: white;
  25. border-radius: 5px;
  26. z-index: 9999;
  27. font-size: 14px;
  28. display: none;
  29. `;
  30. document.body.appendChild(box);
  31. return box;
  32. }
  33.  
  34. // 将时长从MM:SS转换为秒
  35. function parseDuration(duration) {
  36. const [minutes, seconds] = duration.split(':').map(Number);
  37. return minutes * 60 + seconds;
  38. }
  39.  
  40. // 将总秒数转换为HH:MM:SS格式
  41. function formatDuration(totalSeconds) {
  42. const hours = Math.floor(totalSeconds / 3600);
  43. const minutes = Math.floor((totalSeconds % 3600) / 60);
  44. const seconds = totalSeconds % 60;
  45. return `${hours}:${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;
  46. }
  47.  
  48. // 创建计算按钮和输入框
  49. function createControls() {
  50. const container = document.createElement('div');
  51. container.style.cssText = `
  52. position: fixed;
  53. top: 10px;
  54. right: 10px;
  55. display: flex;
  56. gap: 10px;
  57. align-items: center;
  58. z-index: 9999;
  59. `;
  60.  
  61. // 创建页码输入框
  62. const input = document.createElement('input');
  63. input.type = 'number';
  64. input.placeholder = '起始页码';
  65. input.style.cssText = `
  66. width: 80px;
  67. padding: 5px;
  68. border: 1px solid #ccc;
  69. border-radius: 3px;
  70. `;
  71.  
  72. // 创建计算按钮
  73. const button = document.createElement('button');
  74. button.textContent = '计算时长';
  75. button.style.cssText = `
  76. padding: 5px 10px;
  77. background: #00A1D6;
  78. color: white;
  79. border: none;
  80. border-radius: 3px;
  81. cursor: pointer;
  82. `;
  83.  
  84. container.appendChild(input);
  85. container.appendChild(button);
  86. document.body.appendChild(container);
  87. return { button, input };
  88. }
  89.  
  90. // 主要计算逻辑
  91. function calculateTotalDuration(startPage) {
  92. // 针对B站视频列表的选择器
  93. const videoItems = document.querySelectorAll('.list-box li');
  94. let totalSeconds = 0;
  95.  
  96. videoItems.forEach(item => {
  97. // 获取页码(根据实际DOM结构调整选择器)
  98. const pageNumElement = item.querySelector('.page-num');
  99. // 获取时长(根据实际DOM结构调整选择器)
  100. const durationElement = item.querySelector('.duration');
  101.  
  102. if (pageNumElement && durationElement) {
  103. const pageNum = parseInt(pageNumElement.textContent.replace(/[^0-9]/g, ''));
  104. // 只计算指定页码之后的时长
  105. if (pageNum > startPage) {
  106. totalSeconds += parseDuration(durationElement.textContent.trim());
  107. }
  108. }
  109. });
  110.  
  111. return formatDuration(totalSeconds);
  112. }
  113.  
  114. // 初始化插件
  115. function init() {
  116. const resultBox = createResultBox();
  117. const { button, input } = createControls();
  118.  
  119. button.addEventListener('click', () => {
  120. const startPage = parseInt(input.value) || 0;
  121. const totalDuration = calculateTotalDuration(startPage);
  122. resultBox.textContent = `${startPage}集之后的总时长: ${totalDuration}`;
  123. resultBox.style.display = 'block';
  124.  
  125. // 5秒后自动隐藏结果
  126. setTimeout(() => {
  127. resultBox.style.display = 'none';
  128. }, 5000);
  129. });
  130.  
  131. // 鼠标悬停时显示结果
  132. button.addEventListener('mouseover', () => {
  133. if (resultBox.textContent) {
  134. resultBox.style.display = 'block';
  135. }
  136. });
  137.  
  138. // 鼠标离开时隐藏结果
  139. button.addEventListener('mouseout', () => {
  140. resultBox.style.display = 'none';
  141. });
  142. }
  143.  
  144. // 等待页面加载完成后初始化
  145. window.addEventListener('load', init);
  146. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址