Change all links with Greasemonkey

I want to put a page from a site that prefixes all its links with http://linkblur.com/ ? "I tried this:

links = getElementsByTagName('a'); for (l in links) { l.href = l.href.replace('http://linkblur.com/?',''); } 

But that will not work. What am I doing wrong?

+3
source share
2 answers

Try:

 var links = document.links; var link; for(var i=links.length-1; i >=0; i--){ link = links[i]; link.href = link.href.replace("http://linkblur.com/?", ''); } 
+2
source

You are for an iterator to iterate over all the properties of your array, which will not be separate elements, but rather 0 , 1 , 2 , ..., n , length .

You want to change your iterator, and if you want a link prefix, you are doing it wrong too. What you are doing now will replace linkblur.com... an empty string, i.e. Remove the link from existing links.

 var links = document.getElementsByTagName('a'); for (var i = 0; i < links.length; i++) { links[i].href = 'http://linkblur.com/?' + links[i].href; } 
+2
source

Source: https://habr.com/ru/post/1316186/


All Articles