emoji2emoticon

Replace emojis with traditional text emoticons

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         emoji2emoticon
// @namespace    http://tampermonkey.net/
// @version      0.0.2
// @description  Replace emojis with traditional text emoticons
// @author       huberatelier
// @match        *://*/*
// @icon         https://github.com/huberatelier/huberatelierThemes/blob/main/emoji2emoticon/lolicon.png?raw=true
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    const emojers = {
        '😀': ' :D ',
        '😃': ' =D ',
        '😄': ' ^_^ ',
        '😁': ' ^_^V ',
        '😆': ' xD ',
        '😅': ' ^_^\' ',
        '😂': ' LOL ',
        '🙂': ' :) ',
        '🙃': ' (: ',
        '😉': ' ;) ',
        '😊': ' ^_^ ',
        '😇': ' O:-) ',
        '😍': ' <3 ',
        '😘': ' :-* ',
        '😗': ' :-* ',
        '☺️': ' ^_^ ',
        '😚': ' :-* ',
        '😙': ' ;-* ',
        '😋': ' :p ',
        '😛': ' :P ',
        '😜': ' ;P ',
        '😝': ' :p ',
        '🤑': ' $.$ ',
        '🤗': ' \\(^_^)/ ',
        '🤔': ' :-? ',
        '🤐': ' :-X ',
        '😐': ' :| ',
        '😑': ' :| ',
        '😶': ' : ',
        '😏': ' ¬‿¬ ',
        '😒': ' ¬⁔¬ ',
        '🙄': ' -_- ',
        '😬': ' :E ',
        '😌': ' ^_^\' ',
        '😔': ' :\\ ',
        '😪': ' |-O ',
        '😴': ' (-O-) ',
        '😷': ' :() ',
        '🤒': ' :###.. ',
        '🤕': ' @_@ ',
        '😵': ' %) ',
        '😎': ' B) ',
        '🤓': ' 8) ',
        '😕': ' :/ ',
        '😟': ' D: ',
        '🙁': ' :( ',
        '☹️': ' :C ',
        '😮': ' :o ',
        '😯': ' :o ',
        '😲': ' :o ',
        '😳': ' ://) ',
        '😦': ' :(',
        '😧': ' D: ',
        '😨': ' D: ',
        '😰': ' D: ',
        '😥': ' ^_^\' ',
        '😢': ' T_T ',
        '😭': ' T_T ',
        '😱': ' :-o ',
        '😖': ' ://{ ',
        '😣': ' >_< ',
        '😞': ' ://( ',
        '😓': ' ^_^\' ',
        '😩': ' D; ',
        '😫': ' 囧 ',
        '😤': ' >_< ',
        '😡': ' >_< ',
        '😠': ' >_< ',
        '😈': ' >:D ',
        '👿': ' >:( ',
        '💀': ' 8-X '
    };

    const emojiregex = new RegExp(Object.keys(emojers).join('|'), 'g');

    function replacetext(node) {
        const original = node.nodeValue;
        const replaced = original.replace(emojiregex, match => emojers[match]);
        if (replaced !== original) {
            node.nodeValue = replaced;
        }
    }

    function replaceall() {
        const walker = document.createTreeWalker(document.body, NodeFilter.SHOW_TEXT, null, false);
        const nodes = [];

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

        nodes.forEach(replacetext);
    }

    window.addEventListener('load', replaceall);

    const observer = new MutationObserver(() => {
        clearTimeout(observer.timeout);
        observer.timeout = setTimeout(replaceall, 100);
    });

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