tags:

views:

23

answers:

2

I'm trying to write a Greasemonkey script for a heirarchy of websites such that I have a bunch of code modifications for http://www.foo.com/*, then more specific ones for http://www.foo.com/bar/*, and still others for http://www.foo.com/foobar/*.

Is there anyway for me to write all these in the same script, or do I have to make multiple?

+1  A: 

Is there anyway for me to write all these in the same script, or do I have to make multiple?

Yes, just use those three @includes, then in your user script do something like (depends on specifics of script):

var currentURL = (document.location+'');
if (currentURL .match(/http:\/\/www\.foo\.com\/foobar\/.*/)) {
  // do stuff for page set A
} else if (currentURL .match(/http:\/\/www\.foo\.com\/foo\/.*/)) {
  // do stuff for page set B
} else if (currentURL .match(/http:\/\/www\.foo\.com\/.*/)) {
  // do stuff for page set C
}
Erik Vold
+1  A: 

One nifty trick I was shown for dealing with different functions at different sub-locations is to use the global directory of function names as a sort of virtual switchboard...

// do anything that is supposed to apply to the entire website above here.
var place = location.pathname.replace(/\/|\.(php|html)$/gi, "").toLowerCase();
// the regex converts from "foo/" or "foo.php" or "foo.html" to just "foo".
var handler;
if ((handler = global["at_" + place])) {
    handler();
}

// end of top-level code.  Following is all function definitions:
function at_foo() {
    // do foo-based stuff here
}
function at_foobar() {
    // do foobar stuff here.
}
Hellion