Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name YouTube English/Thai Caption Switcher HOPEFULLY THIS WORKS
- // @namespace http://tampermonkey.net/
- // @version 1.1
- // @description Switch between English and auto-generated Thai captions on YouTube
- // @author Your name
- // @match https://www.youtube.com/*
- // @match https://youtube.com/*
- // @grant none
- // ==/UserScript==
- (function() {
- 'use strict';
- // Style for our caption switcher
- const style = document.createElement('style');
- style.textContent = `
- .caption-switcher {
- position: fixed;
- bottom: 120px;
- right: 20px;
- background: rgba(33, 33, 33, 0.9);
- padding: 8px;
- border-radius: 8px;
- z-index: 9999;
- display: flex;
- flex-direction: column;
- gap: 6px;
- font-family: YouTube Sans, sans-serif;
- box-shadow: 0 2px 8px rgba(0,0,0,0.2);
- }
- .caption-button {
- background: #ffffff;
- border: none;
- padding: 8px 12px;
- border-radius: 4px;
- cursor: pointer;
- color: #030303;
- font-size: 14px;
- font-weight: 500;
- transition: all 0.2s;
- min-width: 100px;
- text-align: center;
- }
- .caption-button:hover {
- background: #e5e5e5;
- }
- .caption-button.active {
- background: #065fd4;
- color: white;
- }
- .caption-button:active {
- transform: scale(0.98);
- }
- `;
- document.head.appendChild(style);
- // Function to set caption language
- function setCaption(languageCode, isAuto = false) {
- const player = document.querySelector('.html5-video-player');
- if (!player) return;
- const playerApi = player.wrappedJSObject || player;
- if (playerApi && playerApi.setOption) {
- // For auto-generated captions, we need to specify the trackName
- const track = {
- languageCode: languageCode,
- // For Thai auto-generated captions
- kind: isAuto ? 'asr' : 'captions'
- };
- // Turn on captions with specified settings
- playerApi.setOption('captions', 'track', track);
- // Make sure captions are displayed
- setTimeout(() => {
- if (!playerApi.getOption('captions', 'track')) {
- playerApi.toggleSubtitles();
- }
- }, 100);
- // Update button states
- updateButtonStates(languageCode);
- }
- }
- // Function to update button active states
- function updateButtonStates(activeLanguage) {
- document.querySelectorAll('.caption-button').forEach(button => {
- button.classList.remove('active');
- if (button.dataset.lang === activeLanguage) {
- button.classList.add('active');
- }
- });
- }
- // Create and inject the caption switcher UI
- function createSwitcher() {
- const switcher = document.createElement('div');
- switcher.className = 'caption-switcher';
- // English button
- const engButton = document.createElement('button');
- engButton.className = 'caption-button';
- engButton.textContent = 'πΊπΈ English';
- engButton.dataset.lang = 'en';
- engButton.onclick = () => setCaption('en', false);
- switcher.appendChild(engButton);
- // Thai auto-generated button
- const thaiButton = document.createElement('button');
- thaiButton.className = 'caption-button';
- thaiButton.textContent = 'πΉπ Thai (Auto)';
- thaiButton.dataset.lang = 'th';
- thaiButton.onclick = () => setCaption('th', true);
- switcher.appendChild(thaiButton);
- document.body.appendChild(switcher);
- }
- // Function to initialize the script
- function init() {
- if (document.querySelector('.html5-video-player')) {
- if (!document.querySelector('.caption-switcher')) {
- createSwitcher();
- }
- } else {
- // If player isn't loaded yet, wait and try again
- setTimeout(init, 1000);
- }
- }
- // Watch for navigation changes (for single-page app navigation)
- const observer = new MutationObserver((mutations) => {
- if (window.location.href.includes('watch')) {
- init();
- }
- });
- observer.observe(document.documentElement, {
- childList: true,
- subtree: true
- });
- // Initial setup
- init();
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement