【问题标题】:Get subdomain and load it to url with greasemonkey获取子域并使用greasemonkey将其加载到url
【发布时间】:2009-08-15 20:07:48
【问题描述】:

我的网址是 http://somesubdomain.domain.com(子域可能不同,域始终相同)。需要获取子域并使用greasemonkey 重新加载带有domain.com/some/path/here/somesubdomain 之类的页面(或使用URL domain.com/some/path/here/somesubdomain 等打开一个新窗口)。

【问题讨论】:

    标签: javascript url greasemonkey


    【解决方案1】:
    var full = window.location.host
    //window.location.host is subdomain.domain.com
    var parts = full.split('.')
    var sub = parts[0]
    var domain = parts[1]
    var type = parts[2]
    //sub is 'subdomain', 'domain', type is 'com'
    var newUrl = 'http://' + domain + '.' + type + '/your/other/path/' + subDomain
    window.open(newUrl);
    

    【讨论】:

    • 如果域名是 google.co.uk 怎么办?这将返回错误的子域。
    • 只获取子域:window.location.host.split('.')[0];
    • @TLK 是对的,Philip 的解决方案只有在子域始终不包含点的情况下才有效。有时子域中有点,形成子子域等。我在下面提供了一个更完整(也更痛苦,可悲的是)的解决方案。
    • @TLK 是的,如果域是“sub1.sub2.domain.com”,那也是错误的
    【解决方案2】:

    Derek 提供的答案适用于最常见的情况,但不适用于“xxx.xxx”子域或“host.co.uk”。 (同样,使用 window.location.host,也将检索未处理的端口号:http://www.w3schools.com/jsref/prop_loc_host.asp

    说实话,我看不到这个问题的完美解决方案。 就个人而言,我创建了一种我经常使用的主机名拆分方法,因为它涵盖了更多的主机名。

    此方法将主机名拆分为{domain: "", type: "", subdomain: ""}

    function splitHostname() {
            var result = {};
            var regexParse = new RegExp('([a-z\-0-9]{2,63})\.([a-z\.]{2,5})$');
            var urlParts = regexParse.exec(window.location.hostname);
            result.domain = urlParts[1];
            result.type = urlParts[2];
            result.subdomain = window.location.hostname.replace(result.domain + '.' + result.type, '').slice(0, -1);;
            return result;
    }
    
    console.log(splitHostname());
    

    此方法仅将子域作为字符串返回:

    function getSubdomain(hostname) {
            var regexParse = new RegExp('[a-z\-0-9]{2,63}\.[a-z\.]{2,5}$');
            var urlParts = regexParse.exec(hostname);
            return hostname.replace(urlParts[0],'').slice(0, -1);
    }
    
    console.log(getSubdomain(window.location.hostname));
    // for use in node with express:  getSubdomain(req.hostname)
    

    这两种方法适用于最常见的域(包括 co.uk) 注意:子域末尾的slice 是为了去掉多余的点。

    我希望这能解决你的问题。

    【讨论】:

    • 我使用了 Dereks 解决方案,突然没有子域时遇到了麻烦。在我的情况下,弗拉德的回答效果更好。
    • 100% 准确的答案是 jlbang 在这个页面上的答案。 Filip 的 anwser 占用空间更小,但非常不完整。但是,sidanmor 在此处给出了一个较小但更完整的足迹:stackoverflow.com/questions/9752963/…
    【解决方案3】:

    此处提供的解决方案在某些时候甚至大部分时间都有效,但并非无处不在。据我所知,找到 any 域的完整子域的最佳方法(记住,有时子域中也有句点!你可以有子子域等)是使用Public Suffix List,由 Mozilla 维护。

    不在公共后缀列表中的 URL 部分是子域加上域本身,由一个点连接。删除公共后缀后,您可以通过删除点之间的最后一段来删除域并仅留下子域。

    让我们看一个复杂的例子。假设您正在测试sub.sub.example.pvt.k12.ma.uspvt.k12.ma.us 是公共后缀,信不信由你!因此,如果您使用公共后缀列表,您可以通过删除已知后缀快速将其转换为sub.sub.example。然后你可以从sub.sub.examplesub.sub 剥离剩余部分的最后一部分,即域。 sub.sub 是您的子域。

    【讨论】:

    • 使用此列表的唯一缺点是它需要 Mozilla 发现域并管理它们的列表。它可以用作算法中的第一次通过,然后如果找不到匹配项,则使用其他更幼稚的方法之一。
    • @crush,我同意,这是个问题。但是,鉴于该列表用于 Firefox、Chrome、Opera 和 Internet Explorer(如 the Learn page of that site 中所述),我非常有信心它保持得很好。在最坏的情况下,一个非常奇怪的边缘情况可能会暂时失败。
    • 有趣的资源!知道此列表是否也将使用新的 GTLD 进行更新?此外,不知道这个过程是如此随意 - 任何注册商都可以选择将任何 tld 委托为顶级。
    【解决方案4】:

    除了@jlbang 提到的情况之外,这在大多数情况下都可以工作

    const split=location.host.split(".");
    let subdomain="";
    let domain="";
    if(split.length==1){//localHost
      domain=split[0];
    }else if(split.length==2){//sub.localHost or example.com
      if(split[1].includes("localhost")){//sub.localHost
        domain=split[1];
        subdomain=split[0];
      }else{//example.com
        domain=split.join(".");
      }
    }else{//sub2.sub.localHost or sub2.sub.example.com or sub.example.com or example.com.ec sub.example.com.ec or  ... etc
      const last=split[split.length-1];
      const lastLast=split[split.length-2];
      if(last.includes("localhost")){//sub2.sub.localHost
        domain=last;
        subdomain=split.slice(0,split.length-1).join(".");
      }else if(last.length==2 && lastLast.length<=3){//example.com.ec or sub.example.com.ec
        domain=split.slice(split.length-3,split.length).join(".");
        if(split.length>3){//sub.example.com.ec
          subdomain=split.slice(0,split.length-3).join(".");
        }
      }else{//sub2.sub.example.com
        domain=split.slice(split.length-2,split.length).join(".");
        subdomain=split.slice(0,split.length-2).join(".");
      }
    }
    const newUrl = 'http://example.com/some/path/here/' + subdomain
    

    【讨论】:

      【解决方案5】:

      我在现代 Typescript 中改编了 Vlad 的解决方案:

      const splitHostname = (
          hostname: string
        ): { domain: string; type: string; subdomain: string } | undefined => {
          var urlParts = /([a-z-0-9]{2,63}).([a-z.]{2,5})$/.exec(hostname);
          if (!urlParts) return;
          const [, domain, type] = urlParts;
          const subdomain = hostname.replace(`${domain}.${type}`, "").slice(0, -1);
          return {
            domain,
            type,
            subdomain,
          };
        };
      

      【讨论】:

        【解决方案6】:

        从 URL 获取子域

        function getSubdomain(url) {
        url = url.replace( "https://www.","");
        url = url.replace( "http://www.","");
        url = url.replace( "https://","");
        url = url.replace("http://", "");
        var temp = url.split("/");
        if (temp.length > 0) {
            var temp2 = temp[0].split(".");
            if (temp2.length > 2) {
                return temp2[0];
            }
            else {
                return "";
            }
        }
        return "";
        

        }

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-01-16
          • 2017-03-12
          • 1970-01-01
          • 2022-07-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多