hipda-emoji

HiPDA emoji support

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         hipda-emoji
// @namespace    https://github.com/maltoze/tampermonkey-scripts
// @version      0.2.1
// @description  HiPDA emoji support
// @author       maltoze
// @match        https://www.hi-pda.com/forum/*
// @match        https://www.4d4y.com/forum/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function () {
  'use strict';

  const needDecodeEntity = '&';
  Array.from(document.getElementsByClassName('t_msgfont')).forEach((elem) => {
    if (elem.innerHTML.includes(needDecodeEntity)) {
      elem.innerHTML = elem.innerHTML.replaceAll(needDecodeEntity, '&');
    }
  });

  const fastPostSmiliesElem = document.querySelector('#fastpostsmilies');
  const cmdBeforeElem = document.querySelector('#e_cmd_custom1_rm');
  if (!fastPostSmiliesElem && !cmdBeforeElem) {
    return;
  }

  function insertEmojiTrigger(elem, style) {
    elem.insertAdjacentHTML(
      'afterend',
      `<a id="emoji-trigger" style="text-indent: 0; cursor: pointer; ${style}">😀</a>`,
    );
  }

  fastPostSmiliesElem &&
    insertEmojiTrigger(
      fastPostSmiliesElem,
      'background: none; text-decoration: none',
    );
  cmdBeforeElem && insertEmojiTrigger(cmdBeforeElem, 'text-align: center');

  const emojiSize = 24;
  function loadEmojiButton() {
    const emojiScript = document.createElement('script');
    emojiScript.type = 'module';
    emojiScript.text = `
      import { EmojiButton } from 'https://cdn.jsdelivr.net/npm/@joeattardi/[email protected]/dist/index.min.js';

      const picker = new EmojiButton({
        style: 'twemoji',
        twemojiOptions: {
          base: 'https://cdn.jsdelivr.net/gh/twitter/[email protected]/assets/',
        },
      });
      const trigger = document.querySelector('#emoji-trigger');
      const fastPostElem = document.querySelector('#fastpostmessage');
      const postBoxIframe = document.querySelector('#postbox #e_iframe');
      const postBoxBodyElem =
        postBoxIframe && postBoxIframe.contentWindow.document.querySelector('body');

      picker.on('emoji', (selection) => {
        const imgUrl = selection.url;
        if (fastPostElem) {
          const emojiStr = '[img=${emojiSize},${emojiSize}]' + imgUrl + '[/img]';
          const fastPostValue = fastPostElem.value;
          fastPostElem.value = ''.concat(
            fastPostValue.slice(0, fastPostElem.selectionStart),
            emojiStr,
            fastPostValue.slice(fastPostElem.selectionEnd),
          );
        }
        if (postBoxBodyElem) {
          postBoxBodyElem.innerHTML +=
            '<img width="${emojiSize}" height="${emojiSize}" src=' + imgUrl + ' />';
        }
      });

      trigger.addEventListener('click', () => picker.togglePicker(trigger));
    `;
    document.body.appendChild(emojiScript);
  }

  loadEmojiButton();
})();