Youtube - Subtitle (PC Version)

Automatically enable the subtitles.

安装此脚本?
作者推荐脚本

您可能也喜欢Youtube - Subtitle

安装此脚本
  1. // ==UserScript==
  2. // @name Youtube - Subtitle (PC Version)
  3. // @namespace https://gf.qytechs.cn/en/users/670188-hacker09?sort=daily_installs
  4. // @version 1
  5. // @description Automatically enable the subtitles.
  6. // @author hacker09
  7. // @match https://*.youtube.com/embed/*
  8. // @match https://www.youtube.com/watch?v=*
  9. // @icon https://www.youtube.com/s/desktop/03f86491/img/favicon.ico
  10. // @require https://unpkg.com/tesseract.js/dist/tesseract.min.js
  11. // @run-at document-end
  12. // @grant none
  13. // ==/UserScript==
  14.  
  15. (async function() {
  16. 'use strict';
  17. var video = document.querySelector('video'); // Select the video element on the page
  18. var SubsNotFound = true; // Tesseract has not recognized any subtitles yet
  19. var lastCaptureTime = 0; // Variable to store the last captured frame time
  20.  
  21. video.addEventListener('playing', () => { // When the video is played
  22. document.querySelector(".ytp-subtitles-button.ytp-button").innerHTML.match('unavailable') === null && document.querySelector(".ytp-subtitles-button.ytp-button").ariaPressed === 'false' ? document.querySelector(".ytp-subtitles-button.ytp-button").click() : ''; // Auto enable the subtitles if available and they are disabled
  23. }, { once: true }); // Finishes the timeupdate event listener
  24.  
  25. video.addEventListener('timeupdate', async () => { // When the video is playing
  26. lastCaptureTime = lastCaptureTime >= video.currentTime ? video.currentTime : lastCaptureTime; //Create a new variable to capture the last 1 sec, and fix the variable if the user seeks backwards
  27. if (video.currentTime - lastCaptureTime >= 1 && SubsNotFound && video.currentTime < 60 && document.querySelector(".ytp-subtitles-button.ytp-button").innerHTML.match('unavailable') === null) { //If 1 sec passed since last Tesseract check, if Tesseract didn't recognize any subs on the last try and if less than 1 min passed
  28. lastCaptureTime = video.currentTime; //Update the lastCaptureTime variable
  29. var currentSubtitle = await Tesseract.recognize(captureFrame(video), 'por+eng+spa'); // Capture a frame from the video and use Tesseract to recognize subtitles in Portuguese, English, and Spanish
  30. if (currentSubtitle.data.confidence > 80 && !['VERIFIQUE', 'CLASSIFICAÇÃO'].some(text => currentSubtitle.data.text.includes(text)) && document.querySelector(".ytp-subtitles-button.ytp-button").ariaPressed === 'true') { // If the subtitle confidence is above 85%, does not contain specific ignored texts, the subs are enabled and the subtitles are available
  31. SubsNotFound = false; // Tesseract has recognized subtitles
  32. document.querySelector(".ytp-subtitles-button.ytp-button").click(); // Disable the subtitles
  33. } // Finishes the if condition
  34. } // Finishes the if condition
  35. }); // Finishes the timeupdate event listener
  36.  
  37. function captureFrame(video) { // Starts the captureFrame function
  38. var canvas = document.createElement('canvas'); // Create a new canvas element
  39. canvas.width = video.videoWidth; // Set the canvas width = the video width
  40. canvas.height = video.videoHeight / 5; // Set the canvas height = the video height /5 to capture only the bottom 20% of the video
  41. canvas.getContext('2d').drawImage(video, 0, video.videoHeight * 0.7, canvas.width, canvas.height, 0, 0, canvas.width, canvas.height); // Capture a frame from the video
  42. return canvas.toDataURL('image/png'); // Return the captured frame as a data URL Base64 PNG img
  43. } // Finishes the captureFrame function
  44. })();

QingJ © 2025

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