Greasy Fork

来自缓存

Greasy Fork is available in English.

My Lang Changer for Twitter

Changes all lang="ja" attributes to lang="zh" on Twitter pages.

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

You will need to install an extension such as Tampermonkey to install this script.

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         My Lang Changer for Twitter
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Changes all lang="ja" attributes to lang="zh" on Twitter pages.
// @author       Jason2be
// @match        https://*.twitter.com/*
// @grant        none
// @run-at       document-start
// ==/UserScript==

(function() {
    'use strict';

    // 初始化时检查并替换元素
    function replaceLangAttributesOnInit() {
        const elements = document.querySelectorAll('[lang="ja"]');
        elements.forEach(element => {
            console.log('替换元素 ' + element.outerHTML);
            element.setAttribute('lang', 'zh');
        });
    }

    // 监听 DOM 变化
    const observer = new MutationObserver((mutations) => {
        mutations.forEach(mutation => {
            if (mutation.type === 'childList' && mutation.addedNodes.length) {
                mutation.addedNodes.forEach(node => {
                    if (node.nodeType === Node.ELEMENT_NODE) {
                        const elements = node.querySelectorAll('[lang="ja"]');
                        elements.forEach(element => {
                            console.log('替换元素 ' + element.outerHTML);
                            element.setAttribute('lang', 'zh');
                        });
                    }
                });
            }
        });
    });

    // 开始监听整个文档的DOM变化
    observer.observe(document, { childList: true, subtree: true });

    // 初始化时替换元素
    replaceLangAttributesOnInit();
})();