YouTube Subtitle Preview Plus

to check subtitle when you search

  1. // ==UserScript==
  2. // @name YouTube Subtitle Preview Plus
  3. // @namespace https://github.com/hjie/YouTube-Subtitle-Preview-Plus
  4. // @version 0.0.1
  5. // @description to check subtitle when you search
  6. // @author hjie
  7. // @match https://www.youtube.com/*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. // Replace this with your target language (ISO language code)
  12. const TARGET_LANGUAGE = ["en", "zh"];
  13.  
  14. // Check if youtube videos in thumbnails have subtitles in target language. If they do, add a little indicator at the top left of the thumbnail.
  15. function youtube_subtitle_check() {
  16. // Check for different thumbnail styles. This varies depending on if the user is on the old or new YouTube style.
  17. //let thumbnail_types = [document.querySelectorAll("div.yt-lockup-thumbnail"), document.querySelectorAll("a.ytd-thumbnail"), document.querySelectorAll("a.thumb-link")];
  18. let thumbnail_types = [document.querySelectorAll("a.ytd-thumbnail")];
  19.  
  20. thumbnail_types.forEach(function (type) {
  21.  
  22.  
  23.  
  24. // Get a list of all video thumbnails
  25. type.forEach(function (thumbnail) {
  26. if(!thumbnail.hasAttribute("href")){
  27. return false;
  28. }
  29.  
  30. // Get YT video id for testing
  31. let video_id = "";
  32. if (thumbnail.hasAttribute("href")) {
  33. video_id = thumbnail.getAttribute("href").split("/watch?v=")[1].split("&")[0];
  34. } else {
  35. video_id = thumbnail.querySelector("a").getAttribute("href").split("/watch?v=")[1].split("&")[0];
  36. }
  37.  
  38. // Make sure we only check once
  39. if (thumbnail.getAttribute("subtitle_tested") == "true") {
  40. return;
  41. }
  42. thumbnail.setAttribute("subtitle_tested", "true");
  43.  
  44. let xhttp = new XMLHttpRequest();
  45. xhttp.onreadystatechange = function () {
  46. if (this.readyState == 4 && this.status == 200) {
  47. let text = xhttp.responseText;
  48.  
  49. for (let i = 0; i < TARGET_LANGUAGE.length; i++) {
  50. let lang = TARGET_LANGUAGE[i];
  51.  
  52.  
  53. if (text.includes('lang_code="' + lang + '"') || text.includes('lang_code="' + lang + '-')) {
  54. let left_pos = i * 20;
  55. // Sorry, this is ugly. I'm not good with JavaScript.
  56. let new_element = document.createElement("span");
  57. new_element.style.backgroundColor = "white";
  58. new_element.style.zIndex = "1000";
  59. new_element.style.position = "absolute";
  60. new_element.style.borderRadius = "8px";
  61. new_element.style.boxShadow = "2px 1px 2px black";
  62. new_element.style.padding = "2px";
  63. new_element.style.textAlign = "center";
  64. new_element.style.left = left_pos + "px";
  65. new_element.style.top = "2px";
  66. new_element.style.lineHeight = "normal";
  67. new_element.style.fontFamily = "sans-serif";
  68. new_element.style.fontSize = "8pt";
  69. new_element.style.color = "black";
  70. new_element.style.userSelect = "none";
  71. new_element.innerHTML = lang.toUpperCase();
  72. thumbnail.appendChild(new_element);
  73. }
  74. }
  75. }
  76. };
  77. xhttp.open("GET", "https://video.google.com/timedtext?type=list&v=" + video_id, true);
  78. xhttp.send();
  79. });
  80.  
  81. });
  82.  
  83.  
  84. }
  85.  
  86. // Run on page load and every 3 seconds
  87. youtube_subtitle_check();
  88. setInterval(youtube_subtitle_check, 3000);

QingJ © 2025

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