Youtube 双语字幕全平台

Youtube 双语字幕。移动端(mobile)修复,双端适用,而且支持 Via 浏览器。

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

  1. // ==UserScript==
  2. // @name Youtube 双语字幕全平台
  3. // @name:en Youtube dual subtitle
  4. // @name:zh-TW Youtube 雙語字幕全平臺
  5. // @version 2.0.2
  6. // @author Coink & jk278
  7. // @namespace https://github.com/jk278/youtube-dual-subtitle
  8. // @description: Youtube 双语字幕。移动端(mobile)修复,双端适用,而且支持 Via 浏览器。
  9. // @description:en Fix for mobile devices on YouTube bilingual captions. It works on both mobile and desktop, and supports the Via browser.
  10. // @description:zh-TW Youtube 雙語字幕。移動端(mobile)修復,雙端適用,而且支持 Via 瀏覽器。
  11. // @match *://www.youtube.com/watch?v=*
  12. // @match *://www.youtube.com
  13. // @match *://www.youtube.com/*
  14. // @match *://m.youtube.com/watch?v=*
  15. // @match *://m.youtube.com
  16. // @match *://m.youtube.com/*
  17. // @require https://unpkg.com/ajax-hook@latest/dist/ajaxhook.min.js
  18. // @grant none
  19. // @run-at document-start
  20. // @description Youtube 双语字幕。移动端(mobile)修复,双端适用,而且支持 Via 浏览器。
  21. // ==/UserScript==
  22.  
  23. /*
  24. 如果未自动加载,请切换字幕或关闭后再打开即可。默认语言为浏览器首选语言。
  25. */
  26.  
  27. (function () {
  28. 'use strict';
  29. // 检测浏览器首选语言,如果没有,设置为英语
  30. let localeLang = navigator.language.split('-')[0] || 'en'; // 跟随 YouTube 页面所用语言
  31. // localeLang = 'zh' // 取消注释此行以在此处定义您希望的语言
  32. // 启用双语字幕
  33. function enableSubs() {
  34. ah.proxy({
  35. onRequest: (config, handler) => {
  36. handler.next(config); // 处理下一个请求
  37. },
  38. onResponse: (response, handler) => {
  39. // 如果请求的 URL 包含 '/api/timedtext' 并且没有 '&translate_h00ked',则表示请求双语字幕
  40. if (response.config.url.includes('/api/timedtext') && !response.config.url.includes('&translate_h00ked')) {
  41. let xhr = new XMLHttpRequest(); // 创建新的 XMLHttpRequest
  42. // 使用 RegExp 清除我们的 xhr 请求参数中的 '&tlang=...',同时使用 Y2B 自动翻译
  43. let url = response.config.url.replace(/(^|[&?])tlang=[^&]*/g, '');
  44. url = `${url}&tlang=${localeLang}&translate_h00ked`;
  45. xhr.open('GET', url, false); // 打开 xhr 请求
  46. xhr.send(); // 发送 xhr 请求
  47. let defaultJson = null; // 声明默认 JSON 变量
  48. if (response.response) {
  49. const jsonResponse = JSON.parse(response.response);
  50. if (jsonResponse.events) defaultJson = jsonResponse;
  51. }
  52. const localeJson = JSON.parse(xhr.response); // 解析 xhr 响应
  53. let isOfficialSub = true;
  54. for (const defaultJsonEvent of defaultJson.events) {
  55. if (defaultJsonEvent.segs && defaultJsonEvent.segs.length > 1) {
  56. isOfficialSub = false;
  57. break;
  58. }
  59. }
  60. // 将默认字幕与本地语言字幕合并
  61. if (isOfficialSub) {
  62. // 如果片段长度相同
  63. for (let i = 0, len = defaultJson.events.length; i < len; i++) {
  64. const defaultJsonEvent = defaultJson.events[i];
  65. if (!defaultJsonEvent.segs) continue;
  66. const localeJsonEvent = localeJson.events[i];
  67. if (`${defaultJsonEvent.segs[0].utf8}`.trim() !== `${localeJsonEvent.segs[0].utf8}`.trim()) {
  68. // 避免在两者相同时合并字幕
  69. defaultJsonEvent.segs[0].utf8 += ('\n' + localeJsonEvent.segs[0].utf8);
  70. }
  71. }
  72. response.response = JSON.stringify(defaultJson); // 更新响应
  73. } else {
  74. // 如果片段长度不同(例如:自动生成的英语字幕)
  75. let pureLocalEvents = localeJson.events.filter(event => event.aAppend !== 1 && event.segs);
  76. for (const defaultJsonEvent of defaultJson.events) {
  77. if (!defaultJsonEvent.segs) continue;
  78. let currentStart = defaultJsonEvent.tStartMs,
  79. currentEnd = currentStart + defaultJsonEvent.dDurationMs;
  80. let currentLocalEvents = pureLocalEvents.filter(pe => currentStart <= pe.tStartMs && pe.tStartMs < currentEnd);
  81. let localLine = '';
  82. for (const ev of currentLocalEvents) {
  83. for (const seg of ev.segs) {
  84. localLine += seg.utf8;
  85. }
  86. localLine += ''; // 添加零宽空格,以避免单词粘在一起
  87. }
  88. let defaultLine = '';
  89. for (const seg of defaultJsonEvent.segs) {
  90. defaultLine += seg.utf8;
  91. }
  92. defaultJsonEvent.segs[0].utf8 = defaultLine + '\n' + localLine;
  93. defaultJsonEvent.segs = [defaultJsonEvent.segs[0]];
  94. }
  95. response.response = JSON.stringify(defaultJson); // 更新响应
  96. }
  97. }
  98. handler.resolve(response); // 处理响应
  99. }
  100. });
  101. }
  102. // 当文档加载完成并且字幕可用时,调用 enableSubs 函数启用双语字幕
  103. if (document.readyState === 'complete') {
  104. enableSubs(); // 如果文档已经加载完成,则启用双语字幕
  105. } else {
  106. window.addEventListener('load', enableSubs); // 如果文档尚未加载完成,添加事件监听器以在加载完成时启用双语字幕
  107. }
  108. })();