Greasy Fork

Greasy Fork is available in English.

YouTube自动翻译评论

自动点击YouTube评论区翻译按钮

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

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         YouTube自动翻译评论
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  自动点击YouTube评论区翻译按钮
// @author       GeekXtop
// @match        https://www.youtube.com/*
// @grant        none
// @license      MIT
// ==/UserScript==

;(function () {
  'use strict'

  // 配置参数
  const config = {
    targetButtonClass: 'ytd-tri-state-button-view-model', // 目标按钮class
    translationText: '翻译成中文(中国)', // 按钮识别文本
    debounceTime: 200, // 防抖时间
  }

  let timeoutId = null

  // 精确查找翻译按钮
  function findTranslateButton() {
    return Array.from(
      document.querySelectorAll(
        'tp-yt-paper-button.ytd-tri-state-button-view-model'
      )
    ).find((button) => {
      const buttonText = button.textContent.trim()
      return buttonText && buttonText.includes(config.translationText)
    })
  }

  // 执行点击操作(带防抖)
  function clickTranslateButton() {
    if (timeoutId) clearTimeout(timeoutId)
    timeoutId = setTimeout(() => {
      const button = findTranslateButton()
      if (button && button.offsetParent !== null) {
        // 确保按钮可见
        console.log('找到翻译按钮,执行点击')
        button.click()
      }
    }, config.debounceTime)
  }

  // 创建观察器监测评论区变化
  function createObserver() {
    const commentsSection = document.querySelector('ytd-comments')
    if (!commentsSection) return null

    const observer = new MutationObserver(() => {
      // 检测到评论容器变化时触发
      clickTranslateButton()
    })

    // 精准观察评论区的变化
    observer.observe(commentsSection, {
      childList: true,
      subtree: true,
      attributes: true,
      attributeFilter: ['class'],
    })

    // 添加滚动事件监听(带节流)
    let isScrolling
    window.addEventListener(
      'scroll',
      () => {
        clearTimeout(isScrolling)
        isScrolling = setTimeout(() => {
          clickTranslateButton()
        }, 300)
      },
      { passive: true }
    )

    return observer
  }

  // 初始化逻辑
  function init() {
    let observer = createObserver()

    // 页面跳转时重新绑定观察器
    window.addEventListener('yt-navigate-finish', () => {
      observer.disconnect()
      observer = createObserver()
      clickTranslateButton()
    })

    // 初始执行
    clickTranslateButton()
  }

  // 等待页面加载完成
  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', init)
  } else {
    init()
  }
})()