Search code examples
regextagsbookmarkletrelfolksonomy

rel-tag bookmarklet for last path component of a URL


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.


Solution

  • 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.