Bookmarklet to modify links on a page

In: Uncategorized

5 Jan 2010

I recently needed to modify hundreds of links on a wiki page based on a dynamic value, so I created a simple bookmarklet (ModWikiLinks).

I searched for a tool to properly format/shrink it and found: the bookmarklet crunchinator.

var user = prompt('Enter your username');
var links = document.querySelectorAll('a[href*=localhost]');
for (i in links) {
 if (typeof links[i].href !== "undefined") {
  links[i].href = links[i].href.replace(/http:\/\/[^\/]+/,'http://trus_global.'+user+'.webdev.gspt.net');
  links[i].target = '_blank';
 }
}