Get subdomain and load it to url with greasemonkey

China☆狼群 提交于 2019-11-30 01:58:15
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);
Vlad Filip

The answer provided by Derek will work in the most common cases, but will not work for "xxx.xxx" sub domains, or "host.co.uk". (also, using window.location.host, will also retrieve the port number, which is not treated : http://www.w3schools.com/jsref/prop_loc_host.asp)

To be honest I do not see a perfect solution for this problem. Personally, I've created a method for host name splitting which I use very often because it covers a larger number of host names.

This method splits the hostname into {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());

This method only returns the subdomain as a string:

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)

These two methods will work for most common domains (including co.uk) NOTE: the slice at the end of sub domains is to remove the extra dot.

I hope this solves your problem.

The solutions provided here work some of the time, or even most of the time, but not everywhere. To the best of my knowledge, the best way to find the full subdomain of any domain (and remember, sometimes subdomains have periods in them too! You can have sub-subdomains, etc) is to use the Public Suffix List, which is maintained by Mozilla.

The part of the URL that isn't in the Public Suffix List is the subdomain plus the domain itself, joined by a dot. Once you remove the public suffix, you can remove the domain and have just the subdomain left by removing the last segment between the dots.

Let's look at a complicated example. Say you're testing sub.sub.example.pvt.k12.ma.us. pvt.k12.ma.us is a public suffix, believe it or not! So if you used the Public Suffix List, you'd be able to quickly turn that into sub.sub.example by removing the known suffix. Then you could go from sub.sub.example to just sub.sub after stripping off the last portion of the remaining pieces, which was the domain. sub.sub is your subdomain.

This could work in most cases except for the one that @jlbang mention

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!