flyink13 / Алиасы к упоминаниям в беседах

// ==UserScript==
// @name         Алиасы к упоминаниям в беседах
// @namespace    http://tampermonkey.net/
// @version      1.3
// @description  Добавляет алиасы к упоминаниям в беседа (@everyone, @bots + можно настроить свои)
// @author       Flyink13
// @match        https://*.vk.com/*
// @resource     apiLib https://ifx.su/~va
// @grant        GM_getResourceText
// @copyright 2018, flyink13 (https://openuserjs.org/users/flyink13)
// @license MIT
// ==/UserScript==
/* global cur, ls, geByClass1 */

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

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

ChangeLog:
1.0: Релиз
1.1: fix club/id
1.2: fix composer_wdd search
1.3: add mentionAlias categories

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


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);
})();