A Drupal path redirect bookmarklet

I threw together a fancy little JavaScript bookmarklet to create redirects in Drupal, and figured I’d share.

For this to work you need the path redirect and prepopulate modules installed… Then simply replace example.com in the snippet below with your domain name, and save the link as a browser bookmark.

<a href="javascript:d='example.com/';location.href='http://'+d+'admin/build/path_redirect/new?edit[redirect][redirect]='+encodeURIComponent(location.href.split(/[?#]/)[0].replace(RegExp('https?://'+d,'ig'),''))+'&amp;edit[redirect][query]='+encodeURIComponent(location.href.indexOf('?')&amp;gt;-1?location.href.split(/[?#]/)[1]:'')+'&amp;edit[redirect][fragment]='+encodeURIComponent(location.href.indexOf('#')&amp;gt;-1?location.href.split(/#/)[1]:'')+'&amp;edit[path]='+encodeURIComponent(String(''+(window.getSelection?window.getSelection():document.getSelection?document.getSelection():document.selection.createRange().text)).replace(/[^a-zA-Z0-9]+/g,'-').toLowerCase());">new path redirect</a>

Now click on it, and it will add a redirect to the current page. If you select any text before clicking the bookmarklet, it will use that text for the redirect path. This bookmarklet supports both internal (Drupal) and external paths. It also works with urls containing queries (?foo=bar) and fragments (#baz). Tested in IE, Firefox and Safari.

Have fun!