Greasy Fork镜像 支持简体中文。

Youtube Copying Selected Subtitles - Immersive Translation

When Youtube using Immersive Translation copies the subtitles to the clipboard, supporting before translation, after translation, or select all Youtube

目前為 2024-08-17 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name Youtube Copying Selected Subtitles - Immersive Translation
  3. // @name:zh 沉浸式翻译字幕复制
  4. // @name:zh-CN 沉浸式翻译字幕复制
  5. // @name:en Youtube Copying Selected Subtitles - Immersive Translation
  6. // @namespace https://github.com/i02sDarling/tools
  7. // @description When Youtube using Immersive Translation copies the subtitles to the clipboard, supporting before translation, after translation, or select all Youtube
  8. // @description:zh-CN 使用沉浸式翻译时将字幕复制到剪贴板,支持翻译前,翻译后,或者全选
  9. // @description:en When Youtube using Immersive Translation copies the subtitles to the clipboard, supporting before translation, after translation, or select all Youtube
  10. // @license MIT
  11. // @author i02sDarling
  12. // @version 1.3.5
  13. // @include *//www.youtube.com/watch?v=*
  14. // @match *://*.youtube.com/*
  15. // @exclude *://accounts.youtube.com/*
  16. // @exclude *://www.youtube.com/live_chat_replay*
  17. // @exclude *://www.youtube.com/persist_identity*
  18. // @icon https://www.google.com/s2/favicons?sz=64&domain=youtube.com
  19. // @license MIT
  20. // @compatible edge
  21. // @compatible firefox
  22. // @compatible chrome
  23. // ==/UserScript==
  24.  
  25.  
  26.  
  27. (function () {
  28. 'use strict';
  29. console.log("02sdarling:Running");
  30. let curSelect = 1;
  31. const OutSelection = ['Ori', 'Out', 'Both'];
  32. const button = document.createElement('button');
  33. const toggleBtn = document.createElement('button');
  34. mkContainer();
  35. let intervalId;
  36. intervalId = setInterval(addClickEventWhenTargetAppears, 4000);
  37.  
  38. function toggle() {
  39. button.textContent = 'Copy';
  40. }
  41. function appendStyle(btn) {
  42. btn.style.position = 'absolute';
  43. btn.style.left = '10px';
  44. btn.style.transform = 'translateY(-70%)';
  45. btn.style.backgroundColor = '#007bff';
  46. btn.style.color = '#fff';
  47. btn.style.padding = '10px 15px';
  48. btn.style.border = 'none';
  49. btn.style.borderRadius = '5px';
  50. btn.style.cursor = 'pointer';
  51.  
  52. }
  53.  
  54. function mkContainer() {
  55. button.id = '02sdarling';
  56. button.textContent = 'Waiting';
  57. button.style.top = '70%';
  58. appendStyle(button);
  59. document.body.appendChild(button);
  60. }
  61. function mkToggle() {
  62. toggleBtn.id = '02sdarlingYa';
  63. toggleBtn.textContent = OutSelection[curSelect];
  64. toggleBtn.style.top = '90%';
  65. appendStyle(toggleBtn);
  66. document.body.appendChild(toggleBtn);
  67. }
  68. toggleBtn.addEventListener('click', function () {
  69. curSelect = (curSelect + 1) % 3;
  70. toggleBtn.textContent = OutSelection[curSelect];
  71. })
  72. function getSpan() {
  73. const container=document.getElementById('immersive-translate-caption-window');
  74. const TmpString = container.getInnerHTML();
  75. const domstring = TmpString.replace(/<template[^>]*>|<\/template>/g, '');
  76. return domstring;
  77. }
  78. function getText() {
  79. const span = getSpan();
  80. if (span) {
  81. const spanContents = span.match(/<span\b[^>]*>(.*?)<\/span>/gi);
  82. const res=spanContents.map(span => {
  83. const match = span.match(/>(.*?)<\/span>/); // 正则匹配 span 标签内的内容
  84. return match ? match[1] : null; // 返回捕获到的文本内容
  85. });
  86. return res;
  87. }
  88. return "falut to copy";
  89. }
  90.  
  91.  
  92. function addClickEventWhenTargetAppears() {
  93. try {
  94. if (getSpan()) {
  95. mkToggle();
  96. toggle();
  97. clearInterval(intervalId);
  98. }
  99. if (button.textContent === 'Waiting') {
  100. return;
  101. }
  102. //const ClickContainer=doc.getElementsByClassName('captions-text');
  103. button.addEventListener('click', function () {
  104.  
  105. let resArray = getText();
  106. let res='';
  107. if(curSelect==1) res=resArray[1];
  108. else if(curSelect==2) res=resArray[0]+resArray[1];
  109. else res=resArray[0];
  110. navigator.clipboard.writeText(res)
  111. .then(() => {
  112. console.log('Copy successful:', res);
  113. })
  114. .catch(err => {
  115. console.error('Copy failed:', err);
  116. });
  117. })
  118. } catch (e) {
  119. console.log('Waiting');
  120. }
  121.  
  122. }
  123.  
  124.  
  125. })();

QingJ © 2025

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