intercept-link

add link on attack page for intercept

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

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

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

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.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==UserScript==
// @name        intercept-link
// @namespace   seintz.torn.intercept-link
// @version     6.2
// @description add link on attack page for intercept
// @author      finally [2060206], seintz [2460991]
// @license     GNU GPLv3
// @run-at      document-end
// @match       https://www.torn.com/loader.php?sid=attack*
// @grant       GM.addStyle
// ==/UserScript==

const participantsNode = "ul[class^='participants']";
const actionLogNode = "ul[class^='list']";

GM.addStyle(`
    .finally-ap-link {
        color: var(--default-color);
    }`);

function watchParticipants(observeNode) {
  if (!observeNode) return;

  const participantNode =
    "div[class^= 'playerWrap'] > span[class^= 'playername'";
  observeNode.querySelectorAll(participantNode).forEach((e) => addNameLink(e));

  new MutationObserver((mutations) => {
    mutations.forEach((mutation) => {
      for (const node of mutation.addedNodes) {
        addNameLink(node.querySelector && node.querySelector(participantNode));
      }
    });
  }).observe(observeNode, { childList: true, subtree: true });
}

function addNameLink(node) {
  if (!node) return;
  if (node.querySelector("a")) return;

  var name = node.innerHTML;
  node.innerHTML = `<a class="finally-ap-link" target="_blank" href="profiles.php?NID=${name}">${name}</a>`;
}

function watchActionLog(observeNode) {
  if (!observeNode) return;

  const logNode = "span[class^='message'] > span";
  observeNode.querySelectorAll(logNode).forEach((e) => addLogLink(e));

  new MutationObserver((mutations) => {
    mutations.forEach((mutation) => {
      for (const node of mutation.addedNodes) {
        addLogLink(node.querySelector && node.querySelector(logNode));
      }
    });
  }).observe(observeNode, { childList: true, subtree: true });
}

function addLogLink(node) {
  if (!node) return;
  if (node.querySelector("a")) return;

  node.innerHTML = node.innerHTML
    .replace(
      /^([^\s]+)/i,
      '<a class="finally-ap-link" target="_blank" href="profiles.php?NID=$1">$1</a>'
    )
    .replace(
      /(\s(?:from|hit(?:ting)?|defeated|stalemated\swith|near|against|puncturing|at|damaged|miss(?:ed|ing)|left|mugged|hospitalized|lost\sto)\s)([^\s\,]+)/i,
      '$1<a class="finally-ap-link" href="profiles.php?NID=$2">$2</a>'
    )
    .replace(
      /(\s(?:in)\s)([^\s\,]+)(\'s\sface)/i,
      '$1<a class="finally-ap-link" href="profiles.php?NID=$2">$2</a>$3'
    );
}

watchActionLog(document.querySelector(actionLogNode));
watchParticipants(document.querySelector(participantsNode));

new MutationObserver((mutations) => {
  mutations.forEach((mutation) => {
    for (const node of mutation.addedNodes) {
      watchActionLog(node.querySelector && node.querySelector(actionLogNode));
    }
  });
}).observe(document.body, { childList: true, subtree: true });

new MutationObserver((mutations) => {
  mutations.forEach((mutation) => {
    for (const node of mutation.addedNodes) {
      watchParticipants(
        node.querySelector && node.querySelector(participantsNode)
      );
    }
  });
}).observe(document.body, { childList: true, subtree: true });