Torn ID link replacer thing

Replaces {###} with embedded link to the specified ID anywhere in Torn

スクリプトをインストールするには、Tampermonkey, GreasemonkeyViolentmonkey のような拡張機能のインストールが必要です。

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

スクリプトをインストールするには、TampermonkeyViolentmonkey のような拡張機能のインストールが必要です。

スクリプトをインストールするには、TampermonkeyUserscripts のような拡張機能のインストールが必要です。

このスクリプトをインストールするには、Tampermonkeyなどの拡張機能をインストールする必要があります。

このスクリプトをインストールするには、ユーザースクリプト管理ツールの拡張機能をインストールする必要があります。

(ユーザースクリプト管理ツールは設定済みなのでインストール!)

このスタイルをインストールするには、Stylusなどの拡張機能をインストールする必要があります。

このスタイルをインストールするには、Stylus などの拡張機能をインストールする必要があります。

このスタイルをインストールするには、Stylus tなどの拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

(ユーザースタイル管理ツールは設定済みなのでインストール!)

このスクリプトの質問や評価の投稿はこちら通報はこちらへお寄せください
// ==UserScript==
// @name         Torn ID link replacer thing
// @namespace    http://tampermonkey.net/
// @version      0.01
// @description  Replaces {###} with embedded link to the specified ID anywhere in Torn
// @author       Weav3r
// @match        https://www.torn.com/*
// @grant        none
// ==/UserScript==

(() => {
  const RE = /\{(\d+)\}$/;
  const ok = el => el && (el.tagName === "TEXTAREA" || el.tagName === "INPUT");

  addEventListener("beforeinput", e => {
    if (e.data !== "}" || !ok(document.activeElement)) return;
    const el = document.activeElement;
    const val = el.value.slice(0, el.selectionStart) + "}";
    const m = val.match(RE);
    if (!m) return;
    e.preventDefault();
    el.setRangeText(`<a href="/profiles.php?XID=${m[1]}">${m[1]}</a>`,
                    el.selectionStart - m[0].length + 1,
                    el.selectionStart + 1,
                    "end");
    el.dispatchEvent(new Event("input", { bubbles: true }));
  }, true);
})();