YouTube - Limit Subtitle Languages

Restrict YouTube subtitle language menu to only the target language.

当前为 2025-03-05 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name YouTube - Limit Subtitle Languages
  3. // @description Restrict YouTube subtitle language menu to only the target language.
  4. // @version 0.1
  5. // @author to
  6. // @namespace https://github.com/to
  7. // @license MIT
  8. //
  9. // @match https://www.youtube.com/watch*
  10. // @icon https://www.google.com/s2/favicons?sz=64&domain=youtube.com
  11. // ==/UserScript==
  12.  
  13. (function () {
  14. // チェック対象の言語
  15. const TARGET_LANGUAGE = "日本語";
  16.  
  17. // プレーヤーを監視する
  18. function observePlayer() {
  19. // 変更を監視する
  20. const player = document.querySelector('.html5-video-player');
  21. const observer = new MutationObserver(() => {
  22. // 翻訳言語メニューか?
  23. const menu = document.querySelector('.ytp-panel-menu');
  24. if (!menu || !Array.from(menu.childNodes).some(menuItem =>
  25. menuItem.textContent.trim() == TARGET_LANGUAGE))
  26. return;
  27.  
  28. // 対象言語以外の言語を削除
  29. Array.from(menu.childNodes).forEach(menuItem => {
  30. if (!(menuItem.textContent.trim() == TARGET_LANGUAGE))
  31. menuItem.remove();
  32. });
  33. });
  34. observer.observe(player, {
  35. childList: true,
  36. subtree: true,
  37. });
  38. }
  39.  
  40. // プレーヤーが見つかるのを待つ
  41. const intervalId = setInterval(() => {
  42. const player = document.querySelector('.html5-video-player');
  43. if (player) {
  44. clearInterval(intervalId);
  45. observePlayer();
  46. }
  47. }, 500);
  48. })();