Discussions » Creation Requests

[Request] Add values together based on item number.

§
Posted: 23/12/2020

Trying to get a script written that totals dollar values of all the items on the page based on their item number and have the total displayed under the value of every item of the same item number. Code below (there will be about 30 entries of this same code with different item numbers and dollar amount).

<td class="table-grid-cell table-grid-cell-first-row recent-billings-date" colspan="" rowspan=""><div>Dec 22 2020 18:05:16</div></td><td class="table-grid-cell table-grid-cell-first-row recent-billings-description" colspan="" rowspan=""><div><div><span class="sh-bold">Insertion Fee</span><span class=""> for item number: </span><span class="">12453466</span><span class="sh-superscript">PROMO</span></div><div><span class="">Random Text</span></div><div class="green-color"><span class="">You saved $0.10 by promo</span></div></div></td><td class="table-grid-cell table-grid-cell-first-row recent-billings-amount" colspan="" rowspan=""><span class="recent-billings-amount-bold">$0.00</span></td> 

I'd like the out put of the values to look like this:

$0.00<p>Total $10.00</p>
§
Posted: 23/12/2020

https://i.paste.pics/2e6e70a3d7bf712ca3ca02d3f67c59a6.png

// ==UserScript==
// @name Total Calculator
// @namespace TotalCalculator
// @version 0.1
// @description Show the Total Calculation Results
// @author hacker09
// @include *
// @run-at document-end
// @grant none
// ==/UserScript==

(function() {
'use strict';
var Dollars, Cents; //Make the variables global
var TotalDollars = 0; //Make the variable global with the value of 0
var TotalCents = 0; //Make the variable global with the value of 0
var Lines = document.querySelectorAll("span.recent-billings-amount-bold"); //Create a variable to hold the number of the total amount of elements
for (var i = Lines.length; i--;) { //Starts the for condition
var IncreasyBy; //Create a null variable
Dollars = Lines[i].innerText.split('.')[0].replace('$',''); //Add the Dollars value to the variable and remove the $ symbol
Cents = Lines[i].innerText.split('.')[1]; //Add the Cents value to the variable
TotalDollars += parseInt(Dollars); //Sum the Amount of Dollars and add the result to another variabke
TotalCents += parseInt(Cents); //Sum the Amount of Cents and add the result to another variabke
} //Finishes the for condition
console.log('The Total Amount is $'+TotalDollars+'.'+TotalCents); //Display the total result on the browser console
})();

Post reply

Sign in to post a reply.