YouTube自动展开&翻译评论

自动展开并翻译YouTube评论区回复

  1. // ==UserScript==
  2. // @name YouTube自动展开&翻译评论
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.7
  5. // @description 自动展开并翻译YouTube评论区回复
  6. // @author GeekXtop
  7. // @match https://www.youtube.com/watch*
  8. // @grant none
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12. ;(function () {
  13. 'use strict'
  14.  
  15. const clickTimeout = 3000
  16.  
  17. // 自动点击"展开"按钮
  18. const clickExpandButtons = () => {
  19. // 精确匹配包含"展开"文本的按钮
  20. document
  21. .querySelectorAll('tp-yt-paper-button span.more-button')
  22. .forEach((btn) => {
  23. const button = btn.closest('tp-yt-paper-button')
  24. if (button && btn.textContent.trim() === '展开') {
  25. button.click()
  26. console.log('已展开评论')
  27. }
  28. })
  29. }
  30.  
  31. // 自动点击翻译按钮(适配新版YouTube界面)
  32. const clickTranslateButtons = () => {
  33. document
  34. .querySelectorAll(
  35. 'ytd-tri-state-button-view-model.translate-button.ytd-comment-view-model'
  36. )
  37. .forEach((btn) => {
  38. const button = btn.querySelector('tp-yt-paper-button')
  39. if (button && button.textContent.includes('翻译成中文(中国)')) {
  40. button.click()
  41. console.log('已触发翻译')
  42. // 添加防抖处理避免重复点击
  43. button.style.pointerEvents = 'none'
  44. setTimeout(() => (button.style.pointerEvents = 'auto'), clickTimeout)
  45. }
  46. })
  47. }
  48.  
  49. // 主执行函数
  50. const main = () => {
  51. setTimeout(clickExpandButtons, clickTimeout)
  52. setTimeout(clickTranslateButtons, clickTimeout)
  53. }
  54.  
  55. // 监听DOM变化以处理动态加载的评论
  56. const observer = new MutationObserver(main)
  57. const commentsSection = document.getElementById('comments');
  58. if (commentsSection) {
  59. observer.observe(commentsSection, {
  60. childList: true,
  61. subtree: true,
  62. });
  63. } else {
  64. console.warn('找不到评论区,MutationObserver可能无法正常工作');
  65. }
  66.  
  67. // 初始执行
  68. main()
  69. })()

QingJ © 2025

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