Stop all videos

Stops all videos on the page

当前为 2024-02-04 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Stop all videos
  3. // @namespace Stop all videos
  4. // @version 1.1
  5. // @description Stops all videos on the page
  6. // @author Nameniok
  7. // @match *://*/*
  8. // @license MIT
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14. const config = {
  15. blockVideoPreload: true, // Stop video loading
  16. blockAutoplay: true, // Stop automatic playback
  17. };
  18.  
  19. function stopAndDisablePreload(video) {
  20. video.pause();
  21. video.removeAttribute('preload');
  22. }
  23. function stopAndDisablePreloadForAllVideos() {
  24. Array.from(document.querySelectorAll('video')).forEach(video => {
  25. stopAndDisablePreload(video);
  26. addCanPlayListener(video);
  27. applyAdditionalSettings(video);
  28.  
  29. video.addEventListener('loadedmetadata', () => {
  30. stopAndDisablePreload(video);
  31. addCanPlayListener(video);
  32. applyAdditionalSettings(video);
  33. });
  34. });
  35. }
  36. function addCanPlayListener(video) {
  37. video.addEventListener('canplay', () => {
  38. stopAndDisablePreload(video);
  39. }, { once: true });
  40.  
  41. video.addEventListener('loadedmetadata', () => {
  42. stopAndDisablePreload(video);
  43. }, { once: true });
  44. }
  45. function observeVideos(mutationsList) {
  46. mutationsList.forEach(mutation => {
  47. if (mutation.type === 'childList' && mutation.addedNodes.length > 0) {
  48. mutation.addedNodes.forEach(node => {
  49. if (node.tagName && node.tagName.toLowerCase() === 'video') {
  50. if (config.blockVideoPreload) {
  51. stopAndDisablePreload(node);
  52. }
  53. if (config.blockAutoplay) {
  54. addCanPlayListener(node);
  55. }
  56. applyAdditionalSettings(node);
  57. } else if (node.querySelectorAll) {
  58. Array.from(node.querySelectorAll('video')).forEach(video => {
  59. if (config.blockVideoPreload) {
  60. stopAndDisablePreload(video);
  61. }
  62. if (config.blockAutoplay) {
  63. addCanPlayListener(video);
  64. }
  65. applyAdditionalSettings(video);
  66. });
  67. }
  68. });
  69. }
  70. });
  71. }
  72. function initObserver() {
  73. const observer = new MutationObserver(observeVideos);
  74. const targetNode = document.body;
  75.  
  76. const observerConfig = {
  77. childList: true,
  78. subtree: true
  79. };
  80. observer.observe(targetNode, observerConfig);
  81. }
  82. window.addEventListener('load', () => {
  83. stopAndDisablePreloadForAllVideos();
  84. initObserver();
  85. });
  86. })();