YouTube 4x Speed

Add a 4x speed option to YouTube player.

目前为 2024-09-06 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name YouTube 4x Speed
  3. // @namespace https://www.youtube.com/
  4. // @version 1.0
  5. // @description Add a 4x speed option to YouTube player.
  6. // @author YourName
  7. // @match https://www.youtube.com/*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13.  
  14. function addSpeedOption() {
  15. const player = document.querySelector('video');
  16. const speedMenu = document.querySelector('.ytp-settings-menu');
  17. if (player && speedMenu) {
  18. const existingSpeeds = player.playbackRate;
  19. // Add the 4x speed option to the playback rate menu if it doesn't already exist
  20. if (!Array.from(document.querySelectorAll('.ytp-menuitem')).some(el => el.innerText.includes('4'))) {
  21. const speedOption = document.createElement('div');
  22. speedOption.className = 'ytp-menuitem';
  23. speedOption.setAttribute('role', 'menuitem');
  24. speedOption.setAttribute('aria-checked', 'false');
  25. speedOption.setAttribute('tabindex', '0');
  26. const speedLabel = document.createElement('div');
  27. speedLabel.className = 'ytp-menuitem-label';
  28. speedLabel.innerText = '4x';
  29. speedOption.appendChild(speedLabel);
  30. speedOption.addEventListener('click', function() {
  31. player.playbackRate = 4.0;
  32. closeSpeedMenu();
  33. });
  34.  
  35. speedMenu.appendChild(speedOption);
  36. }
  37. }
  38. }
  39.  
  40. function closeSpeedMenu() {
  41. // Close the YouTube settings menu
  42. const settingsButton = document.querySelector('.ytp-settings-button');
  43. settingsButton.click();
  44. }
  45.  
  46. function observerCallback() {
  47. addSpeedOption();
  48. }
  49.  
  50. // Mutation observer to ensure the script runs even after navigating to a different video
  51. const observer = new MutationObserver(observerCallback);
  52. observer.observe(document, { childList: true, subtree: true });
  53. })();