Advertisement
geraldandy

YouTube English/Thai Caption Switcher HOPEFULLY THIS WORKS

Nov 27th, 2024
8
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.71 KB | None | 0 0
  1. // ==UserScript==
  2. // @name YouTube English/Thai Caption Switcher HOPEFULLY THIS WORKS
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.1
  5. // @description Switch between English and auto-generated Thai captions on YouTube
  6. // @author Your name
  7. // @match https://www.youtube.com/*
  8. // @match https://youtube.com/*
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. // Style for our caption switcher
  16. const style = document.createElement('style');
  17. style.textContent = `
  18. .caption-switcher {
  19. position: fixed;
  20. bottom: 120px;
  21. right: 20px;
  22. background: rgba(33, 33, 33, 0.9);
  23. padding: 8px;
  24. border-radius: 8px;
  25. z-index: 9999;
  26. display: flex;
  27. flex-direction: column;
  28. gap: 6px;
  29. font-family: YouTube Sans, sans-serif;
  30. box-shadow: 0 2px 8px rgba(0,0,0,0.2);
  31. }
  32. .caption-button {
  33. background: #ffffff;
  34. border: none;
  35. padding: 8px 12px;
  36. border-radius: 4px;
  37. cursor: pointer;
  38. color: #030303;
  39. font-size: 14px;
  40. font-weight: 500;
  41. transition: all 0.2s;
  42. min-width: 100px;
  43. text-align: center;
  44. }
  45. .caption-button:hover {
  46. background: #e5e5e5;
  47. }
  48. .caption-button.active {
  49. background: #065fd4;
  50. color: white;
  51. }
  52. .caption-button:active {
  53. transform: scale(0.98);
  54. }
  55. `;
  56. document.head.appendChild(style);
  57.  
  58. // Function to set caption language
  59. function setCaption(languageCode, isAuto = false) {
  60. const player = document.querySelector('.html5-video-player');
  61. if (!player) return;
  62.  
  63. const playerApi = player.wrappedJSObject || player;
  64.  
  65. if (playerApi && playerApi.setOption) {
  66. // For auto-generated captions, we need to specify the trackName
  67. const track = {
  68. languageCode: languageCode,
  69. // For Thai auto-generated captions
  70. kind: isAuto ? 'asr' : 'captions'
  71. };
  72.  
  73. // Turn on captions with specified settings
  74. playerApi.setOption('captions', 'track', track);
  75.  
  76. // Make sure captions are displayed
  77. setTimeout(() => {
  78. if (!playerApi.getOption('captions', 'track')) {
  79. playerApi.toggleSubtitles();
  80. }
  81. }, 100);
  82.  
  83. // Update button states
  84. updateButtonStates(languageCode);
  85. }
  86. }
  87.  
  88. // Function to update button active states
  89. function updateButtonStates(activeLanguage) {
  90. document.querySelectorAll('.caption-button').forEach(button => {
  91. button.classList.remove('active');
  92. if (button.dataset.lang === activeLanguage) {
  93. button.classList.add('active');
  94. }
  95. });
  96. }
  97.  
  98. // Create and inject the caption switcher UI
  99. function createSwitcher() {
  100. const switcher = document.createElement('div');
  101. switcher.className = 'caption-switcher';
  102.  
  103. // English button
  104. const engButton = document.createElement('button');
  105. engButton.className = 'caption-button';
  106. engButton.textContent = 'πŸ‡ΊπŸ‡Έ English';
  107. engButton.dataset.lang = 'en';
  108. engButton.onclick = () => setCaption('en', false);
  109. switcher.appendChild(engButton);
  110.  
  111. // Thai auto-generated button
  112. const thaiButton = document.createElement('button');
  113. thaiButton.className = 'caption-button';
  114. thaiButton.textContent = 'πŸ‡ΉπŸ‡­ Thai (Auto)';
  115. thaiButton.dataset.lang = 'th';
  116. thaiButton.onclick = () => setCaption('th', true);
  117. switcher.appendChild(thaiButton);
  118.  
  119. document.body.appendChild(switcher);
  120. }
  121.  
  122. // Function to initialize the script
  123. function init() {
  124. if (document.querySelector('.html5-video-player')) {
  125. if (!document.querySelector('.caption-switcher')) {
  126. createSwitcher();
  127. }
  128. } else {
  129. // If player isn't loaded yet, wait and try again
  130. setTimeout(init, 1000);
  131. }
  132. }
  133.  
  134. // Watch for navigation changes (for single-page app navigation)
  135. const observer = new MutationObserver((mutations) => {
  136. if (window.location.href.includes('watch')) {
  137. init();
  138. }
  139. });
  140.  
  141. observer.observe(document.documentElement, {
  142. childList: true,
  143. subtree: true
  144. });
  145.  
  146. // Initial setup
  147. init();
  148. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement