Оценок пока нет JavaScript add target to __blank on page if it is external

const url = {}

url.isExternal = function(link){
    var domain = function(link) {
        return link.replace('http://','').replace('https://','').replace('//','').split('/')[0];
    }   
    return domain(location.href) !== domain(link)
}

url.replaceAllLinks = function(html, link){
    let regex = /href\s*=\s*(['"])(https?:\/\/.+?)\1/ig;
    let l;
    while((l = regex.exec(html)) !== null) {
        html = html.replace(l[2], link);
    }
    return html;
}

url.addTargeBlankToExternal = function(html){
    let regex = /href\s*=\s*(['"])((https|http)?:\/\/.+?)\1/ig;
    let link;
    while((link = regex.exec(html)) !== null) {
      console.log(link[2])
      if(url.isExternal(link[2])){
          html = html.replace(link[2] + "\"", link[2] + "\" target=\"__blank\"");
      }
    }
    return html;
}



export default url

Пожалуйста, оцените материал

WebSofter

Web - технологии