GreaseMonkey Script To Sum Selected Numbers In A Web Page

(Originally posted 2014-12-13.)

This post is meant to inspire people who like programming the web to do simple tasks. It contains a sample Firefox GreaseMonkey [1] script, which I hope you will find useful. [2]

Suppose you are looking at a web page, perhaps one with a table in, and you want to add up some numbers you see there. Perhaps they’re in a column in that table.

With this script you select the numbers you’re interested it and push the “Sum Up” button that appears at the top.

Here’s the script:

    // ==UserScript==
    // @name        swipeCalc
    // @namespace   MGLP
    // @description Does calculations on selected text
    // @version     0.0
    // ==/UserScript==

    input.value="Sum Up";
    input.onclick = showResult;

    function showResult()
      // Get array of space- and range-separated tokens
        var tokens=[]
            rangeTokens=selection.getRangeAt(r).toString().split(" ")

        // Sum up any detected number values
        alert("Sum: "+tally+" Average: "+tally/count+" Minimum: "+minimum+" Maximum: "+maximum)

Everything up to the function definition is code to insert a button at the top of every page, with the words “Sum Up” on it. [3] When you push the button it invokes the function “showResult”

Most of the code is the function “showResult”, which is actually quite simple:

  • First we break up the (perhaps disjoint) selected text into words.
  • Then we loop through all the tokens, using parseFloat() to turn them into floating-point numbers. [4]
  • For each valid floating point we use it to contribute to the sum, average, maximum and minimum tallies.
  • We display in an alert the statistics we’ve computed.

I hope this script doesn’t look intimidating. Personally I intend to build on it – as there’s so much more we can do with GreaseMonkey.

When I wrote about GreaseMonkey in 2005 I didn’t then realise I’d be using it to prototype Firefox Extensions. I just might do that here. But, of course, time is fleeting and there are lots of challenges out there, with more appearing (it seems) every day.

  1. Greasemonkey is a Firefox extension. I first wrote about it here (in 2005): GreaseMonkey  ↩

  2. Feel free to swipe it and build on it.  ↩

  3. You might not like the behaviour of adding the button to all pages. GreaseMonkey makes it easy to control that.  ↩

  4. parseFloat is a built-in javascript function that treats anything up to the first non-number-related character as a valid floating point number. So “1234X” would be treated as 1234. It’s not quite up to the most sophisticated of requirements but it’s simple and useful.  ↩

Published by Martin Packer

I'm a mainframe performance guy and have been for the past 35 years. But I play with lots of other technologies as well.

One thought on “GreaseMonkey Script To Sum Selected Numbers In A Web Page

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: