I have a Greasemonkey script that keeps track of different things on unicreatures.com.
One of the things I wanted to count was clicks on some links on the page, but not all links.
These need counted,
http://unicreatures.com/explore.php?area=sea&id=89&key=bf12
These should not be counted,
http://unicreatures.com/explore.php?area=sea&gather=5&enc=394844&r=
Someone helped me figure out a regexp that did what I wanted, but I had to code each different explore location (area=**) into it, so I decided that wouldn’t work.
The regexp version
var links = document.getElementsByTagName( 'a' );
for ( var i = 0; i < links.length; i++ ) {
var link = links[i];
if ( /area=sea(?!\&gather)/.test( link.href )) {
link.addEventListener( 'click', function () {
localStorage.steps=Number(localStorage.steps)+1
// alert(localStorage.steps + ' in Sargasso' );
}, true );
}
}
Obviously I don’t want a billion if statements for the different values of area=, and I couldn’t find a way to add a variable to a regexp.
So I finally found some string manipulation commands and put together this:
var url = window.location.href;
var startOf=url.indexOf("=")+1;
var endOf=url.indexOf("&");
var loc =url.substring(startOf,endOf);
var links = document.getElementsByTagName( 'a' );
for ( var i = 0; i < links.length; i++ ) {
var link = links[i];
if (url.indexOf("area=")>=0 && url.indexOf("gather=")<0) {
link.addEventListener( 'click', function () {
localStorage.steps=Number(localStorage.steps)+1
localStorage[loc+"Steps"]=Number(localStorage[loc+"Steps"])+1
alert(localStorage[loc+"Steps"] +" in local"+loc);
}, true );
}
}
For some reason it counts even when the second condition is false. Is this a simple case of me getting the syntax wrong somewhere, or is this a Greasemonkey bug? I don’t get any errors in the console.
Try just to tweak your regexp version, instead of this:
use this regex:
that will match all links that have an ‘area’ parameter followed by an ‘id’ parameter, which seems enough to match the links you want.