Live Stream Chat Users (Mentions & Search)

Minimalistic userscript that allows you to mention any user in chat by one click on their name or to search for their channel by double click.

目前为 2022-04-24 提交的版本。查看 最新版本

// ==UserScript==
// @name           Live Stream Chat Users (Mentions & Search)
// @name:ru        Пользователи в чате стрима (упоминания и поиск)
// @namespace      https://greasyfork.org/en/users/830433-vintprox
// @description    Minimalistic userscript that allows you to mention any user in chat by one click on their name or to search for their channel by double click.
// @description:ru Минималистичный пользовательский скрипт, который позволяет упомянуть пользователя в чате при одном лишь клику по его имени, а также производить поиск канала при двойном.
// @version        1.0.1
// @icon           https://i.postimg.cc/mkQB9T7G/youtube-mentions-userscript.png
// @license        MIT
// @author         vintprox
// @match          https://www.youtube.com/*
// @grant          none
// ==/UserScript==

(function() {
  if (location.pathname != "/live_chat") return;

  
  const style = document.createElement("style");
  style.type = "text/css";
  style.appendChild(document.createTextNode(`
    #author-name {
      cursor: pointer;
    }
    #author-name:hover {
      text-decoration: underline;
    }
  `));
  document.head.appendChild(style);

  const chat = document.querySelector("#chat");
  const input = document.querySelector("#input[contenteditable]");

  chat.addEventListener("click", function (e) {
    if (e.detail > 1) return;
    if (e.target.id != "author-name") return;

    const mention = `@${e.target.innerText}\xa0`;
    input.innerText = mention + input.innerText;
    input.dispatchEvent(new Event("input"));

    const range = document.createRange();
    range.setStart(input, 1);
    range.collapse(true);
    const selection = window.getSelection();
    selection.removeAllRanges();
    selection.addRange(range);
    input.focus();
  });
  
  chat.addEventListener("dblclick", function (e) {
    if (e.target.id != "author-name") return;

    e.preventDefault();
    window.open(`https://www.youtube.com/results?search_query="${encodeURI(e.target.innerText)}"&sp=CAASAhAC`);
  });
})();