The idea is to route the outgoing links through DDG's redirect service, to conceal my site in referrers. Is there anything wrong with the script as it is?
Also, see the comment in the code... Is the mentioned alternative superior? Inferior? Identical?
Thanks again!
var baseUrl='https://duckduckgo.com/l/?u=';
var invisibleHost=window.location.hostname;
var pageLinks=document.getElementsByTagName('a');
var n_links=pageLinks.length;
var leaveAsIs=true;
for(var i=0;i<n_links;i++){
var presentLink=pageLinks[i].href;
leaveAsIs=true;
if(/^https?:\/\//i.test(presentLink) && invisibleHost.test(presentLink))
leaveAsIs=false;
pageLinks[i].href=leaveAsIs?presentLink:baseUrl+presentLink;
}
always use test()
unless you need to return, rather than simply test for, a match.
Also, your nested if conditions could be one, multi-condition, and you don't need two REGEXs for http and https - just use one but stipulate that the 's' is optional:
/^https?:\/\//i.test(presentLink)