1598be378367f43a39cf73330358f8614c45f807
1function extractDomain(url, y) {
2 var domain;
3
4 //remove protocol
5 if (url.indexOf("://") > -1) {
6 domain = url.split('/')[2];
7 }
8 else {
9 domain = url.split('/')[0];
10 }
11
12 //remove port number
13 domain = domain.split(':')[0];
14
15 if (y == 1) {
16
17 d = splitHostname(domain);
18
19 if (d.subdomain != "www" && d.subdomain != "") {
20 return d.subdomain;
21 }
22 else {
23 return d.domain;
24 }
25
26 } else {
27 return domain;
28 }
29}
30
31function addhttp(url) {
32 if (!/^(f|ht)tps?:\/\//i.test(url)) {
33 url = "http://" + url;
34 }
35 return url;
36}
37
38function splitHostname(h) {
39 var result = {};
40 var urlParts = new RegExp('([a-z\-0-9]{2,63})\.([a-z\.]{2,5})$').exec(h);
41 if (urlParts == null) {
42 console.log("Error formatting link " + h);
43 return "";
44 }
45 result.domain = urlParts[1];
46 result.type = urlParts[2];
47 result.subdomain = h.replace(result.domain + '.' + result.type, '').slice(0, -1);;
48 return result;
49}