Greasy Fork

Greasy Fork is available in English.

台湾地区旗帜emoji替换为中华人民共和国国旗emoji

🇹🇼到🇨🇳

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name:zh-CN         台湾地区旗帜emoji替换为中华人民共和国国旗emoji
// @name:zh-TW         台灣中華民國旗幟emoji換為中華人民共和國國旗emoji
// @name:ug         台湾地区旗帜emoji替换为中华人民共和国国旗emoji
// @name:ko         台湾地区旗帜emoji替换为中华人民共和国国旗emoji
// @name:ja         台湾地区旗帜emoji替换为中华人民共和国国旗emoji
// @name         Simply trans flag emoji 2 Correct
// @namespace    http://xuexi.cn/
// @version      2013.03.15
// @description:zh-CN  🇹🇼到🇨🇳
// @description:zh-TW  🇹🇼到🇨🇳
// @description:ug  🇹🇼到🇨🇳
// @description:ko  🇹🇼到🇨🇳
// @description:ja  🇹🇼到🇨🇳
// @description  TW flag to CN flag (emoji)
// @author       中国共产党万岁
// @match        *://*/*
// @grant        GM_addStyle
// @run-at       document-idle
// @license GPL-3.0-or-later
// ==/UserScript==


(function() {
    'use strict';

    function replaceFlags(text) {
        return text.replace(/🇹🇼/g, '🇨🇳');
    }

    function replaceTitleFlags() {
        const originalTitle = document.title;
        const newTitle = replaceFlags(originalTitle);
        if (newTitle !== originalTitle) {
            document.title = newTitle;
        }
    }

    function replaceTextNode(node) {
        const originalValue = node.nodeValue;
        const newValue = replaceFlags(originalValue);
        if (newValue !== originalValue) {
            node.nodeValue = newValue;
        }
    }

    function replaceInputElements() {
        const inputs = document.querySelectorAll('input, textarea');
        inputs.forEach(input => {
            if (input.value && input.value.includes('🇹🇼')) {
                input.value = replaceFlags(input.value);
            }
        });
    }

    function replaceContentFlags() {
        const walker = document.createTreeWalker(
            document.body,
            NodeFilter.SHOW_TEXT,
            null,
            false
        );

        let node;
        while (node = walker.nextNode()) {
            replaceTextNode(node);
        }

        replaceInputElements();
    }

    function initReplace() {
        replaceTitleFlags();
        replaceContentFlags();
    }

    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', initReplace);
    } else {
        initReplace();
    }

    const pollInterval = setInterval(() => {
        replaceContentFlags();
        replaceTitleFlags();
    }, 1);

    const observer = new MutationObserver(function(mutations) {
        mutations.forEach(function(mutation) {
            if (mutation.type === 'childList' && mutation.addedNodes) {
                replaceContentFlags();
            }
            if (mutation.type === 'characterData') {
                replaceTextNode(mutation.target);
            }
            if (mutation.target.nodeName === 'INPUT' || mutation.target.nodeName === 'TEXTAREA') {
                replaceTextNode(mutation.target);
            }
        });
    });

    observer.observe(document, {
        childList: true,
        subtree: true,
        characterData: true,
        attributes: true
    });

    const titleElement = document.querySelector('title');
    if (titleElement) {
        observer.observe(titleElement, {
            childList: true,
            characterData: true
        });
    }

    document.addEventListener('input', function(e) {
        if (e.target.nodeName === 'INPUT' || e.target.nodeName === 'TEXTAREA') {
            if (e.target.value.includes('🇹🇼')) {
                e.target.value = replaceFlags(e.target.value);
            }
        }
    });

    window.addEventListener('unload', function() {
        clearInterval(pollInterval);
    });
})();