GC I Failed! Helper

Adds up losing stocks

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

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.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         GC I Failed! Helper
// @namespace    grundos.cafe
// @version      1.1
// @description  Adds up losing stocks
// @author       Sphere
// @match        https://www.grundos.cafe/games/stockmarket/portfolio/
// @grant        none
// ==/UserScript==

(() => {
  
  const rows = document.querySelectorAll('.portfolio-subtable tbody tr');
  const sellButton = document.querySelector('#show_sell');
  
  let maxLoss = 0;
  const negativeRows = [];
  
  rows.forEach(row => {
    const paid = parseInt(row.querySelector('td:nth-of-type(3)').textContent.replace(/,/g, ''), 10);
    const current = parseInt(row.querySelector('td:nth-last-of-type(3)').textContent.replace(/,/g, ''), 10);
    
    if (current < paid) {
      maxLoss += paid - current;
      negativeRows.push(row);
    }
  });
  
  const p = document.createElement('p');
  p.textContent = 'Possible loss: ';
  
  const strong = document.createElement('strong');
  strong.textContent = `${maxLoss.toLocaleString()} NP `;
  p.appendChild(strong);
  
  const button = document.createElement('input');
  button.classList.add('form-control');
  button.type = 'button';
  button.value = 'Select losing shares';
  button.addEventListener('click', event => {
    for (const row of negativeRows) {
      const shares = parseInt(row.querySelector('td').textContent.replace(/,/g, ''), 10);
      const input = row.querySelector('input');
      input.value = `${shares}`;
      
      row.parentNode.parentNode.parentNode.parentNode.style.removeProperty('display'); // lol
    }
    sellButton.style.removeProperty('display');
  });
  p.appendChild(button);
  
  const form = document.querySelector('main form');
  form.parentNode.insertBefore(p, form);
  
})();