Youtube 双语字幕全平台

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

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

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