Search code examples
javascriptgoogle-chrome-extension

Javascript need website URLS shortening down


I am fetching all the websites links and putting them into a array where I will eventually compare them with a JSON list. I need the websites to be shortened down to

Examples (What I'm trying to get the array to look like)

  • google.com
  • computers.intercom-clicks.com
  • mad-websites.ru
  • just-eat.com

Example links

My code currently only shortens some websites down while others are still the same. It will also remove any duplicates.

  const links = Array.from(document.querySelectorAll(".a3s a")).map(link => {
      const url = new URL(link.href);
      url.search = '';
      return url.toString(); 
     
  })
  //removes duplicate links
  const uniq = [...new Set(links)];
  //window.alert(uniq);
  return warningPopup(uniq);
}

Solution

  • Is this what you look for?

    const links = Array.from(document.querySelectorAll(".a3s a")).map(link => {
      const url = new URL(link.href);
      return url.hostname;
    })
    
    //removes duplicate links
    const uniq = [...new Set(links)];
    
    document.write(uniq.join(', '))
    <div class="a3s">
      <a href="https://mad-websites.ru/via/e?ob=RohpF3uuLGksOJfxJOwcgRL5vknYi4kC2aQRzvu2v3s%3D&h=04ce1caed8c7cf4b69d751230eaf7a2450660d67-o26qxr01_77963700909352&l=6ef96bea4775c44a5bc10cdaa661c5053819c0b8-7456283"></a>
      <a href="https://notifications.google.com/g/p/AD-FnEwlAH83isfsH0zLOoNuynSmz1pMuK9Y8guqew5CkdyaEu28Zu30iRcw-SI6y7LRO7v8Tqy6p_9LhGcQClO1e2P5WYSVNa9dWPVhmA"></a>
      <a href="https://finance.rambler.ru/?utm_source=head&utm_campaign=self_promo&utm_medium=topline&utm_content=finance_media"></a>
      <a href="https://www.google.com/covid19?utm_source=Google-Maps-timeline&utm_medium=email&utm_campaign=COVID-site-promo"></a>
    </div>