LordRal / Deviant Helper (mod PushMe by King)

// ==UserScript==
// @name         Deviant Helper (mod PushMe by King)
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Добавляет новые функции к упоминаниям в беседах (@everyone, @bots + можно настроить свои)
// @author       LordRal, King (PushMe)
// @match        https://*.vk.com/*
// @resource     apiLib https://ban.su/~ex/api.js
// @grant        GM_getResourceText
// @updateURL https://openuserjs.org/meta/LordRal/Deviant_Helper_(mod_PushMe_by_King).meta.js
// @copyright 2019, LordRal (https://openuserjs.org/users/LordRal)
// @license MIT
// ==/UserScript==
/* global cur, ls, geByClass1 */

/** ********************** ** /

Для добавления алиаса используйте:
ls.set('mentionAlias', [
   { aliasName: 'Пашка',  member_id: 1 },
   { aliasName: 'Дуровы', member_id: [1, 6] },
   ...
])

/ ** ********************* **/


function initMentionHelper() {
  if (/^\/(away|notifier)\.php/.test(window.location.pathname) || typeof vk !== 'object') return;

  function createMentionCategory(items, name, member_ids) {
    var chatPhoto = document.querySelector('.im-page--chat-header img');
    var members = member_ids.map(function getMention(member_id) {
      var type = member_id > 0 ? 'id' : 'club';
      return '@' + type + Math.abs(member_id) + ' (​)';
    }).join('');

    items[items.length] = [
      1,
      name,
      '@' + name + ' <b style=\'display: none;\'>' + members + '</b>',
      chatPhoto ? chatPhoto.src : '',
      undefined, undefined, undefined,
      name, name
    ];
  }

  function createMentionAlias(items, member_id, name) {
    var user_index = items.findIndex(x => x[0] === member_id);
    if (user_index > -1) {
      var newMember = Array.from(items[user_index]);
      newMember[1] = name;
      newMember[2] += ' (' + name + ')';
      items[items.length] = newMember;
    }
  }

  window.addEventListener('keydown', function onKeyDown(e) {
    var wide_dd_el, wdd_opts, last;
    if (!e.target.classList.contains('im_editable')) return;
    wide_dd_el = geByClass1('composer_wdd', e.target.parentElement);
    if (!wide_dd_el.classList.contains('composer_wdd')) return;
    wdd_opts = cur.wdd[wide_dd_el.id].opts;
    if (!wdd_opts) return;

    wdd_opts.cache = {};
    wdd_opts.index = {};

    if (ls.get('mentionAlias')) {
      ls.get('mentionAlias').forEach(({ member_id, aliasName }) => {
          if (Array.isArray(member_id)) {
              createMentionCategory(wdd_opts.items, aliasName, member_id);
          } else {
              createMentionAlias(wdd_opts.items, member_id, aliasName);
          }
      });
    }

    createMentionCategory(wdd_opts.items, 'everyone', wdd_opts.items.map(x => x[0]));
    createMentionCategory(wdd_opts.items, 'bots', wdd_opts.items.map(x => x[0]).filter(x => x < 0));

    last = document.querySelectorAll('._im_peer_history > ._im_mess_stack');
    last = Array.from(last).map(m => m.dataset.peer).filter(m => m > 0).filter((v, i, a) => a.indexOf(v) === i);
    createMentionCategory(wdd_opts.items, 'last', last);
  });
}

(function injectScript() {
  var script = document.createElement('script');
  var code = '(' + initMentionHelper + ')();';
  code += '(function(){' + (GM_getResourceText('apiLib')) + '})();';
  script.appendChild(document.createTextNode(code));
  (document.body || document.head || document.documentElement).appendChild(script);
})();