Universal Emoji & Kaomoji

quickly insert emojis or kaomojis when writing in any textarea/input

当前为 2022-09-28 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        Universal Emoji & Kaomoji
// @namespace   https://github.com/lmssieh/universal-emoji
// @match       *://*/*
// @grant       none
// @version     0.1
// @author      lmssieh
// @description quickly insert emojis or kaomojis when writing in any textarea/input
// @license     GNU GPLv3
// ==/UserScript==

(function () {
	let emojis = {};
	let kaomojis = {};

	let urls = [
		"https://raw.githubusercontent.com/lmssieh/universal-emoji/main/assets/emojis.json",
		"https://raw.githubusercontent.com/lmssieh/universal-emoji/main/assets/kaomojis.json",
	];
	let promiseUrls = urls.map((url) => fetch(url));

	Promise.all(promiseUrls)
		.then((responses) => Promise.all(responses.map((r) => r.json())))
		.then((data) => {
			emojis = { ...data[0] };
			kaomojis = data[1];
			console.log(emojis, kaomojis);
		})
		.then(main);

	function main() {
		function replaceText(str) {
			console.log(str);
			let newStr = str;
			// emoji regex, find anything inside colons, ex: :rooster:
			const regex = /(\:[\w\s]+\:)/g;
			let m;
			while ((m = regex.exec(str)) !== null) {
				// This is necessary to avoid infinite loops with zero-width matches
				if (m.index === regex.lastIndex) {
					regex.lastIndex++;
				}

				// The result can be accessed through the `m`-variable.
				m.forEach((match, groupIndex) => {
					const formateEmojiName = match.substr(1, match.length - 2);
					console.log(formateEmojiName, emojis[formateEmojiName]);
					if (!emojis[formateEmojiName]) return;
					const emoji = emojis[formateEmojiName].emoji;
					newStr = newStr.replace(match, `${emoji}`);
				});
			}

			// kaomoji regex, find a word after /slash, ex: /dab
			const kregex = /(\/[^ \/]+)/g;
			let mk;

			while ((mk = kregex.exec(str)) !== null) {
				// This is necessary to avoid infinite loops with zero-width matches
				if (mk.index === kregex.lastIndex) {
					kregex.lastIndex++;
				}

				// The result can be accessed through the `m`-variable.
				mk.forEach((match, groupIndex) => {
					const formatedKaomoji = match
						.substr(1, match.length - 1)
						.split("_")
						.join(" ");
					const kaomoji = kaomojis[formatedKaomoji];
					if (!kaomoji) return;
					newStr = newStr.replace(match, `${kaomoji}`);
				});
			}
			return newStr;
		}

		let debounce = null;
		document.addEventListener("keyup", ({ target }) => {
			if (
				target.tagName === "TEXTAREA" ||
				(target.tagName === "INPUT" && target.type === "text")
			) {
				clearTimeout(debounce);
				debounce = setTimeout(() => {
					target.value = replaceText(target.value);
				}, 100);
			}
		});
	}
})();