Remove/delete a part of link in a href

This is a website eg: example.com. In this website there are links like linkshrink.net/a123=example.com.

What I want to do is replace /linkshrink.net/a123= this part with “/”, i.e. just a slash, which will remove that ad go direct to the link.

All I want to do is just find and replace certain text in “href” in the whole page, when the page is getting loaded each time.

I was able to copy script for Linkshrink.net

var links = document.links;
for( i=0; i<document.links.length; i++ )
{
 	var pattern = /http:\/\/linkshrink.net\/[a-zA-Z0-9]*\=/;
    if(pattern.test(document.links[i].href) )
    {
        
        document.links[i].href = document.links[i].href.substr(document.links[i].href.indexOf("=")+1);
    }
}

Above Code worked very well with linkshrink.net
My Link was linkshrink.net/a123=example.com
Now In my web page it shows example.com

Now i tried on other site, its not working:
bc.vc/13123/example.com

var links = document.links;
for( i=0; i<document.links.length; i++ )
{
 	var pattern = /http:\/\/bc.vc\/[a-zA-Z0-9]*\=/;
    if(pattern.test(document.links[i].href) )
    {
        
        document.links[i].href = document.links[i].href.substr(document.links[i].href.indexOf("=")+1);
    }
}

Where did i go wrong ? linkshrink link ends with “=” sign , however bc.vc ends with “/” ,
I also tried to replace = with /
but since there are multiple slash signs “/” it messes up the whole link

Link in href: http://bc.vc/12312/http://example.example.eu/asd123asd/asd12asd/
want to delete http://bc.vc/12312/ from all links in the page

It worked for linkshrink , but not working for bc.vc? any ideas?

So you want to grab all the links on your page and remove the preceding http://bc.vc/12312/ from each of them (if present)?

So:

  • http://bc.vc/12312/https://sitepoint.com/ would become https://sitepoint.com/
  • http://bc.vc/12312/https://google.com/ would become https://google.com/

and so on…

Did I get that right

yes you got it right,

links can be
http://bc.vc/12312/mainlink or
http://sh.st/sh/mainlink or
http://linkshrink.net/asd23=mainlink

just want to delete the link b4 mainlink in the whole page

Ok, in that case, could you not look for the second occurrence of http(s):// and disregard anything before that?

Something like this:

[...document.links].forEach(
  (a) => {
    const arr = a.href.split(/(https?:\/\/)/);
    if(arr.length > 3) a.href = `${arr[3]}${arr[4]}`;
  }
);

Here’s a complete example you can run:

<!DOCTYPE HTML>
<html lang="en">
  <head>
    <meta chraset="utf-8">
    <title>Strip ads from link</title>
  </head>
  <body>
    <a href="http://bc.vc/12312/https://sitepoint.com/">Link 1</a><br>
    <a href="http://sh.st/sh/https://soundcloud.com/you/likes">Link 2</a><br>
    <a href="https://linkshrink.net/asd23=http://stackoverflow.com/questions/21017867/how-to-validate-for-money-in-jquery/21018113#21018113">Link 3</a><br>
    <a href="https://stackedit.io/">A normal link</a><br>

    <script>
      [...document.links].forEach(
        (a) => {
          const arr = a.href.split(/(https?:\/\/)/);
          if(arr.length > 3) a.href = `${arr[3]}${arr[4]}`;
        }
      );
    </script>
  </body>
</html>

for your code , the main links in href should be same ??? , coz my main links are actually download links with each different link.

like http://bc.vc/12312/https://sitepoint.com/1.mkv
http://bc.vc/12312/https://sitepoint.com/2.mkv
http://bc.vc/12312/https://sitepoint.com/3.mkv

and also the script will be saved as .js file and loaded in tampermonkey

and some time http:// is not there in main links , they start from sitepoint.com/1.mkv

I think a code which will trim/delete all text before mainlink will be better? may be using index.of ???

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.