(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=document.createElement("input")
input.type="button";
input.value="Sum Up";
input.onclick = showResult;
document.body.insertBefore(input,document.body.firstChild)
function showResult()
{
// Get array of space- and range-separated tokens
selection=window.getSelection()
var tokens=[]
for(r=0;r<selection.rangeCount;r++){
rangeTokens=selection.getRangeAt(r).toString().split(" ")
for(t=0;t<rangeTokens.length;t++){
tokens.push(rangeTokens[t])
}
}
// Sum up any detected number values
tally=0
count=0
maximum=Math.max()
minimum=Math.min()
for(t=0;t<tokens.length;t++){
tokenValue=parseFloat(tokens[t])
if(!isNaN(tokenValue)){
tally+=tokenValue
count++
maximum=Math.max(maximum,tokenValue)
minimum=Math.min(minimum,tokenValue)
}
}
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.
-
Greasemonkey is a Firefox extension. I first wrote about it here (in 2005): GreaseMonkey ↩
-
Feel free to swipe it and build on it. ↩
-
You might not like the behaviour of adding the button to all pages. GreaseMonkey makes it easy to control that. ↩
-
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. ↩
One thought on “GreaseMonkey Script To Sum Selected Numbers In A Web Page”