javascript Google Chrome 插件:如何从 URL (tab.url) 获取域

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3689423/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 01:56:58  来源:igfitidea点击:

Google Chrome Plugin: How to get domain from URL (tab.url)

javascriptgoogle-chromegoogle-chrome-extension

提问by OtoNoOto

Using the Google Chrome API's tab.urlvalue, what is the best method to get just the domain from the entire value?

使用 Google Chrome API 的tab.urlvalue,从整个 value 中获取域的最佳方法是什么?

In JavaScript I would use window.location.protocol& window.location.hostname. For example something like this:

在 JavaScript 中,我会使用window.location.protocol& window.location.hostname。例如这样的事情:

var domain = window.location.protocol + "//" + window.location.hostname;

But that gets the extension domain and not the tab so cannot use that method. So with a function similar to the one below... How would I strip just the domain from the tab.urlvalue?

但这会获得扩展域而不是选项卡,因此不能使用该方法。因此,使用类似于下面的函数......我如何从tab.url值中剥离域?

function show_alert() {
    chrome.tabs.getSelected(null, function(tab) {
        var currentURL = tab.url;
        alert(currentURL);
    });
}

回答by Ross Zurowski

Since this question was originally answered, a better solution has appeared.

既然最初回答了这个问题,就出现了更好的解决方案。

Most modern browsers now support use of the URLconstructor, which provides access to href, hostname, pathand all standard ways of splitting up a URL.

大多数现代浏览器都支持使用的URL构造函数,它提供了访问hrefhostnamepath和分裂的一个URL所有标准的方式。

To get the domain, you could do the following:

要获取域,您可以执行以下操作:

chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
  var tab = tabs[0];
  var url = new URL(tab.url)
  var domain = url.hostname
  // `domain` now has a value like 'example.com'
})

回答by Eli Grey

First of all, domains don't include a protocol. I have created a regular expression for your problem. To get the hostname (you'd want to do this as IP addresses are not domains) of a URI, use the the following:

首先,域不包含协议。我为您的问题创建了一个正则表达式。要获取 URI 的主机名(您希望这样做,因为 IP 地址不是域),请使用以下命令:

var domain = uri.match(/^[\w-]+:\/{2,}\[?([\w\.:-]+)\]?(?::[0-9]*)?/)[1];
// Given uri = "http://www.google.com/", domain == "www.google.com"

If you want the origin (protocol + host (not hostname, there's a difference) + optional port) instead of the domain, use the following:

如果您想要源(协议 + 主机(不是主机名,有区别)+ 可选端口)而不是域,请使用以下内容

var origin = uri.match(/^[\w-]+:\/{2,}\[?[\w\.:-]+\]?(?::[0-9]*)?/)[0];
// Given uri = "http://www.google.com/", origin == "http://www.google.com"

回答by kinghat

chrome.tabs.query(
  {
    active: true,
    currentWindow: true
  },
  ([currentTab]) => {
    const url = new URL(currentTab.url);
    const domain = url.hostname;
    console.log(domain);
  }
);

combination of: https://stackoverflow.com/a/37486863/8023318but not using the depreciated getSelectedand https://stackoverflow.com/a/48755190/8023318. would like to see if this can be done via getCurrent.

组合:https://stackoverflow.com/a/37486863/8023318但不使用折旧getSelectedhttps://stackoverflow.com/a/48755190/8023318。想看看这是否可以通过getCurrent.