Balanz ccl new web

CCL = (mejor venta en pesos) / (mejor compra cable). Escribe el resultado en la columna #3

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 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         Balanz ccl new web
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  CCL = (mejor venta en pesos) / (mejor compra cable). Escribe el resultado en la columna #3
// @author       https://github.com/jose-velarde
// @match        https://clientes.balanz.com/*
// @grant        none
// @run-at       context-menu
// @license MIT 
// ==/UserScript==

(function () {
  'use strict';

  function parseArNumber(raw) {
    if (!raw) return NaN;
    let s = String(raw).trim();

    // Por si viene "40172 × 998,70"
    if (s.includes('×')) s = s.split('×')[1].trim();

    // Quitar separador de miles y pasar coma a punto
    s = s.replace(/\./g, '').replace(',', '.');

    // Limpiar cualquier cosa no numérica (por si hay "usd", etc.)
    s = s.replace(/[^0-9.]/g, '');

    return parseFloat(s);
  }

  const rows = document.querySelectorAll('tbody tr.d-flex.d-md-table-row.ng-star-inserted');

  let compraCable = NaN;
  let ventaPesos = NaN;

  for (const row of rows) {
    const t = row.querySelector('td:nth-child(2) a span')?.textContent?.trim();
    if (!t) continue;

    // precio "Vender" (izquierda) y "Comprar" (derecha)
    const sellPriceText = row.querySelector('app-button-actions .btn-outline-danger')  // botón Vender
      ?.closest('div.w-50')?.querySelector('b.exchange-lower')?.textContent;

    const buyPriceText = row.querySelector('app-button-actions .btn-outline-success') // botón Comprar
      ?.closest('div.w-50')?.querySelector('b.exchange-upper')?.textContent;

    const venta = parseArNumber(sellPriceText);
    const compra = parseArNumber(buyPriceText);

    // dónde escribir el CCL (tu "ultCol")
    const outCell = row.querySelector('td:nth-child(3) div');

    if (t.endsWith('C')) {
      compraCable = compra;

      if (outCell && Number.isFinite(ventaPesos) && Number.isFinite(compraCable) && compraCable !== 0) {
        outCell.textContent = (ventaPesos / compraCable).toFixed(2);
      } else if (outCell) {
        outCell.textContent = '';
      }
    } else {
      ventaPesos = venta;
      if (outCell) outCell.textContent = '';
    }
  }
})();