Javascript 获取当前url的域和路径

Javascript 获取当前url的域和路径,javascript,google-chrome-extension,Javascript,Google Chrome Extension,我有一个网址。我想获取当前url的域(example.com)和路径(example.com/folder/) 使用 chrome.tabs.onUpdated.addListener(function(tabid, changeInfo, tab){ chrome.tabs.query({'active' : true, 'currentWindow': true}, function(tabs){ let newUrl = new URL(tabs[0].url);

我有一个网址。我想获取当前url的域(
example.com
)和路径(
example.com/folder/

使用

chrome.tabs.onUpdated.addListener(function(tabid, changeInfo, tab){
    chrome.tabs.query({'active' : true, 'currentWindow': true}, function(tabs){
        let newUrl = new URL(tabs[0].url);
        currentDomain = newUrl.hostname;
});

导致我获得主机-
www.example.com
而不是
example.com
www
与其他子域一样,因此JS无法知道它是否是域的一部分。但您可以手动删除
www

currentDomain = newUrl.hostname.replace(/^www\./, "");
要获取路径,请使用
pathname

const currentPath = `${currentDomain}/${newUrl.pathname}`

嗯,我需要纯域-没有任何子域,不仅仅是www。有没有办法只获取域?那更复杂,因为你不知道域本身有多少“部分”。它可以是example.com,也可以是example.co.uk,也可以是www.yt.be。您可以使用此处建议的解决方案:
pathname
获取
/folder/index.html
——但我只需要
/folder/
。这是可以实现的吗?