[Greasemonkey] URL redirects

Alf Eaton alf at hubmed.org
Sun Apr 24 18:59:43 EDT 2005


That's a nice idea - I decided to simplify it a bit to make this, which  
is what I'm using at the moment:

---------------------

var rules = new Array(
     [/^(http\:\/\/www\.nature\.com)(\/.*)$/, '$1.example.proxy.fr$2'],
     [/^(http\:\/\/www\.pnas\.org)(\/.*)$/, '$1.example.proxy.fr$2'],
     [/^(http\:\/\/www\.annualreviews\.org)(\/.*)$/,  
'$1.example.proxy.fr$2'],
     [/^(http\:\/\/www\.sciencemag\.org)(\/.*)$/,  
'$1.example.proxy.fr$2'],
     [/^(http\:\/\www\.cell\.com)(\/.*)$/, '$1.example.proxy.fr$2'],
      
[/^http\:\/\/www.ncbi.nlm.nih.gov\/entrez\/ 
query.fcgi\?.*&list_uids\=(\d*)/,  
'http://www.hubmed.org/display.cgi?uids=$1'],
     [/^http\:\/\/prdownloads.sourceforge.net\/(.*)/,  
'http://ovh.dl.sourceforge.net/sourceforge/$1'],
     [/^(http.*\/cgi\/reprint\/.+[^pdf])$/, '$1.pdf'],
);

var href = window.location.href;

for (i = 0; i < rules.length; i++){
     var replaced = href.replace(rules[i][0], rules[i][1]);
     if (replaced != href){
         window.location.href = replaced;
         return;
     }
}

---------------------

alf.

On Apr 23, 2005, at 03:55, Edward Lee wrote:

>> 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?
>
> Here you go. Because it uses replace to match and use $backreferences,
> make sure to match the whole location because otherwise the original
> location will be part of the redirected url. Such as matching
> 'http://gmail.google.com' with 'http://gmail.google.com/gmail?12345'
> and replacing it with 'https://gmail.google.com' will make it
> 'https://gmail.google.com/gmail?12345'.
>
> (function() {
> var redirects = new Array();
> function addRedirect(match, target) {
>   if (typeof(match) != 'object') {
>     match = [match];
>   }
>   redirects.push({
>     match: match,
>     target: target
>   });
> }
>
> addRedirect(/^http\:\/\/www.ncbi.nlm.nih.gov\/entrez\/ 
> query.fcgi\?.*&list_uids\=(\d*)/,
> 'http://www.hubmed.org/display.cgi?uids=$1');
> addRedirect(/^http\:\/\/prdownloads.sourceforge.net\/(.*)/,
> 'http://ovh.dl.sourceforge.net/sourceforge/$1');
> addRedirect(/^(http.*\/cgi\/reprint\/.+[^pdf])$/, '$1.pdf');
> addRedirect([/^(http\:\/\/www\.nature\.com)(\/.*)$/,
> /^(http\:\/\/www\.pnas\.org)(\/.*)$/,
> /^(http\:\/\/www\.annualreviews\.org)(\/.*)$/,
> /^(http\:\/\/www\.sciencemag\.org)(\/.*)$/,
> /^(http\:\/\/www\.cell\.com)(\/.*)$/], '$1.example.proxy.fr$2');
>
> var _ = window.location;
> for (var i = 0; i < redirects.length; i++) {
>   var match = redirects[i].match;
>   var target = redirects[i].target;
>   for (var j = 0; j < match.length; j++) {
>     var tmp = _.href.replace(match[j], target);
>     if (tmp != _.href) {
>       _.href = tmp;
>       return;
>     }
>   }
> }
> }) ();



More information about the Greasemonkey mailing list