[Greasemonkey] URL redirects

Alf Eaton alf at hubmed.org
Sat Apr 23 01:52:28 EDT 2005


I've been using a userscript (see below), ported from PithHelmet rules,  
to redirect URLs based on matching various patterns. It's working ok,  
but I have a couple of questions:

1) Is there an easier way to write these patterns, perhaps as an array  
of 'matched URL', 'redirect URL' pairs that can be looped through?

2) Might Greasemonkey be able to hook into an earlier Firefox event for  
the purpose of URL redirecting, as at the moment the redirect doesn't  
happen until the original page has finished loading (ie like  
PithHelmet, which rewrites the URL immediately)? OnLocationChange  
<http://forums.mozillazine.org/viewtopic.php?t=49716> perhaps? I'm  
actually a bit surprised there isn't already a specific Firefox  
extension for doing this.

alf.


==========================

var href = window.location.href;

// redirect URLs from one site to another
if  
(href.match(/^http\:\/\/www.ncbi.nlm.nih.gov\/entrez\/ 
query.fcgi\?.*&list_uids\=(\d*)/)){
      
window.location.href='http://www.hubmed.org/display.cgi? 
uids='+RegExp.$1;
     return;
}

// skip Sourceforge download page
if (href.match(/^http\:\/\/prdownloads.sourceforge.net\/(.*)/)){
      
window.location.href='http://ovh.dl.sourceforge.net/sourceforge/ 
'+RegExp.$1;
     return;
}

// skip PDF reprint loading frames
if (href.match(/^(http.*\/cgi\/reprint\/.+[^pdf])$/)){
     window.location.href=RegExp.$1+'.pdf';
     return;
}

// redirect journal sites through university proxy
var sites = new Array(
     '^(http\:\/\/www\.nature\.com)(\/.*)$',
     '^(http\:\/\/www\.pnas\.org)(\/.*)$',
     '^(http\:\/\/www\.annualreviews\.org)(\/.*)$',
     '^(http\:\/\/www\.sciencemag\.org)(\/.*)$',
     '^(http\:\/\/www\.cell\.com)(\/.*)$'
);

for (i = 0; i < sites.length; i++){
     if (href.match(sites[i])){
         window.location.href=RegExp.$1+'.example.proxy.fr'+RegExp.$2;
         return;
     }
}

============================



More information about the Greasemonkey mailing list