intercept-link

add link on attack page for intercept

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

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

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
// @name        intercept-link
// @namespace   seintz.torn.intercept-link
// @version     6.3
// @description add link on attack page for intercept
// @author      Elvay [3095345]
// @license     GNU GPLv3
// @run-at      document-end
// @match       https://www.torn.com/page.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 });