Greasy Fork

Greasy Fork is available in English.

Auto-Translate Posts on X/Twitter

Automatically clicks the "Translate post" button on X.com posts if available

当前为 2025-01-04 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Auto-Translate Posts on X/Twitter
// @version      1.0
// @description  Automatically clicks the "Translate post" button on X.com posts if available
// @author       https://github.com/ruukulada
// @namespace    https://github.com/ruukulada
// @match        https://*.x.com/*
// @license      MIT
// @grant        none
// ==/UserScript==

(function () {
    'use strict';

    // Function to find and click the "Translate post" button
    const clickTranslateButton = () => {
        // Look for a <button> containing a <span> with text "Translate post"
        const translateButton = Array.from(document.querySelectorAll('button span'))
            .find(el => el.textContent.trim() === "Translate post");
        if (translateButton) {
            // Click the parent <button>
            const button = translateButton.closest('button');
            if (button) {
                button.click();
                console.log("Clicked 'Translate post' button.");
            }
        }
    };

    // Monitor for changes on the page to detect new posts
    const observer = new MutationObserver((mutations) => {
        for (let mutation of mutations) {
            if (mutation.addedNodes.length > 0) {
                clickTranslateButton();
            }
        }
    });

    // Start observing the body of the document
    observer.observe(document.body, { childList: true, subtree: true });

    // Initial check in case the button is already present
    clickTranslateButton();
})();