Stop Button for YouTube

A custom 'Stop Button' for YouTube video player, This allows you to end video (ad) or stop live stream by simply clicking on it.

当前为 2019-05-17 提交的版本,查看 最新版本

  1. /**
  2. BSD 2-Clause License
  3.  
  4. Copyright (c) 2019, KiranMurmuTH
  5. All rights reserved.
  6.  
  7. Redistribution and use in source and binary forms, with or without
  8. modification, are permitted provided that the following conditions are met:
  9.  
  10. 1. Redistributions of source code must retain the above copyright notice, this
  11. list of conditions and the following disclaimer.
  12.  
  13. 2. Redistributions in binary form must reproduce the above copyright notice,
  14. this list of conditions and the following disclaimer in the documentation
  15. and/or other materials provided with the distribution.
  16.  
  17. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  21. FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  23. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  24. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  25. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  26. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27.  
  28. **/
  29.  
  30. // ==UserScript==
  31. // @name Stop Button for YouTube
  32. // @namespace io.github.kiranmurmuth.user-script.stop-button-for-youtube
  33. // @version 1.2.3
  34. // @description A custom 'Stop Button' for YouTube video player, This allows you to end video (ad) or stop live stream by simply clicking on it.
  35. // @author KiranMurmuTH
  36. // @copyright 2019, KiranMurmuTH
  37. // @match *://www.youtube.com/*
  38. // @source https://github.com/kiranmurmuth/stop-button-for-youtube
  39. // @homepageURL https://kiranmurmuth.github.io/stop-button-for-youtube
  40. // @license BSD 2-Clause
  41. // @grant none
  42. // ==/UserScript==
  43.  
  44. var _ytp_thread_handler = 0;
  45. var _ytp_thread_interval = 250;
  46. var _ytp_thread_stopped = false;
  47. var _ytp_player_main_control = null;
  48. var _ytp_player_main_video = null;
  49. var _ytp_player_stop_button = null;
  50. var _ytp_player_create_button = null;
  51. var _ytp_player_left_control = null;
  52.  
  53. const _ytp_selector = {
  54. watch: 'ytd-player.style-scope.ytd-watch-flexy',
  55. channel: 'ytd-player.style-scope.ytd-channel-video-player-renderer',
  56. main_control: 'div#movie_player',
  57. main_video: 'video.video-stream.html5-main-video',
  58. hide_control: 'ytp-hide-controls',
  59. stop_button: 'button.ytp-stop-button.ytp-button',
  60. player_control: 'div.ytp-left-controls'
  61. };
  62.  
  63. const _ytp_button_property = {
  64. class_value: 'ytp-stop-button ytp-button',
  65. title_value: 'Stop or skip video',
  66. label_value: 'Stop or skip video',
  67. html5_value: '<svg height="100%" version="1.1" viewBox="0 0 36 36" width="100%"><use class="ytp-svg-shadow" xlink:href="#ytp-id-91">' +
  68. '</use><path class="ytp-svg-fill" d="M 12,12 v 12 h 12 V 12 h -2 z M 12,12 v 12 h 12 V 12 h -2 z" id="ytp-id-91"></path></svg>'
  69. };
  70.  
  71. function _ytp_player_main_thread() {
  72. if (_ytp_button_create(_ytp_selector.watch) || _ytp_button_create(_ytp_selector.channel)) {
  73. console.log("ytp-create-control-button: 'button-name: ytp-stop-button', status: 201 Created");
  74. }
  75. _ytp_thread_stopped = true;
  76. _ytp_thread_handler = 0;
  77. }
  78.  
  79. function _ytp_player_stop_video(_ytd_player_element) {
  80. _ytp_player_main_video = document.querySelector(_ytd_player_element + ' ' + _ytp_selector.main_video);
  81. if (typeof _ytp_player_main_video !== 'undefined' && _ytp_player_main_video) {
  82. _ytp_player_main_control = document.querySelector(_ytd_player_element + ' ' + _ytp_selector.main_control);
  83. if (typeof _ytp_player_main_control !== 'undefined' && _ytp_player_main_control) {
  84. if (_ytp_player_main_control.isAtLiveHead()) {
  85. _ytp_player_main_control.stopVideo();
  86. _ytp_player_main_control.classList.remove(_ytp_selector.hide_control);
  87. console.log("ytp-main-video-player: 'mode-name: ytp-unstarted-mode', status: 200 Ok");
  88. } else {
  89. _ytp_player_main_video.currentTime = _ytp_player_main_video.getDuration();
  90. _ytp_player_main_video.ended = true;
  91. _ytp_player_main_video.paused = true;
  92. console.log("ytp-main-video-player: 'mode-name: ytp-ended-mode', status: 200 Ok");
  93. }
  94. } else {
  95. console.log("ytp-main-video-player: 'mode-name: ytp-unstarted-mode', status: 404 Not Found");
  96. }
  97. } else {
  98. console.log("ytp-main-video-player: 'mode-name: ytp-ended-mode', status: 404 Not Found");
  99. }
  100. }
  101.  
  102. function _ytp_button_create(_ytd_player_element) {
  103. _ytp_player_stop_button = document.querySelector(_ytd_player_element + ' ' + _ytp_selector.stop_button);
  104. if (typeof _ytp_player_stop_button !== 'undefined' && !_ytp_player_stop_button) {
  105. _ytp_player_left_control = document.querySelector(_ytd_player_element + ' ' + _ytp_selector.player_control);
  106. if (typeof _ytp_player_left_control !== 'undefined' && _ytp_player_left_control) {
  107. _ytp_player_create_button = document.createElement('BUTTON');
  108. if (typeof _ytp_player_create_button !== 'undefined' && _ytp_player_create_button) {
  109. _ytp_player_create_button.setAttribute('class', _ytp_button_property.class_value);
  110. _ytp_player_create_button.setAttribute('title', _ytp_button_property.title_value);
  111. _ytp_player_create_button.setAttribute('aria-label', _ytp_button_property.label_value);
  112. _ytp_player_create_button.innerHTML = _ytp_button_property.html5_value;
  113. _ytp_player_create_button.addEventListener('click', function () { _ytp_player_stop_video(_ytd_player_element);});
  114. _ytp_player_left_control.appendChild(_ytp_player_create_button);
  115. _ytp_player_stop_button = document.querySelector(_ytd_player_element + ' ' + _ytp_selector.stop_button);
  116. if (typeof _ytp_player_stop_button !== 'undefined' && _ytp_player_stop_button) {
  117. return true;
  118. } else {
  119. return false;
  120. }
  121. } else {
  122. return false;
  123. }
  124. } else {
  125. return false;
  126. }
  127. } else {
  128. return false;
  129. }
  130. }
  131.  
  132. (function () {
  133. 'use strict';
  134. if (!_ytp_thread_stopped) {
  135. _ytp_thread_handler = setInterval(_ytp_player_main_thread, _ytp_thread_interval);
  136. }
  137. })();

QingJ © 2025

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