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
CodePudding user response:
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
CodePudding user response:
Given Tab.url is a plain string, you can run it through the URL
constructor and get the origin from that
// just for the demo
const tabs = [{
url: "https://stackoverflow.com/questions/73524018/chrome-extension-how-to-make-my-tabs-query-return-the-url-without-anything-afte",
}, {
url: "https://example.com/foo/bar/baz?msg=Hello World",
}];
const origins = tabs.map(({ url }) => new URL(url).origin);
console.log(origins);