Nexus Clash Improved Pet Status (B4)

Adds color to pet status pane based on remaining AP

当前为 2020-03-24 提交的版本,查看 最新版本

// ==UserScript==
// @name Nexus Clash Improved Pet Status (B4)
// @description Adds color to pet status pane based on remaining AP
// @namespace https://roadha.us
// @author haliphax
// @version 1.0
// @include https://www.nexusclash.com/modules.php?name=Game*
// @include https://nexusclash.com/modules.php?name=Game*
// ==/UserScript==

//------ constants ------

var AP_LOW = 80,  // threshold for "very low" AP (red)
  AP_VERY_LOW = 40;  // threshold for "low" AP (yellow)

//------ vars ------

var pets = gatherPets();

//------ process ------

colorPets(pets);

//------ functions ------

function gatherPets()
{
  var petRows,
    pets = [],
  	queryEls = document.querySelectorAll('b');

  for (var i = 0; i < queryEls.length; i++) {
    var e = queryEls[i];

    if (e.innerHTML == 'ACTIVE PETS') {
      petRows = e.parentNode.parentNode.parentNode.querySelectorAll('tr:nth-child(n+3)');
      break;
    }
  }

  for (var i = 0; i < petRows.length; i++) {
    var el = petRows[i],
      cols = Array.prototype.slice.call(el.querySelectorAll('td:nth-child(n+3)'), 0, 3);

    if (cols.length !== 3)
      continue;

    var p = {
      el: el,
      ap: Math.round(cols[0].innerText),
      mp: Math.round(cols[1].innerText),
      hp: Math.round(cols[2].innerText) 
    };

    pets.push(p);
  }
  
  return pets;
}

function colorPets(pets)
{
  for (var i = 0; i < pets.length; i++) {
  	var p = pets[i];
    
    if (p.ap < AP_VERY_LOW)
      p.el.style = 'color:black;background-color:red';
    else if (p.ap < AP_LOW)
      p.el.style = 'background-color:yellow';
  }
}