Search code examples
javascriptgoogle-chrome-extension

Chrome extension: How to make my tabs query return the URL without anything after the .com/


I would like to get the URL as the website's domain name rather than having the extra parts after the ".com/"

Currently when I query tabs with chrome.tabs.query(params, function(){...}); the URL will still contain all the content about the specific page rather than just being the website's domain

How should I go about this so that it will also work with .ca or .gov as well? Is there a trick to shrink the string after the domain ends or will I have to look for string patterns in the URL for each different ending


Solution

  • You can parse the result and extract the domain name from it.

    let result = 'https://example.com/some/after?=after:stuf&';
    const url = new URL(result);
    const domain = url.hostname;
    console.log(domain);
    

    OUTPUT

    example.com