Auto Play Video

Automatically play videos on a specific page

  1. // ==UserScript==
  2. // @name Auto Play Video
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1
  5. // @description Automatically play videos on a specific page
  6. // @match https://elearning.tcsasac.com/#/home/myTrainingCourseList/*
  7. // @grant none
  8. // ==/UserScript==
  9.  
  10. (function() {
  11. 'use strict';
  12.  
  13. // 定义视频列表元素的选择器
  14. const videoListSelector = 'div.ant-list-item';
  15. // 定义视频 URL 属性
  16. const videoUrlAttribute = 'data-video-url';
  17.  
  18. // 检查页面 URL 是否匹配目标页面
  19. if (window.location.href.includes('/home/myTrainingCourseList/')) {
  20. // 获取视频列表元素
  21. const videoListItems = document.querySelectorAll(videoListSelector);
  22.  
  23. // 遍历视频列表,为每个视频添加点击事件
  24. videoListItems.forEach(function(item) {
  25. item.addEventListener('click', function() {
  26. // 获取视频 URL
  27. const videoUrl = this.getAttribute(videoUrlAttribute);
  28.  
  29. // 跳转到视频播放页面
  30. window.location.href = videoUrl;
  31. });
  32. });
  33.  
  34. // 自动播放下一个未观看的视频
  35. playNextUnwatchedVideo();
  36. } else if (window.location.href.includes('/home/myTrainingDetail/')) {
  37. // 如果在视频播放页面
  38. // 定义视频元素的选择器
  39. const videoSelector = 'video';
  40.  
  41. // 获取视频播放器元素
  42. const videoElement = document.querySelector(videoSelector);
  43.  
  44. if (videoElement) {
  45. // 检查视频是否已经在播放
  46. if (videoElement.paused) {
  47. // 开始播放视频
  48. videoElement.play();
  49. console.log('Video started playing');
  50. }
  51.  
  52. // 监听视频播放状态
  53. videoElement.addEventListener('timeupdate', function() {
  54. // 检查视频是否已播放完毕
  55. if (videoElement.currentTime >= videoElement.duration) {
  56. console.log('Video finished playing');
  57.  
  58. // 记录已观看的视频
  59. const currentVideoUrl = window.location.href;
  60. addWatchedVideo(currentVideoUrl);
  61.  
  62. // 自动播放下一个未观看的视频
  63. playNextUnwatchedVideo();
  64. }
  65. });
  66. } else {
  67. console.log('Video element not found on the page');
  68. }
  69. }
  70.  
  71. function playNextUnwatchedVideo() {
  72. // 获取所有视频 URL
  73. const videoUrls = Array.from(document.querySelectorAll(videoListSelector))
  74. .map(item => item.getAttribute(videoUrlAttribute));
  75.  
  76. // 获取已观看的视频 URL
  77. const watchedVideos = getWatchedVideos();
  78.  
  79. // 找到第一个未观看的视频 URL
  80. const unwatchedVideoUrl = videoUrls.find(url => !watchedVideos.includes(url));
  81.  
  82. if (unwatchedVideoUrl) {
  83. // 跳转到下一个未观看的视频
  84. window.location.href = unwatchedVideoUrl;
  85. } else {
  86. console.log('All videos have been watched.');
  87. }
  88. }
  89.  
  90. function addWatchedVideo(videoUrl) {
  91. // 从本地存储中获取已观看的视频列表
  92. let watchedVideos = getWatchedVideos();
  93.  
  94. // 添加新的已观看视频
  95. watchedVideos.push(videoUrl);
  96.  
  97. // 将更新后的列表存回本地存储
  98. localStorage.setItem('watchedVideos', JSON.stringify(watchedVideos));
  99. }
  100.  
  101. function getWatchedVideos() {
  102. // 从本地存储中获取已观看的视频列表
  103. const watchedVideos = JSON.parse(localStorage.getItem('watchedVideos')) || [];
  104. return watchedVideos;
  105. }
  106. })();

QingJ © 2025

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