views:

312

answers:

3

Many web sites support folksonomy tags. You may have heard of rel-tag, where it says that "The last path component of the URL is the text of the tag".

I am looking for a bookmarklet or greasemonkey script (javascript) to get the "last path component" for the URL currently being viewed in the browser, add that tag into another URL, and then open that page in a new tab or window.

For example, if I am looking at a delicious.com page with the tag "foo", I may want to create a new URL with the tag "foo". This should also work for multiple tags in the last path component, such as, foo+bar.

Some regexp suggestions have been offered.

A: 

If you can assume both your URLs to be valid, you can get the tag from the first URL with this regex:

^[a-z]+://[^/#?]+/[^#?]*?([^#?/]+)(?:[#?]|$)

The first (and only) capturing group will hold the tag. This regex won't match URLs that don't have any tags.

To append the tag to another URL, search for the regex:

^([^#?]*?)/?(?:[#?]|$)

and replace with:

$1/tag

This regex makes sure not to end up with two adjacent slashes in the URL if the path of the original URL ends with a slash.

Jan Goyvaerts
+1  A: 

Since you're using JavaScript, there's no need to worry about hostnames, querystrings, etc - just use location.pathname to get at the important bit.

For example:

var NewUrl = 'http://technorati.com/tag/';
var LastPart = location.pathname.match( /[^\/]+\/?$/ );
window.open( NewUrl + LastPart );

That allows for a potential single trailing slash.

You can use /[^\/]+\$/ to disallow trailing slashes, or /[^\/]+\/*$/ for any number of them.

Peter Boughton
Thanks. I pasted the script above into http://userjs.up.seesaa.net/js/bookmarklet.html and it generated a bookmarklet that works for a single tag, but (in technorati) did not work for more than one tag (e.g., /foo+bar). Apparently technorati wants single tags. However, setting NewUrl to http://delicious.com/tag/ works well. I use it now when on an intranet social bookmarking site and I want to see the same tags on delicious.
pauld
A: 

implementation, as in how the servers are set up, all that jazz? I'm not very knowledgeable about that stuff =\ ahh that sounds

kmirhfew